selecting multiple rows -vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
the code below is to type blah in cell a43 then hide rows 25 to 70 for sheets 1 to 5. it works only for sheet1 and then i get error message "selection method of range class failed"

Code:
Sub fivesheets()
For i = 1 To 5
    ThisWorkbook.Worksheets(i).Range("a43").Value = "blah"
    ThisWorkbook.Worksheets(i).Rows("25:70").Select
    Selection.EntireRow.Hidden = True
Next i
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You need to do it like
Code:
Sub fivesheets()
For i = 1 To 5
    ThisWorkbook.Worksheets(i).Range("a43").Value = "blah"
    ThisWorkbook.Worksheets(i).Rows("25:70").Hidden = True
Next i
End Sub
You were trying to select cells on a sheet that wasn't active, which can't be done.
 
Upvote 0
I see why did not work now. Thanks a lot for your help.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Sorry I am thinking about your comment: "You were trying to select cells on a sheet that wasn't active, which can't be done.".
The first line in the loop

Code:
ThisWorkbook.Worksheets(i).Range("a43").Value = "blah"

that makes worksheets(i) get selected

then the second line
Code:
ThisWorkbook.Worksheets(i).Rows("25:70").Select

will not change anything in term which sheet is selected, it is still worksheet(i). This line will select rows 25 to 70 of the current worksheets(i)

then the last line
Code:
 Selection.EntireRow.Hidden = True
will hide the selected rows.

Am I right? Thank you.
 
Last edited:
Upvote 0
Am I right?
Nope.
You first point is wrong, all that line of code is doing is writing the value "blah" to cell A43 of a sheet with index number i.
It is not selecting or activating anything.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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