VBA Excel - 将行复制到另一个带有条件的工作簿表

Posted

技术标签:

【中文标题】VBA Excel - 将行复制到另一个带有条件的工作簿表【英文标题】:VBA Excel - Copy Rows to Another Workbook Sheet with conditions 【发布时间】:2016-08-23 01:40:34 【问题描述】:

新手尝试在配置为提示登录并允许 diff Id 和 PW 查看不同工作表的 Excel 工作簿上混合和匹配代码。

If Me.userid.Value = "admin" And Me.userpw.Value = "admin" Then
MsgBox "Login Successful!", vbInformation, "Login Alert"
MsgBox "Entry and use data with caution and exercise confidentiality at all times!", vbExclamation, "Critical Reminder"

Unload Me

Sheets("Summary Report View").Visible = True
Sheets("Summary Report View").Select
Sheets("Data Validation").Visible = True
Sheets("Data Entry 1").Visible = True
Sheets("Data Entry 2").Visible = True
Sheets("Data Entry 3").Visible = True

我面临的挑战是无法将其他工作簿(称为 6-9months 的特定工作表)中的数据复制到我正在处理的此工作簿到数据条目 1 中。条件是获取具有名称的所有行列 I 中的“John”并粘贴到名为“数据条目 1”的活动工作表中。我试图通过单击按钮来激活代码以获取所有行,但它似乎不起作用。

Confirmation = MsgBox("Are you sure to removal all contents? This is not reversible", vbYesNo, "Confirmation")

    Select Case Confirmation
    Case Is = vbYes

    Sheets("Data Entry 2").Cells.ClearContents
    MsgBox "Information removed", vbInformation, "Information"

    Dim GCell As Range
    Dim Txt$, MyPath$, MyWB$, MySheet$
    Dim myValue As String
    Dim P As Integer, Q As Integer
    Txt = "John"

    MyPath = "C:\Users\gary.tham\Desktop\"
    MyWB = "Book1.xlsx"

    'MySheet = ActiveSheet.Name

    Application.ScreenUpdating = False

    Workbooks.Open Filename:=MyPath & MyWB
    lastrow = ActiveSheet.Range("A" & Rows.Count).End(x1Up).Row
    For i = 2 To lastrow

    If Cells(i, 11) = txt Then
    Range(Cells(i, 1), Cells(i, 13)).Select
    Selection.Copy
    P = Worksheets.Count
    For Q = 1 To P
    If ThisWorkbook.Worksheets(Q).Name = "Data Entry 2" Then
    Worksheets("Data Entry 2").Select
    ThisWorkbook.Worksheets(Q).Paste
    End If
    Next Q
    End If
    Next i

    Case Is = vbNo
    MsgBox "No Changes Made", vbInformation, "Information"

    End Select

【问题讨论】:

@Ralph,非常感谢您的留言,我很感激。请理解,这不是一个代码编写服务,因为它更像是一个在代码错误上互相帮助的社区。我已经更新了正在使用的代码(“我通过一系列网站和 youtube 运行”)...抱歉,我对 VBA 不太熟悉。 【参考方案1】:

您的代码的基本问题是您同时处理多个 Excel 文件 (1) 您正在打开并搜索“John”的文件和 (2) 从中调用宏的当前文件我们正在向其中导入数据。然而,您的代码并未引用这两个文件,而只是声明在 ActiveSheet 中搜索“john”。此外,您不会告诉 VBA 您要在两个文件中的哪个文件中搜索当前活动的工作表。

因此,如果您正在处理多个文件,那么您应该专门解决​​所有问题,并且不要要求 VBA 做出假设,即您指的是哪个文件或哪个工作表或哪个工作表上的哪个单元格。使困惑?如果 VBA 是一个人,那么他/她可能也会感到困惑。然而,VBA 只是做出假设,您会想知道为什么代码没有按照您的预期执行。因此,在处理多个文件时,您应该使用以下显式 (!) 引用并准确告诉 VBA 您想要什么:

Workbooks("Book1.xlsx").Worksheets("Sheet1").Cells(1, 1).Value2

Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Value2

话虽如此,我更改了您的代码以使用上述内容。

Option Explicit

Sub CopyDataFromAnotherFileIfSearchTextIsFound()

Dim strPath As String
Dim wbkImportFile As Workbook
Dim shtThisSheet As Worksheet
Dim shtImportSheet As Worksheet

Dim lngrow As Long
Dim strSearchString As String
Dim strImportFile As String

'uPPer or lOwEr cases do not matter (as it is currently setup)
strSearchString = "jOHn"
strImportFile = "Book1.xlsx"

Set shtThisSheet = ThisWorkbook.Worksheets("Data Entry 2")
'If the import file is in the same folder as the current file
'   then you could also use the following instead
'strPath = ThisWorkbook.Path
strPath = "C:\tmp" '"C:Users\gary.tham\Desktop"

With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

Set wbkImportFile = Workbooks.Open(Filename:=strPath & "\" & strImportFile)
'To speed up things you could also (if acceptable) open the file
'   read-only without updating links to other Excel files (if there are any):
'Set wbkImportFile = Workbooks.Open(Filename:=strPath & "\" & strImportFile, ReadOnly:=True, UpdateLinks:=False)
Set shtImportSheet = wbkImportFile.Worksheets("6-9months")

shtThisSheet.Cells.ClearContents
For lngrow = 2 To shtImportSheet.Cells(shtImportSheet.Rows.Count, "I").End(xlUp).Row
    If InStr(1, shtImportSheet.Cells(lngrow, "I").Value2, strSearchString, vbTextCompare) > 0 Then
        shtImportSheet.Range(shtImportSheet.Cells(lngrow, 1), shtImportSheet.Cells(lngrow, 13)).Copy
        shtThisSheet.Range("A" & shtThisSheet.Cells(shtThisSheet.Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteAll, xlPasteSpecialOperationNone
    End If
Next lngrow

wbkImportFile.Close SaveChanges:=False

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

End Sub

请注意,上面的代码不是您的精确副本。有两个变化:

(1)当前文件(您要导入的文件)中的“数据条目2”工作表将被清除,无需询问用户。

(2) 直接引用工作表“Data Entry 2”而不进行上述检查:当前文件中是否确实存在同名工作表。

所以,不要忘记进行适当的调整以满足您的需求。

如果此解决方案适合您或您还有其他问题,请告诉我。

【讨论】:

代码运行良好,我已经对其进行了修改以适应更多要求!谢谢!!我是否需要输入工作代码来更新问题以显示工作代码? 不,一切正常。您有一个问题并且有一个答案(您接受它作为适用的解决方案)。问题显示您遇到的问题,答案显示您的问题的解决方案。像这样,该帖子可能会在未来帮助其他人解决他们的问题。所以,一切都很好。但是谢谢你的提问。如果您再次需要帮助,请随时返回并随时阅读其他用户的问题。也许你可以帮助他们解决问题? 我会更经常地访问这个网站。就像你说的,也许我可以帮助别人。再次感谢@ralph

以上是关于VBA Excel - 将行复制到另一个带有条件的工作簿表的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA根据条件从一个工作表复制到其他工作表特定单元格

将行从一个链接表插入到另一个链接表

如何将EXCEL中符合条件的数据利用公式复制到另一个工作表

使用 Python pandas 根据条件将行值复制到另一列

在 Excel 中将某些特定单元格从一个工作表复制到另一个工作表的 VBA 可能是啥?

使用 INSERT 查询将行从一个表复制到另一个表 [关闭]