Forum Discussion
Selecting an Excel Range in VBA
I have attached the spreadsheet.
Go to the "Weekly" sheet and click on the "Import Print Data" command button to see the error occur
If I comment the line of code that causes the error and un-comment the line of code above the error line, the code works as wanted.
Sheets("Groups").Range(Cells(2, 2), Cells(34, 12)).Select ' this is the line of code that gets the error.
I need to be able to select a range using numeric rows and columns instead of numeric rows and alpha columns.
I have placed comment lines above and below the code with rows of asterisks
WallisMcMath as I suspected the "cells" references are referring back to the Weekly sheet instead of the Groups sheet. Besides, your technique of using .select to continuously change focus throughout your code is not a good idea for a number of reasons. after a brief look at your code you should declare a variable for those sheets (e.g. Dim wstGroups as worksheet) and then set wstGroups = Sheets("Groups") and then just use that variable to find areas and such.
Also as previously mentioned, you can just take action of those areas instead of doing the .select
I this example code I am using the "with" statement to make life easier and prefix the "cells" with a period to say use the "Groups" sheet instead of whatever sheet it wants (and it worked for me but it hit another error later in your code):
.Range(.Cells(2, 2), .Cells(34, 12)).Copy
End With
Sheets("Weekly").Range("A1").Select 'xx
ActiveSheet.Pictures.Paste(Link:=True).Select
'************************************************************************************************************