wb.Name Like VBA Not Working the first time

%3CLINGO-SUB%20id%3D%22lingo-sub-3347961%22%20slang%3D%22en-US%22%3Ewb.Name%20Like%20VBA%20Not%20Working%20the%20first%20time%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3347961%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20had%20actually%20created%20another%20topic%20for%20this%20issue%20then%20thought%20it%20was%20fixed.%20However%2C%20turns%20out%20it's%20not.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20below%20doesn't%20work%20first%20time%20the%20workbook%20is%20opened.%20If%20I%20close%20it%20and%20re-open%20it%20works.%20How%20can%20I%20fix%20this%3F%3C%2FP%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20CopyRAWMMP()%0AApplication.DisplayAlerts%20%3D%20False%0ADim%20Ct%20As%20Long%0AFor%20Each%20wb%20In%20Application.Workbooks%0A%20%20%20%20If%20Trim(LCase(wb.Name))%20Like%20%22data-*%22%20Then%0A%20%20%20%20%20%20%20%20Ct%20%3D%20Ct%20%2B%201%0A%20%20%20%20%20%20%20%20wb.Activate%0A%20%20%20%20%20%20%20%20Exit%20For%0A%20%20%20%20End%20If%0ANext%20wb%0A%20%20%20%20If%20Ct%20%3D%200%20Then%0A%20%20%20%20MsgBox%20%22MSG%22%0A%20%20%20%20%20Exit%20Sub%0A%20%20%20%20End%20If%0A%20%20%20%20%0A%20%20%20%20Range(%22A2%3AN2%22).Select%0A%20%20%20%20Range(Selection%2C%20Selection.End(xlDown)).Select%0A%20%20%20%20Selection.Copy%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20Selection.Copy%0A%20%20%20%20Workbooks(%22Real%20Time.xlsm%22).Activate%0A%20%20%20%20Worksheets(%22Raw%20MMP%22).Activate%0A%20%20%20%20Range(%22RawMMP%5BDate%5D%22).Select%0A%20%20%20%20ActiveSheet.Paste%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20Application.DisplayAlerts%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EImmediate%20window%20doesn't%20recognize%20that%20the%20workbook%20is%20opened.%20I%20was%20advised%20that%20the%20workbook%20is%20opening%20in%20a%20different%20instance.%20But%20I%20don't%20even%20know%20what%20that%20means.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'd%20really%20appreciate%20your%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3347961%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3350835%22%20slang%3D%22en-US%22%3ERe%3A%20wb.Name%20Like%20VBA%20Not%20Working%20the%20first%20time%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3350835%22%20slang%3D%22en-US%22%3EWhat%20*exactly*%20happens%20the%20first%20time%20you%20open%20the%20file%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3377511%22%20slang%3D%22en-US%22%3ERe%3A%20wb.Name%20Like%20VBA%20Not%20Working%20the%20first%20time%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3377511%22%20slang%3D%22en-US%22%3EThank%20you%20for%20your%20response.%20I%20missed%20that%20I%20got%20a%20response.%20I%20hadn't%20actually%20thought%20of%20that%20since%20I%20was%20using%20an%20errorhandler%20and%20now%20for%20the%20life%20of%20me%2C%20I%20can't%20reproduce%20the%20error.%20I'll%20try%20on%20a%20different%20computer.%3C%2FLINGO-BODY%3E
Contributor

Hello,

 

I had actually created another topic for this issue then thought it was fixed. However, turns out it's not.

 

The code below doesn't work first time the workbook is opened. If I close it and re-open it works. How can I fix this?

 

 

 

Sub CopyRAWMMP()
Application.DisplayAlerts = False
Dim Ct As Long
For Each wb In Application.Workbooks
    If Trim(LCase(wb.Name)) Like "data-*" Then
        Ct = Ct + 1
        wb.Activate
        Exit For
    End If
Next wb
    If Ct = 0 Then
    MsgBox "MSG"
     Exit Sub
    End If
    
    Range("A2:N2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Workbooks("Real Time.xlsm").Activate
    Worksheets("Raw MMP").Activate
    Range("RawMMP[Date]").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Application.DisplayAlerts = True
End Sub

 

 

 

 

Immediate window doesn't recognize that the workbook is opened. I was advised that the workbook is opening in a different instance. But I don't even know what that means.

 

I'd really appreciate your help.

 

 

2 Replies
What *exactly* happens the first time you open the file?
Thank you for your response. I missed that I got a response. I hadn't actually thought of that since I was using an errorhandler and now for the life of me, I can't reproduce the error. I'll try on a different computer.