I have two workbooks that I am using for data exchange. Workbook A is the main one, which pulls info from Workbook B. I have Macros running in A that I only want to be performed in A, however, when I open A before B, the macros are being performed in B. If I open B before A then the macros work in A as expected.
makeQuickChangesToGameState()
I attempted to use sheet/workbook activation as well as toying with an If statement (commented out), with no luck. Any pointers would be great!
Edit: Here is code to Background_Lists
Private Sub Workbook_Open()
'If ThisWorkbook.Name = "A" Then
ThisWorkbook.Sheets("Background").Select
Call Background_Lists
Call Find_Missing
'End If
End Sub
End Sub
答案 0 :(得分:1)
尝试定义每个工作簿,例如:
Private Sub Background_Lists()
Dim WorkbookA As Workbook
Dim WorkbookB As Workbook
Dim WorkSheetA as Worksheet
Dim WorkSheetB as Worksheet
Dim WorkSheetParts as Worksheet
Set WorkbookA = Workbooks("PATIENT_TRACK.xlsm")
Set WorkbookB = Workbooks("PATIENT_DATA.xlsx")
Set WorkSheetA =WorkbookA.Worksheets("Background")
Set WorkSheetB =WorkbookB.Worksheets("Sheet1")
Set WorkSheetParts =WorkbookA.Worksheets("Parts")
a = 0
WorkSheetA.Range("E4:E2004").Clear
WorkSheetA.Range("B4:B2004").Value = WorkSheetParts .Range("B18:B2018").Value
WorkbookA.Range("D4:D2004").Value = WorkSheetB.Range("A2:A2002").Value
For i = 4 To 2004
If WorkSheetA.Cells(i, 4).Value >= 300000 Then
WorkSheetA.Cells(4 + a, 5).Value = WorkSheetA.Cells(i, 4).Value
a = a + 1
End If
Next i
End Sub
现在,您可以使用WorkbookA或WorkbookB代替ThisWorkbook,并将宏指向正确的工作簿。如果这对您有用,请告诉我。
答案 1 :(得分:0)
将sub
Background_Lists调整为以下内容:
Sub Background_Lists()
a = 0
With ThisWorkbook.Sheets("Background")
.Range("E4:E2004").Clear
.Range("B4:B2004").Value = .Range("=Parts!B18:B2018").Value
.Range("D4:D2004").Value = .Range("=[B.xlsx]Sheet1!A2:A2002").Value
For i = 4 To 2004
If .Cells(i, 4).Value >= 300000 Then
.Cells(4 + a, 5).Value = .Cells(i, 4).Value
a = a + 1
End If
Next i
End With
End Sub