Problem with ROWS in VBA

madkinson

Board Regular
Joined
Dec 17, 2004
Messages
113
Office Version
  1. 365
Platform
  1. Windows
I am trying to write some VBA that will reference two variables, however I get a run-time error when I debug.

Here is the code:

Rows("StartRow:EndRow-1").Select

where Dim StartRow As Integer
and Dim EndRow As Integer

For example, when I run my subroutine, it assigns a value of 2 to StartRow and 4 to EndRow. I can see this in my Locals window. What am I doing wrong?

p.s. I apologize for not posting the entire code, but the IT staff don't let me download any add-ons :(
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Anything you enclose in quotes in treated as literal text, not variables. Try:

Rows(StartRow & ":" & EndRow-1).Select
 
Upvote 0
The goal of the rest of the code is to hide the selected rows.
 
Upvote 0
Hi

Your code is being interpreted as text where it falls between the double quotes:

Rows("StartRow:EndRow-1").Select

everything in bold is being seen as text when Excel tries to run the code. What you want to do is have Excel see your variables, therefore they need to be not surrounded by quotes as Vog indicated:

Code:
Rows(StartRow & ":" & EndRow-1).Select

Best regards

Richard
 
Upvote 0
Also, when dealing with row numbers in variables, it's better to use the Long data type since the amount of rows available in Excel exceeds the range of the Integer data type.
 
Upvote 0
p.s. I apologize for not posting the entire code, but the IT staff don't let me download any add-ons

You can enclose your code in HTML Code tags.

"["code"]" Code Here "["/code"]"

No quotes. ;)

HTH,

Smitty
 
Upvote 0
THANKS!! You guys really bailed me out. It worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top