macos - 编译错误 : searching for one specific data point, 循环遍历整个工作簿,复制/粘贴数据

标签 macos vba excel

Excel VBA 初学者回来了解更多。我正在创建一个执行以下两件事的宏:

1)在单个工作簿中的多个工作表中搜索特定的数据(名称),下面的变量 A
2)如果出现该名称,将特定范围的单元格从工作表(下面的变量 X)复制到主文件(下面的变量 B)

Sub Pull_X_Click()

Dim A As Variant 'defines name
Dim B As Workbook 'defines destination file
Dim X As Workbook 'defines existing report file as source
Dim Destination As Range 'defines destination for data pulled from report
Dim ws As Worksheet
Dim rng As Range

A = Workbooks("B.xlsm").Worksheets("Summary").Range("A1").Value
Set B = Workbooks("B.xlsm")
Set X = Workbooks.Open("X.xlsm")
Set Destination = Workbooks("B").Worksheets("Input").Range("B2:S2")

'check if name is entered properly
If A = "" Then
MsgBox ("Your name is not visible; please start from the Reference tab.")
Worksheets("Reference").Activate
Exit Sub
End If

X.Activate


For Each ws In X.Worksheets
  Set rng = ws.Range("A" & ws.Rows.Count).End(xlUp)
  If InStr(1, rng, A) = 0 Then
Else
    X.ActiveSheet.Range("$A$2:$DQ$11").AutoFilter Field:=1, Criteria1:=A
    Range("A7:CD7").Select
    Selection.Copy
    Destination.Activate
    Destination.PasteSpecial
End If
Next ws

Application.ScreenUpdating = False

End Sub

更新 : 我设法解决了之前的编译错误,并且似乎代码(应该?)工作。但是,它到了这一步:

X.Activate



...然后什么也没有发生。没有运行时错误或任何东西,但它似乎没有搜索文件(变量 X)或根据变量 A 的存在提取任何数据。有什么想法吗?

最佳答案

我要做的是遍历行并评估出现必要数据的列,然后避免复制/粘贴,只是使目标范围等于源范围:

Sub SearchNCopy()

Dim A As String 'The String you are searching for
Dim b As String ' the string where you shall be searching
Dim wbs, wbt As Workbook ' Declare your workbooks
Dim wss As Worksheet
Dim i, lrow As Integer

Set wbt = Workbooks("B.xlsm") 'Set your workbooks
Set wbs = Workbooks.Open("X.xlsm")


A = wbt.Worksheets("Summary").Range("A1").Value

If A = "" Then
    MsgBox ("Your name is not visible; please start from the Reference tab.")
    Worksheets("Reference").Activate
    Exit Sub
End If

For Each wss In wbs.Worksheets 'Loop through sheets

    lrow = wss.Cells(wss.Rows.Count, "A").End(xlUp).Row 'Find last used row in each sheet - MAKE SURE YOUR SHEETS DONT HAVE BLANKS BETWEEN ENTIRES

        For i = 1 To lrow Step 1 'Loop through the rows

            b = wss.Range("A" & i).Value 'Assign the value to the variable from column a of the row

                If Not InStr(1, b, A) = 0 Then 'Evaluate the value in the column a and if it contains the input string, do the following

                    wbt.Worksheets("Input").Range("B2:CC2") = wss.Range("A" & i & ":CD" & i) 'copies the range from one worksheet to another avoiding copy/paste (much faster)

                End If

            Next i

Next wss

End Sub

关于macos - 编译错误 : searching for one specific data point, 循环遍历整个工作簿,复制/粘贴数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32211077/

相关文章:

python - 无法使用openpyxl保存excel文件

excel - 下标与上标冲突

excel - 如何找到使用 VBA 匹配的范围内的最后一行单元格?

macos - XPC 服务和 AVFoundation

macos - 请输入提交消息来解释为什么需要进行此 merge ,特别是如果它将更新的上游 merge 到主题分支中

java - Mac OS 和 java.awt.Window.init(Window.java :497)) 处的 java.lang.NullPointerException

excel - 计算列中的连续数字

linux - 如果我安装了 SIGCHLD 处理程序,对特定 pid 的 *blocking* 等待是否仍然有效?

excel - 如何在Excel VBA中将日期格式更改为数字?

excel - 计算/汇总一列但排除#N/A