Radioactive Coffe
Radioactive Coffe

Reputation: 155

Insert Code Into A Variable?

The code below should create the right picture of what I'm trying to do, which is to insert a code into a variable that can be used later on.

ALSO:

S1 is a String, fixedInterest is a String

    S1 = ThisWorkbook.Sheets("Sheet1")
    fixedInterest = S1.Range("A1").Value

Upvotes: 0

Views: 105

Answers (1)

Chrowno
Chrowno

Reputation: 198

If I'm not mistaken you want to store the codeline itself(not the value) in a variable so you don't have to use it every time you need that value.

The most feasible way of achieving that would be the creation of a function.

Using your variables:

Function S1() as String

    S1 = ThisWorkbook.Sheets(1).Range("A1").Value

End Function

In your main code you can then assign the value this function returns to a variable:

fixedInterest = S1()

Also to clarify: A string variable always only returns a string value. You can't 'convert it back to code'. It can however be used in methods that make us of a string.

e.g.:

Dim strName as string

strName = "MyWorkbook"

Workbooks(strName).open
'is the same as
Workbooks("MyWorkbook").open

__ Edit:

Dim S1 as worksheet

Set S1 = ThisWorkbook.Sheets(1)

S1.Range("A2").Value= "something"

Upvotes: 1

Related Questions