【问题标题】:How to copy paste data based on some criterias from one workbook to another(specific cells) using VBA?如何使用 VBA 根据某些标准将粘贴数据从一个工作簿复制到另一个(特定单元格)?
【发布时间】:2020-08-03 15:06:51
【问题描述】:

我编写了以下代码,用于将数据从一个工作簿复制到另一个工作簿中的特定单元格(我认为这是一个挑战,目标文件有月份和下面的相关数据,每个月我都需要将数据复制到当前月份列,这就是为什么使用“最后一列”功能不覆盖历史月份也使其动态地转到没有当前月份数据的最后一列)。即使代码运行良好,我也想对其进行优化以便轻松调试并避免将来出现问题;本年度发生了变化。 你有什么想法可以让这段代码变得更好

代码

Dim  x, LastRow, LastColumn, workfile, sourcefile As String
 
 sourcefile = ActiveWorkbook.Name
 workfile = ThisWorkbook.Name


LastRow = Range("A" & Rows.Count).End(xlUp).Row
For x = LastRow To 1 Step -1
If Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 1).Value = "001B" And Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 2).Value = "GBP" Then
    Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 8).Copy
    Workbooks(workfile).Worksheets("A").Activate
    Lastcolumn2 = Workbooks(workfile).Worksheets("A").Cells(28, 21).End(xlToLeft).Column + 1 
    Workbooks(workfile).Worksheets("A").Cells(28, Lastcolumn2).PasteSpecial xlPasteValues
Else

End If

If Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 1).Value = "001R" And Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 2).Value = "GBP" Then
    Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 8).Copy
    Workbooks(workfile).Worksheets("A").Activate
    Lastcolumn3 = Workbooks(workfile).Worksheets("A").Cells(29, 21).End(xlToLeft).Column + 1
    Workbooks(workfile).Worksheets("A").Cells(29, Lastcolumn3).PasteSpecial xlPasteValues
Else
End If

If Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 1).Value = "001B" And Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 2).Value = "EUR" Then
    Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 8).Copy
    Workbooks(workfile).Worksheets("A").Activate
    Lastcolumn4 = Workbooks(workfile).Worksheets("A").Cells(35, 21).End(xlToLeft).Column + 1
    Workbooks(workfile).Worksheets("A").Cells(35, Lastcolumn4).PasteSpecial xlPasteValues
    Else
    End If
    
If Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 1).Value = "001R" And Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 2).Value = "EUR" Then
    Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 8).Copy
    Workbooks(workfile).Worksheets("A").Activate
    Lastcolumn5 = Workbooks(workfile).Worksheets("A").Cells(36, 21).End(xlToLeft).Column + 1
    Workbooks(workfile).Worksheets("A").Cells(36, Lastcolumn5).PasteSpecial xlPasteValues
    Else
    End If
    Next

【问题讨论】:

标签: excel vba loops if-statement copy-paste


【解决方案1】:

所以这是“工作文件”的屏幕截图,我需要每个月在相关月份列下复制数据。变化区域的影响率只是公式和计算。所以复制的是在那之前:目前从 8 月开始的空列,因为我已经在 7 月运行了它,正如我所说的那样它可以工作,但代码似乎太复杂,很难为其他人调试

【讨论】:

    【解决方案2】:

    您需要做的就是确定每个条件的目标行是什么,然后将该值插入您要执行的代码块中。这样,您就可以避免多次重复相同的代码。

    你可以这样做:

    Dim  x, LastRow, LastColumn, workfile, sourcefile, exchangedownload1, exchangedownload2 As String
    Dim targetRow As Integer
    
    sourcefile = ActiveWorkbook.Name
    workfile = ThisWorkbook.Name
    
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    For x = LastRow To 1 Step -1
    
        ' store the values you are wanting to examine in these 2 variables
        exchangedownload1 = Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 1).Value
        exchangedownload2 = Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 2).Value
    
        ' determine the value for targetRow in this Case statement
        Select Case exchangedownload2
            Case Is "GBP"
                If exchangedownload1 = "001B" Then
                    targetRow = 28
                ElseIf enchangedownload1 = "001R" Then
                    targetRow = 29
            Case Is "EUR"
                If exchangedownload1 = "001B" Then
                    targetRow = 35
                ElseIf enchangedownload1 = "001R" Then
                    targetRow = 36
        End Select
    
        ' this is your code block that was being repeated with just a 
        ' different value for your targetRow, so just plug the value for 
        ' targetRow where it belongs and you only have to have this code block once
        Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 8).Copy
        Workbooks(workfile).Worksheets("A").Activate
        Lastcolumn2 = Workbooks(workfile).Worksheets("A").Cells(targetRow, 21).End(xlToLeft).Column + 1 
        Workbooks(workfile).Worksheets("A").Cells(targetRow, Lastcolumn2).PasteSpecial xlPasteValues
    
    Next
    

    【讨论】:

    • ' 确定此 Case 语句中 targetRow 的值 Select Case xrate2 Case Is = GBP If xrate1 = "001B" Then targetrow = 28 ElseIf xrate1 = "001R" Then targetrow = 29 Select Case xrate2 Case Is = EUR If xrate1 = "001B" Then targetrow = 35 ElseIf xrate1 = "001R" Then targetrow = 36 End Select "This part didn't work out as it says Compile error: End select without Select Case.
    • @NigarHuseynzade 我不明白你的评论,你想表达什么?您对我的回答有疑问还是在实施时遇到问题?
    • 对不起,也许我不清楚,我在实现上面你写的代码时遇到了问题,它显示错误,我做了一些修改,但仍然没有工作。
    【解决方案3】:
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    For x = LastRow To 1 Step -1
        ' store the values you are wanting to examine in these 2 variables
        xrate1 = Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 1).Value
        xrate2 = Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 2).Value
    
        ' determine the value for targetRow in this Case statement
        Select Case xrate2
            Case "GBP"
        Select Case xrate1
            Case "001B": targetrow = 28
        Case Else: targetrow = 29
        End Select
        Select Case xrate2
            Case "EUR"
        Select Case xrate1
            Case "001B": targetrow = 35
         Case Else: targetrow = 36
        End Select
    
        ' copying data
        Workbooks(sourcefile).Worksheets("exchangedownload").Cells(x, 8).Copy
        Workbooks(workfile).Worksheets("A").Activate
        Lastcolumn2 = Workbooks(workfile).Worksheets("A").Cells(targetrow, 21).End(xlToLeft).Column + 1
        Workbooks(workfile).Worksheets("A").Cells(targetrow, Lastcolumn2).PasteSpecial xlPasteValues
    Next
    

    以上是调整后的,因为我不能将 If 语句与 Case 一起使用,Case 是 If, Elseif 的替换。但是仍然收到编译错误,说它是“Next without For”和 End Select without Case statement((

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多