Putting a string variable in a Range function

stephicohu

New Member
Joined
Jan 27, 2023
Messages
26
Office Version
  1. 365
Platform
  1. MacOS
Hi!

I have been self-teaching myself VBA Excel and been doing this for about a month. I have been enjoying the learning experience. I do have several questions that has been bothering me. I am wanting to put a variable in the Range function so I can change the value intermittently. This is an example of my work:

Absolute Macro
'

'
Dim Num As String

Num = "AG5"


Range(" & Num &").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-31]:RC[-25])"
Range("AH5").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-32]:RC[-26])"
Range("AH6").Select


I have tried the following options and they didn't work:

Range("Num")
Range (Num)

the error message I get is this:

Run-time error '1004':
Method 'Range' of 'object '_Global' failed.

I do have two questions to ask:

1) what am I doing wrong?
2) what does the run-time error means?

Thanks for your help in this matter. I believe it might be a simple thing but I haven't been able to find the answer yet. Oh yeah, I am running VBA Excel on MacOS.......
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This should work
VBA Code:
Range(Num).Select
 
Upvote 0
Solution

Forum statistics

Threads
1,214,402
Messages
6,119,304
Members
448,886
Latest member
GBCTeacher

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