Specifying a range across all sheets in the workbook

pells

Active Member
Joined
Dec 5, 2008
Messages
361
Is it possible to specify a range across all worksheets in a workbook?

I am looking to check across all workbooks in cell range R6 to see if anything is entered in the cell. ActiveSheet.Range ("R6") works for the active sheet but I need to check across all sheets.

Many thanks.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Something like this:

Code:
Sub Test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
    If ws.Range("R6") <> "" Then MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
Next ws
End Sub
 
Upvote 0
Maybe something like
Code:
Dim was As Worksheet
For Each ws In Worksheets
    If Range("A6").Value = "" Then
    ....more code here
Next ws
 
Upvote 0
Something like this:

Code:
Sub Test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
    If ws.Range("R6") <> "" Then MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
Next ws
End Sub
Many thanks for this njimack.

I think I made a slight mistake with my original post as I do need to specify some worksheet names but not all of them - I hope this makes sense?

This can be done?

Once again, many thanks for your reply.
 
Upvote 0
Amended for specific sheet names

Code:
Sub Test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name = "Sheet1" or ws.Name = "Sheet2" Then 'amend as required
    If ws.Range("R6") <> "" Then MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
End If
Next ws
End Sub
 
Upvote 0
Amended for specific sheet names

Code:
Sub Test()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.Name = "Sheet1" or ws.Name = "Sheet2" Then 'amend as required
    If ws.Range("R6") <> "" Then MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
End If
Next ws
End Sub
Fantasic - many thanks for this, it works perfectly! :-)
 
Upvote 0
Hi

Another option:

Code:
Sub Test()
Dim ws As Worksheet
 
For Each ws In ThisWorkbook.Worksheets(Array("Sheet1", "Sheet2"))
    If ws.Range("R6") <> "" Then MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
Next ws
End Sub
 
Upvote 0
Hi

Another option:

Code:
Sub Test()
Dim ws As Worksheet
 
For Each ws In ThisWorkbook.Worksheets(Array("Sheet1", "Sheet2"))
    If ws.Range("R6") <> "" Then MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
Next ws
End Sub
Many thanks pgc01, this works perfectly too, brilliant! :-)

I am just thinking, how do I go about making a slight change to this so that if cell R6 is blank across all sheets that I specify, then continue to run the sub and if cell R6 is not blank, stop the sub?

Once again, many thanks.
 
Upvote 0
Many thanks pgc01, this works perfectly too, brilliant! :-)

I am just thinking, how do I go about making a slight change to this so that if cell R6 is blank across all sheets that I specify, then continue to run the sub and if cell R6 is not blank, stop the sub?

Once again, many thanks.

This should do the trick (untested)

Code:
Sub Test()
Dim ws As Worksheet
Dim Count As Integer
For Each ws In ThisWorkbook.Sheets
If ws.Name = "Sheet1" Or ws.Name = "Sheet2" Then 'amend as required
    If ws.Range("R6") <> "" Then
        Count = Count + 1
        MsgBox "Sheet name:" & ws.Name & vbCr & "Value in R6:" & ws.Range("R6").Value
    End If
End If
Next ws
If Count = 0 Then Exit Sub
'rest of your code here
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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