-1

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.

BBK
  • 9
  • 1
  • 1
    How is the workbook in question opened? Not related to your question but you are highly encouraged to read on [how to avoid using Select](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba?rq=1). – Raymond Wu May 10 '22 at 03:23
  • 1
    Where is the code located (workbook name e.g. `data-...` or `Real Time.xlsm` or a third one)? What is the name of the worksheet in the `data-...` workbook? What is the code supposed to do? What does *"doesn't work"* mean? Is there an error (number and description)? – VBasic2008 May 10 '22 at 05:03

0 Answers0