Looking for correct method for assigning variables

erock1357

New Member
Joined
Jun 19, 2014
Messages
11
Hello, I am trying to make a macro that will essentially copy several different cells from one workbook to another. I used the macro recorder and it gave me the following code: (I attempted to add in the variable)

Code:
Sub DoAllTheThings()
'
' DoAllTheThings Macro
'
    Dim eVariable1 As Integer
    eVariable1 = "Extra!C4"
    
    ActiveCell.FormulaR1C1 = "=eVariable1 & Invoice!R6C8"
    ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R7C8"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R11C2"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R32C9"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R33C9"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R34C9"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R35C9"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R36C9"
    'ActiveCell.Offset(0, 1).Select
    'ActiveCell.FormulaR1C1 = "=[201400001.xlsx]Invoice!R38C9"
    'ActiveCell.Offset(1, -8).Select
End Sub

Essentially, What I am looking to do is use cell C4 on sheet Extra! to reference what document the data is to be pulled from. In cell Extra!C4, I have 201400001.xlsx with no brackets or anything in it. I have this cell auto updating everytime I run the macro so that when I manually "loop" the macro,it will auto increment the cell +1 so that it will open the next numbered workbook in sequence.

Summary:
I need a variable to replace [201400001.xlsx] in the above code with a cell reference to Extra!C4 so that I can use this cell to dictate what document to open.

Thanks in advance for the help :)
erock1357
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
On the face of it, this would help settle the variable question. As a VBA newcomer I understand why you'd have assigned an Integer variable because hey, they are all integers, but really you need to declare as a String type in this case, example


Dim eVariable1 As String eVariable1 = Worksheets("Extra").Range("C4").Value

I can't predict how this will turn out for you based on real practice with your workbook, but to answer your declaration question, a String type is what you'd need in this case.
</pre>.
 
Upvote 0
Thanks for your reply. That does help me with my variable question.

How do I know whether to set as a string vs an integer or anything else for that matter? What would be a good source for reading up on that?
 
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,318
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top