mojo3340
mojo3340

Reputation: 549

VBA "Like" operator and wildcard

I have a loop which activates a certain file and copies data; it is not working and i cannot see why. Please see below:

For Each w In Workbooks
    If w.Name Like "*File 1*" Then
        Windows(w.Name).Activate
        Sheets("Test").Range("C7:C15").Copy
    End If
Exit For
Next w

The wildcard is there because in reality, each week the name of the file will change from "File 1 - week1" to "File 1 - week2", and so on. The point is, the vode stops at the like operator line, so presuming an issue with the "File 1"?

Any help greatly appreciated

Upvotes: 0

Views: 2383

Answers (1)

Ryan Wildry
Ryan Wildry

Reputation: 5677

One issue appears you are exiting after the first iteration. From the way you described the problem, it sounds like you want to stop searching after you've found a match, not after the first try.

Simply move the Exit For, like this:

For Each w In Workbooks
    If w.Name Like "*File 1*" Then
        Windows(w.Name).Activate
        Sheets("Test").Range("C7:C15").Copy
        Exit For
    End If
Next w

Upvotes: 1

Related Questions