Forum Discussion
hsbign15
Jan 12, 2018Copper Contributor
Variable Cell Reference
Here is my code:
-----------------------------------
Dim k As Integer
Dim dy As String
Dim day1 As String
day1 = "A1"
Dim day2 As String
day2 = "K2"
Dim day3 As String
day3 = "H7"
For k = 1 To 3
dy = "day" & CStr(k)
Range(dy).Value = "Hello:
Next k
-----------------------------------------
After the code I want cells A1, K2, H7 to have values of "Hello"
I want to declare many days (Ex. Up to day37) each with a different cell reference.
How do I code it to change all the cells referenced from day1 to day37?
Thank You
3 Replies
Sort By
- Haytham AmairahSilver Contributor
Hi,
Your code doesn't work and contains unnecessary objects!
If you want to change the cells A1, K2 and H7 to have the value of "Hello"
Simply, you just need to this one:
Sub process() Range("A1,K2,H7").Value = "Hello" End Sub
But please clarify your problem with some screenshots!
The cells that you want to change contain values and you want to replace them with the value of "Hello" ???
Or it's just blank cells?
Is the day1 to day37 values are the header of these cells that you want to change?
- hsbign15Copper Contributor
I do not simply want to directly add the cells inside Range(). I want to add an integer to "day" and use that to reference day1, day2, day3, etc. which are the different cells to change. I need it this way so that I can change the number of iterations within the For loop. For example:
Dim days As Integer
days = 31
Dim day1 As String
day1 = "A1"
Dim day2 As String
day2 = "K2"
Dim day3 As String
day3 = "H7"
Dim day4 As String
day4 = "G4"
..........All the Way to day37 with different cell values......
For k = 1 To days
Range(day & k).Value = "Hello"
Next k
-------------------------------------------------------------
In the example (which doesn't work), I want all the cells that are referenced in the day1, day2, day3, all the way to "day & days" to have the value "Hello". This way I can change the number of iterations of the For loop by changing the days variable. Also, if I want all I have to do is change the value of day1, day2, etc. instead of having to manually change the cells within Range() in my entire code.
- Haytham AmairahSilver Contributor
I tried to find a solution for you, but I could not.
Try to post your request here, this the best place to get support!