本文介绍了VBA 宏 workbook.open 或 workbook.activate 通过变量引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何引用我的主要工作簿和通过此子过程打开的第二个工作簿?我尝试做 workbooks.("client_path").activate 因为我使用这个宏的目标是打开一个单独的工作簿,它被分配给变量 client_path 并协调每个 (1 到 200) 列 A:A 中的值以及我的主要工作簿的 K:K 列的所有值.如果在 client_path 工作簿(再次列 A:A)中找到值,但不在我的主工作簿(再次列 K:K)中 - 我想将唯一值添加到列 M:我的主要工作簿的 M.相反的逻辑,我希望在我的主要工作簿中找到但在我的 client_path 工作簿中找不到的任何值出现在我的主要工作簿的 N:N 列中.

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.

我正在开发此代码的主要工作簿的名称是标题Client DIRTY watchlist"工作簿client_path的内容每天更新,随着时间的推移毫无用处.

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

然后当您定义范围时,Excel 知道它们属于哪个工作簿.

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")

然后继续你的循环代码

这篇关于VBA 宏 workbook.open 或 workbook.activate 通过变量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:04