简体   繁体   中英

VBA Macro workbook.open or workbook.activate through variable reference

How do I reference my primary workbook and the second workbook I open through this sub procedure? I attempt to do workbooks.("client_path").activate as my goal with this macro is to open a separate workbook, which is assigned to variable client_path and reconcile every (1 to 200) values in column A:A with all the values of column K:K of my primary workbook. If a value is found on the client_path workbook (again column A:A), but not on my primary workbook (again column K:K) - I would like to add the unique value to column M:M of my primary workbook. Opposite logic, I would like any value found on my primary workbook but not found on my client_path workbook to appear in column N:N of my primary workbook.

The name of my the primary workbook which I am developing this code is title "Client DIRTY watchlist" The contents of workbook client_path update daily and are useless as time passes.

Do I need to create a function to accomplish this variable workbook reference?

Sub Client_Dirty_Recon()

Dim Client_path As String
Dim Client_watchlist As Workbook
Dim Client_client_email As Workbook
Set Client_watchlist = ActiveWorkbook
Dim email_range As Range
Dim watchlist_range As Range

Application.ScreenUpdatClient = False  
Client_path = Range("Path")
Workbooks.Open Client_path
Dim recon_list As Range

'For Each n In recon_list:
Dim i As Variant
    For i = 1 To 200

        Set email_range = ActiveWorkbook.ActiveSheet.Range("A" & i)
        Dim b As Variant

            For Each b In email_range

                Set watchlist_range = Sheets("Client DIRTY watchlist").Range("B:B")


                'if b
            Next b

    Next i

End Sub

Can you just make references to your workbook earlier?

Dim wb as workbook
Dim wbDirty as workbook

set wb = thisWorkbook
set wbDirty = workbooks.open Client_Path

Then when you define the ranges, Excel knows which workbook they belong to.

Dim rngReconcile as range
Dim rngWatch as range

set rngReconcile = wb.Sheets(1).Range("K:K")
set rngWatch = wbDirty.Sheets("Client DIRTY watchlist").Range("B:B")

Then continue on with your looping code

dim Wb as workbook

set wb= Workbooks.Open (Client_path).activate

this opens, and activates it all in one line, an sets a variable for later use (wb).

note that refering later to wb will NOT open it again, and NOT activate it again, its just reference to the wb ! (unless you tell him to)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM