【问题标题】:Copy and Paste Values Using Multiple Criterias VBA Excel使用多个标准 VBA Excel 复制和粘贴值
【发布时间】:2021-11-27 20:56:03
【问题描述】:

我很难弄清楚如何使用两个条件进行复制和粘贴。

基本上我有两列这样的:

Currency (Column M) Dollar Amount (Column N)
CAD -5
CAD -10
USD 10
USD -20

现在,我需要将负值复制并粘贴到两个不同的列中:

CAD (row = lastrow + 2, column = K) USD (row = lastrow + 2, column = L)
-5 -20
-10

这是我目前拥有的代码,但它在每个值之间添加了一个空格 = i:

lastRow = wsA.Cells(Rows.Count, "M").End(xlUp).Row

For i = 3 To lastRow
    CurrencyValue = Cells(i, 13).Value
    NumberValue = Cells(i, 14).Value
    If CurrencyValue = "CAD" And NumberValue < -0.05 Then
        Range("N" & i).Copy Range("K" & lastRow + i)
    ElseIf CurrencyValue = "USD" And NumberValue < -0.05 Then
        Range("N" & i).Copy Range("L" & lastRow + i)
    End If
Next i

【问题讨论】:

    标签: excel vba for-loop copy


    【解决方案1】:

    也许像下面这样的东西,可以进一步优化。

    With wsA
       For i = 3 to lastRow
           CurrencyValue = .Cells(i, 13).Value 'note the period
           NumberValue = .Cells(i, 14).Value 'note the period
    
           If CurrencyValue = "CAD" And NumberValue < -0.05 Then
               .Range("K" & .Rows.Count).End(xlUp).Offset(1).Value = CurrencyValue
           ElseIf CurrencyValue = "USD" And NumberValue < -0.05 Then
               .Range("N" & .Rows.Count).End(xlUp).Offset(1).Value = CurrencyValue
           End If
        Next
    End With
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多