Forum Discussion

FrankNStein's avatar
FrankNStein
Copper Contributor
Nov 25, 2018

Dim a Variable in Excel VBA

Can I refer to a cell value/text for "dimming" a variable name, instead of typing it manually?

Something like this: "Dim & Range("A1").Text & as Variant", where A1 contains "myText".

Thank you in advance. Regards.

1 Reply

  • Haytham Amairah's avatar
    Haytham Amairah
    Silver Contributor

    Hi,

     

    In VBA, if you want to refer the cell's value/text in the code, you can do this!

    But you need to declare the Variable name as follows:

    Dim Ce11_A1 As Variant

     

    Then assign the cell range to the variable you just declared:

    Cell_A1 = Range("A1")

    So that you got the value/text of cell A1 stored in the variable Cell_A1.

     

    After that, you can use this variable as you want in the code.

     

    Example

    Sub test()
        Dim Ce11_A1 As Variant
        Cell_A1 = Range("A1")
        MsgBox Cell_A1
    End Sub

     

    Hope that helps

Resources