0

I have searched for a solution but can't seem to find one. If one exists, please point me to it. The question is how do I name a range in VBA.

    wrkSheet.Range("A1").Name = "Test"

Works fine but as soon as I change it to

    wrkSheet.Range("A1:B2").Name = "Test"

gives me problems. Note, wrkSheet is a worksheet object that is defined earlier.

Thanks

MathLover
  • 77
  • 1
  • 3
  • 12
  • "gives me problems" means what exactly? `wrkSheet` is really a string, or is it a worksheet object? If it's a string then you want `Sheets(wrkSheet).Range("A1:B2").Name = "Test"` – Tim Williams Mar 12 '14 at 18:46

2 Answers2

1

you can use this to name a range. Not sure why wrkSheet is a string as it should be a worksheet object

Dim ws As Worksheet
Dim r As Range
Set ws = Sheets(1)
Set r = ws.Range("A1:B2")
r.Name = "test"
Sorceri
  • 7,870
  • 1
  • 29
  • 38
0

You're not naming a range there... you giving values to a excel range of cells... To name a range you do

range = wrkSheet.Range("A1:B2").Name

Then you can do range(1,1) = "test" for example.

Stephenloky
  • 413
  • 8
  • 21