简体   繁体   English

VBA Macro workbook.open或workbook.activate通过变量引用

[英]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. 我尝试做workbooks.("client_path").activate因为我的宏目标是打开一个单独的工作簿,该工作簿分配给变量client_path并将A:A列中的每个(1到200)值与所有值进行协调我的主要工作簿的K:K列。 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. 如果在client_path工作簿(同样是A:A列)上找到了一个值,但是在我的主要工作簿上(又是K:K列)上找不到了一个值-我想将唯一值添加到我的主要工作簿的M:M列中。 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. 相反,我希望在主工作簿上找到但在client_path工作簿上找不到的任何值都出现在主工作簿的N:N列中。

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. 我正在开发此代码的主要工作簿的名称为标题“ Client DIRTY监视列表”。工作簿client_path的内容每天更新,并且随着时间的流逝而无用。

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. 然后,当您定义范围时,Excel会知道它们属于哪个工作簿。

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). 这将打开并在一行中将其全部激活,并设置一个变量供以后使用(wb)。

note that refering later to wb will NOT open it again, and NOT activate it again, its just reference to the wb ! 请注意,稍后引用wb不会再次打开它,也不会再次激活它,它只是指向wb! (unless you tell him to) (除非你告诉他)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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