Home » excel » excel – Can cell number for vba be given using two variables

excel – Can cell number for vba be given using two variables

Posted by: admin April 23, 2020 Leave a comment

Questions:
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.

How to&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