Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can cell number for vba be given using two variables

Tags:

excel

vba

Dim VAR1, VAR2, VAR3, VAR4, AS variant

VAR1 = Worksheets("Sheet3").Cells(3, "J").Value 
VAR2 = Worksheets("Sheet3").Cells(3, "K").Value 
VAR3 = Worksheets("Sheet3").Cells(3, "L").Value 
VAR4 = Worksheets("Sheet3").Cells(3, "M").Value

=sheet1.range(VAR1,VAR2).COPY
=Sheet3.range(VAR2,VAR4).Pastespecial

for the above i have given cell numbers in Var1 Var2...Var4

Is it possible to define a cell location using two variables,

Var1=B
Var2=5

and somehow combine them to get B5,I want to do this in the macro itself.

like image 333
Viladimir Avatar asked Dec 30 '25 17:12

Viladimir


1 Answers

You can. VBA allows you to concatenate strings in a range assignment. In this case it looks like:

Sub test()
Dim Var1 As Variant
Dim Var2 As Variant
Dim rng As Excel.Range

Var1 = "B"
Var2 = "5"
Set rng = ActiveSheet.Range(Var1 & Var2)
End Sub
like image 171
Doug Glancy Avatar answered Jan 01 '26 11:01

Doug Glancy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!