Find sheet in any open workbook?

Bonacci8

New Member
Joined
Sep 15, 2008
Messages
26
I want to prompt the user to enter a sheet name in an input box and then have the macro search for that sheet name in any open workbook. Once it finds the sheet name I want it to run some minor code to format a pivot table on that sheet. As I know just enough vba to hurt myself....I can't seem to figure out how to search all the open workbooks. The reason for this approach is I want the user of one spreadsheet to be ale to hit a button that runs the macro,formats the pivot and the paste values it into her workbook. The person she gets the data seems to always change the formatting and the file name but the sheet name remains constant. I was thinking by avoiding having to input teh workbook name that it would allow the same code to be used on whichever sheet name was entered and avoid having to enter a workbook and sheet name. Any thoughts or is this just a bad idea?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi,

This may get you started.

Code:
Sub test()
Application.DisplayAlerts = False
Dim wb As Workbook
Dim MySheet As Worksheet
Dim Response As String
  Response = Application.InputBox("Enter Sheet Name")
'Loop through each open workbook
For Each wb In Workbooks
    If Not (wb.Name = ThisWorkbook.Name) Then
        wb.Activate
        For Each MySheet In Worksheets '' loop thru each worksheet
            If MySheet.Name = "Response" Then
               ''' Your code'''
            End If
        Next
    End If
Next wb
End Sub
 
Upvote 0
Code worked great!, one modification had to remove the quotes on the string name in the.... If MySheet.Name = "Response" Then .... line.

One other question is if the user enters nothing in the input box how would I test that to exit the sub?

Thanks Again!!!
 
Upvote 0
Hi,

See if this helps.

Code:
Sub test()
Application.DisplayAlerts = False
Dim wb As Workbook
Dim MySheet As Worksheet
Dim Response As Variant
  Response = Application.InputBox("Enter Sheet Name")
'Loop through each open workbook
If Response = "" Then
    Exit Sub
ElseIf Response = False Then '' user click Cancel
    Exit Sub
End If
For Each wb In Workbooks
    If Not (wb.Name = ThisWorkbook.Name) Then
        wb.Activate
        For Each MySheet In Worksheets '' loop thru each worksheet
            If MySheet.Name = Response Then
               ''' Your code'''
            End If
        Next
    End If
Next wb
End Sub
[/codde]
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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