复制与条件匹配的可见行并乘以百分比,然后粘贴到另一张工作表的最后一行

Copy visible rows that matches criteria and multiply by percentage then paste to another sheet's last row

提问人:Stelios 提问时间:10/23/2023 最后编辑:JohnMStelios 更新时间:10/23/2023 访问量:49

问:

只有当我删除标准时,我才能成功执行上述主题。但是一旦我把它放回去,它就不会执行或什么都不做。以下是我的代码和标准。我想知道我将如何包含标准并保持此代码正常工作。我需要 A 列中与 DB2 工作表中的单元格 B2 匹配的所有行,然后复制到 DB3 工作表的最后一行。感谢任何帮助。

Sub VisibleRowsAndMultiplyByPercentage()
    Dim wsSource As Worksheet
    Dim wsDestination As Worksheet
    Dim lastRowSource As Long, lastRowDest As Long
    Dim criteria As Variant
    Dim i As Long, j As Long
    
    ' Set source and destination worksheets
    Set wsSource = ThisWorkbook.Sheets("DB2")
    Set wsDestination = ThisWorkbook.Sheets("DB3")
        
    ' Get the criteria value from cell B2 in destination sheet (Sheet5)
    criteria = wsSource.Range("B2").Value
      
    ' Find the last row in source sheet
    lastRowSource = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
    
    ' Find the last row in destination sheet column A
    lastRowDest = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1

    
    ' Loop through each row in column A of the source sheet
    For i = 3 To lastRowSource
        ' Check if the row is visible and column A matches the criteria (B2 value)
        If Not wsSource.Rows(i).Hidden And wsSource.Cells(i, 1).Value = criteria Then
            ' Copy values from column 1 to 10
            For j = 1 To 10
                wsDestination.Cells(lastRowDest, 1).Resize(, 10).Value = wsSource.Cells(i, 1).Resize(, 10).Value
            Next j
            ' Loop through columns 11 to 22 in the source row, multiply by percentage, and copy to destination sheet
            For j = 11 To 22
                ' Multiply the value by the corresponding percentage from row 1
                Dim multipliedValue As Double
                multipliedValue = wsSource.Cells(i, j).Value * wsSource.Cells(1, j).Value
                ' Copy the multiplied value to destination sheet
                wsDestination.Cells(lastRowDest, j).Value = multipliedValue
            Next j
            ' Move to the next row in destination sheet
            lastRowDest = lastRowDest + 1
        End If
    Next i
End Sub
Excel VBA 复制粘贴

评论

0赞 Notus_Panda 10/23/2023
你的没有声明,试试它声明了。此外,第一个 for 循环不是必需的(您可以一次分配完整的 10 列值),您可以在第二个循环中创建一个数组以一次粘贴所有值。除此之外,我无法立即看到任何会导致问题的内容。如果由于筛选器而隐藏了行,还可以将范围设置为仅循环访问非隐藏行(即数据集的大小)。criteriaSpecialCells(xlCellTypeVisible)
0赞 Stelios 10/23/2023
嗨,@Notus_Panda,您介意根据您的输入翻译我的代码吗?我只是对标准有问题,除此之外一切都很好
0赞 Notus_Panda 10/23/2023
我不确定你翻译是什么意思。你没有,这取决于值是字符串还是要比较的数字,并且很可能是导致你的代码像现在这样什么都不做的原因。至于值,一下子,是这样的:Dim criteria As StringwsDestination.Cells(lastRowDest, 1).Resize(,10).Value = wsSource.Cells(i, 1).Resize(,10).Value
0赞 Stelios 10/23/2023
@Notus_Panda,我根据您的输入更新了上面的代码,并得到了运行时错误“1004”:应用程序定义或对象定义的错误。我也只是尝试在没有调整大小代码的情况下声明条件,但它仍然什么也没做。
0赞 Notus_Panda 10/23/2023
我的意思是让那行代替循环,应该更清楚,对不起。for j = 1 To 10

答:

0赞 Notus_Panda 10/23/2023 #1

这是您的代码根据我在评论中提到的内容进行了调整:

Sub VisibleRowsAndMultiplyByPercentage()
    Dim wsSource As Worksheet
    Dim wsDestination As Worksheet
    Dim lastRowSource As Long, lastRowDest As Long
    Dim criteria As Variant
    Dim i As Long, j As Long
    
    ' Set source and destination worksheets
    Set wsSource = ThisWorkbook.Sheets("DB2")
    Set wsDestination = ThisWorkbook.Sheets("DB3")
        
    ' Get the criteria value from cell B2 in destination sheet (Sheet5)
    criteria = wsSource.Range("B2").Value
      
    ' Find the last row in source sheet
    lastRowSource = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
    
    ' Find the last row in destination sheet column A
    lastRowDest = wsDestination.Cells(wsDestination.Rows.Count, "A").End(xlUp).Row + 1

    Dim arrPrint
    Dim multipliedValue As Double
    ReDim arrPrint(1 To 12)
    ' Loop through each row in column A of the source sheet
    For i = 3 To lastRowSource
        ' Check if the row is visible and column A matches the criteria (B2 value)
        If Not wsSource.Rows(i).Hidden And wsSource.Cells(i, 1).Value = criteria Then
            ' Copy values from column 1 to 10
            wsDestination.Cells(lastRowDest, 1).Resize(, 10).Value = wsSource.Cells(i, 1).Resize(, 10).Value
            ' Loop through columns 11 to 22 in the source row, multiply by percentage, and copy to destination sheet
            For j = 11 To 22
                ' Multiply the value by the corresponding percentage from row 1
                arrPrint(j - 10) = wsSource.Cells(i, j).Value * wsSource.Cells(1, j).Value
            Next j
            ' Copy the multiplied value to destination sheet
            wsDestination.Cells(lastRowDest, 11).Resize(, UBound(arrPrint)).Value = arrPrint
            ' Move to the next row in destination sheet
            lastRowDest = lastRowDest + 1
        End If
    Next i
End Sub

用于防止未声明的变量(和其他此类问题)导致问题;请参阅如何在将来的所有代码中自动设置它。Option Explicit