Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6543545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:21:35+00:00 2026-05-25T11:21:35+00:00

My code is: Sub zaa() ‘code to find another open file that has the

  • 0

My code is:

Sub zaa()
'code to find another open file that has the defined name "data" 
'    - useful where the name changes each month or week

For Each wb In Workbooks
  On Error GoTo abcd
  x = wb.Name
  Workbooks(x).Activate
  If Range("Data").Address <> "" Then y = wb.Name

  Exit Sub

abcd:
Next wb

End Sub

Basic goal to find an Excel file with a specific named range when I know it exists but don’t know the file name as it changes each week or month. Aim is to find the file and exit the sub at that point (or then do other code on that file and exit rather than going to other files.)

I find it works okay if I only have two files open but not if have more (unless the target one is second in line). Whilst I can run with what I have I thought others may benefit from what I have and I can have a more robust solution.

UPDATE:
Thanks to all those who have responded & to Mitch for putting original post in readable format! (I have since learnt how to correct that issue and to be able to copy code directly – which I have indicated in some comments below that I was having trouble with.)
I have had varying degrees of success – code from PaulStock & Reafidy both worked for me originally but PaulStock code has stopped working for me. Responses & code from Jean-François Corbett and Chris Neilsen have been helpful to me but presently when I run the code (as is) it appears to do nothing – ie doesn’t leave the sheet I run it from (and that’s not where the range name data appears). I was originally running the code in Excel 2007 in a sheet with other macros but to try and get different results have moved them to a stand alone sheet with no other macro files open. Have also tried running them from a file saved as ’97-’03 format. Neither yielded different results. Others with more experience than I (see the errors I made evidenced in comments discussion with Reafidy & bear in mind that my original posted code was result of material found through google and modified by me for specific task & application and implication that I am not savvy enough to have come up with it on my own) may find other solutions better for them but for now:

I am more than happy as Reafidy’s code works for me.

As I am not registered (I did try but it didn’t work) and don’t have enough reputation points I can not vote but I have put a check mark next to Reafidy’s solution.

FURTHER UPDATE:
I have now discovered that PaulStock & Jean-François Corbett’s code wasn’t working for me due to a very minor discrepancy on my part – the code contained “Data” whilst my named range was “data”. Making this change (to allow for case sensitivity) in their code means that both of their solutions now work for me and hence I have attempted to add a tick to their solutions! Unfortunately I have found that only one solution can have a tick.

I have also learnt that I took Chris’ code too literally. In an effort to test each code ‘as is’ that is what I did. A simple additon of ‘wb.activate’ in the section where he has ‘do stuff’ makes the code do what I want.

THANKS again for all four contributions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:21:36+00:00Added an answer on May 25, 2026 at 11:21 am

    You’re doing a few convoluted things in your code, and I think that’s what’s confusing the issue.

    Having you error handler (abcd:) start in the middle of a For...Next loop is incredibly bad practice and can only lead to confusion. I can’t think of a reason why this should ever be done.

    Anyhow, there’s no need to use error handling for this task. I understand that there are exceptional cases where bad VBA design forces us to use error handling instead of what should be built-in VBA functionality (e.g. testing whether a non-Variant array has been allocated), but this is not one of those cases. There’s a standard way to do it (.Names collection of workbook object), and using error handling instead of this is messy and convoluted and asking for trouble.

    Also, why say

    x = wb.Name
    Workbooks(x).Activate
    

    when you can just say wb.Activate? You’re not using x for anything. Or y, for that matter.

    The following works and is both simplified and optimised, relative to your original code as well as to the other answers that have been given up to now:

    Sub zaa2()
        Dim wb As Workbook
        Dim nm As Name
    
        For Each wb In Workbooks
            For Each nm In wb.Names
               If nm.Name = "Data" Then
                    wb.Activate
                    Exit Sub
               End If
            Next
        Next wb
    End Sub
    ' A workbook containing a range named "Data" is now activated 
    ' (if one is found amongst the open workbooks). 
    ' Note that there may be more than one, but only the first found is activated...
    

    EDIT: In your comment, you mention you had trouble due to confusion between uppercase "Data" and lowercase "data". To guard against this in the future, one possibility is to ignore case. This could be done as follows:

    If StrComp(nm.Name, "data", vbTextCompare) = 0 Then
    

    or

    If LCase(nm.Name) = "data" Then
    

    Both will return True if nm.Name is "Data", "data", "dATa", etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: Sub addDropdown(Name) ActiveSheet.DropDowns.Add(74.25, 60, 188.25, 87.75).Select Set n = ActiveSheet.DropDowns(Name)
Why does this VBA code not work?: Sub AllToGeorgia12() Selection.Font.Size = 12 Selection.WholeStory Selection.Font.Name
I'm using excel VBA. I want to press a button that opens another file
This is the code: sub function($&) { my $param1 = shift; my $code =
Here's a snippit of perl code: sub insert_timesheet { my $dbh = shift; my
Consider the following code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Using the following code: Private Sub MakeMeSomeXmlBeforeRyanGetsAngry() Dim db As New MyDBDataContext Dim customer
I have the following code: Private Sub SortWorksheet(ByVal sheet As Worksheet) Dim sStartColumn Dim
i have the following code Private Sub select_color_Click(ByVal sender As System.Object, ByVal e As
I'm reading Programming Perl , and I found this code snippet: sub new {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.