Glork
Glork

Reputation: 367

vba excel : runtime error 1004 method range of object _global failed

I'm having the error "runtime error 1004 method range of object _global failed" when I launched the following macro :

Dim nameDebut As Range, nameFin As Range ' <- my global variable
sub mySub()
...
   Set nameDebut = Range("A1").Offset(0, 1)
   Set nameFin = Range("A1").Offset(0, 20)
   Range("nameDebut:nameFin").Select ' <- fail occurs here
...

So I would like to select this range of cells like this. Is it possible ? Any workaround ?

Tx

Upvotes: 0

Views: 690

Answers (1)

FunThomas
FunThomas

Reputation: 29286

Just change the last line to

Range(nameDebut,nameFin).Select 

Not discussing your code, but using select is nearly never needed in VBA. Maybe this can help:

dim myRange as Range
set myRange = Range(nameDebut,nameFin)
' Now use `myRange` for whatever you want to do...

Upvotes: 1

Related Questions