Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generalize range with a changing variable

Sub test11()
Dim i As Integer
i = 15
ActiveWorkbook.ActiveSheet.Range("b2:b11") = "love"
ActiveWorkbook.ActiveSheet.Range("c2:c&i") = "Hate"
End Sub

This code line gives error 1004. How to change this line so that I can set i to different numbers every time. The above code is just a part of long code where range is used number of times.

ActiveWorkbook.ActiveSheet.Range("c2:c&i") = "Hate"

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You must refer i outside the quotes. This is because, VBA assumes i as a part of range which is not the case (because i is not the range itself, instead value of i refers to an integer which is part of range). Hence, these kind of variable substitution must be done outside quotes ("")

Try this:

Sub test11()
Dim i As Integer
i = 15
ActiveWorkbook.ActiveSheet.Range("b2:b11") = "love"
ActiveWorkbook.ActiveSheet.Range("c2:c" & i) = "Hate"
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading