Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Variable in Range VBA

Is it possible to use a variable in Range?

I have some cells called test1, test2, etc

Range("test1") works like Range("A1"), but Range(variable) or Range("variable") does not work.

I want to set a variable cellName and when I loop through test1, test2, the cells called test1, and test2, does something.

Dim cellName as String
Dim rng As Range, cell As Range

Set rng = Range("C5:C8")

For each cell In rng
  cellName = cell.Value
  Worksheets("Sheet2").Range("cellName").Value = "Good"
Next cell
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try:

Sub test()
Dim cellName As String
Dim rng As Range

For Each rng In Range("C5:C8")
  cellName = rng.Value
  Worksheets("Hoja1").Range(cellName).Value = "Good"
Next rng

Set rng = Nothing


End Sub

enter image description here

enter image description here

about 4 years ago · Santiago Trujillo Report

0

If you want to modify cells in C5:C8 you just need to do :

Dim cellName as String
Dim rng As Range, cell As Range

Set rng = Range("C5:C8")

For each cell In rng
  cell = "Good"
Next cell
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!