Find open workbook set worksheet range

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
727
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I need some help, the more I try the worse my code gets :-)

What I'm trying to accomplish is
1. look through all open workbook for one that has "connect" in the name.
2. look through all open worksheets in the "connect" workbook and name myrange to the sheet that begins with "KPI"

Am I even close?

VBA Code:
    Dim wb As Workbook
    Dim ws As Worksheet
    For Each wb In Workbooks
        If wb.Name Like "*connect*" Then
            For Each ws In Worksheets
                If ws.Name Like "KPI*" Then
                    Set myrange = wb.ws
                End If
            Next ws
        End If
    Next wb
    
    With myrange
        .Range("A1:P20").Select
    End With

Thank you
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I found a solution

VBA Code:
    Dim wb As Workbook
    Dim ws As Worksheet
    For Each wb In Workbooks
        If wb.Name Like "*connect*" Then
            For Each ws In Worksheets
                If ws.Name Like "KPI*" Then
                    Set mypath = Workbooks(wb.Name).Worksheets(ws.Name)
                End If
            Next ws
        End If
    Next wb
    
    With mypath
        .Range("A1:P20").Select
    End With
 
Upvote 0
Alas, my solution did not work. I must have already activated the workbook. I'm still in need of help on this.

Thank you
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,331
Members
452,636
Latest member
laura12345

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