使用VBA中的嵌套if语句将列的单元格从工作表复制到另一个工作表

时间:2015-12-03 14:36:50

标签: vba excel-vba excel

我想在VBA中编写一个代码如下:

  1. 在工作表中" Calculation_Results"从B列获取每个单元但不是B1
  2. 如果等于Worksheets("Parameters").Range("A3").Value,则将Worksheets("SQLresults").Range("A").Value的值放入Worksheets("Calculation_Results").Range("A").Cells
  3. 如果等于Worksheets("Parameters").Range("A4").Value那么 将0.03* Worksheets("SQLresults").Range("B3").Value的值放在Worksheets("Calculation_Results").Range("A").Cells
  4. 其他Worksheets("Calculation_Results").Range("A").Cells = 0.03 * Worksheets("SQLresults").Range("B3").Value
  5. 好的,我写了以下内容,但它不起作用

    ' calculate in Excel the shock
     With ThisWorkbook
       If Len(Sheets("Calculation_Results").name) = 0 Then Else Sheets("Calculation_Results").Delete
         Dim wss As Worksheet
         ' With ThisWorkbook
            Set wss = .Sheets.Add(After:=.Sheets("SQLresults"))
             wss.name = "Calculation_Results"
             '
    ' local_calculation Macro
    '
    
    '
    ' With Sheets("Calculation_Results")
         For Each element In Worksheets("SQLresults").Range("B")
           If element.Value = Worksheets("Parameters").Range("A3").Value _
           Then Worksheets("Calculation_Results").Range("A").Cells = Worksheets("SQLresults").Range("A").Value_
                  End If
                  Else If element.Value = Worksheets("Parameters").Range("A4").Value_
                  Then Worksheets("Calculation_Results").Range("A").Cells = 0.03* Worksheets("SQLresults").Range("B3").Value
                  End If
                  Else
                  Worksheets("Calculation_Results").Range("A").Cells = 0.25 * Worksheets("SQLresults").Range("B3").Value
         Next
          End With
    
    抱歉,这很容易,但我在VBA中相当新!第一部分,创建一个新工作表,只是第二部分工作得不好:(我无法理解错误信息。

4 个答案:

答案 0 :(得分:2)

  1. 对所有工作表进行质量验证,以便于阅读和使用
  2. 定义SQL表中最后使用的行,以便了解要检查的实际行数(Range("B")是VBA中的无效语法,您需要定义列和行...但是Columns("B")Columns(2)是有效的语法)。
  3. 遍历每一行并与您一起评估标准,以确定计算结果中每个对应行的值。
  4. 在此示例中使用Select Case可能更容易理解,而不是If Then Else
  5. 请参阅此代码:

    Dim wb As Workbook
    Dim wCalc As Worksheet
    Dim wSQL As Worksheet
    Dim wP As Worksheet
    
    Set wb = ThisWorkbook
    Set wSQL = wb.Sheets("SQLResults")
    Set wP = wb.Sheets("Parameters")
    
    With wb
    
        If Len(.Sheets("Calculation_Results").Name) = 0 Then Else .Sheets("Calculation_Results").Delete
    
        Set wCalc = .Sheets.Add(After:=wSQL)
        wCalc.Name = "Calculation_Results"
    
        ' local_calculation Macro
    End With
    
    With wSQL
    
        Dim ERow As Long
        ERow = .Range("B" & .Rows.Count).End(xlUp).Row
    
        Dim element As Range
    
        For Each element In .Range("B2:B" & ERow)
    
            Select Case element.Value2
    
                Case Is = wP.Range("A3").Value2: wCalc.Cells(element.Row, 1) = element.Value2
                Case Is = wP.Range("A4").Value2: wCalc.Cells(element.Row, 1) = element.Value2 * 0.03
                Case Else: wsCalc.Cells(element.Row, 1) = element.Value2 * 0.25
    
            End Select
    
          Next
    
    End With
    

    作为替代方案,您可以完全摆脱循环,只需应用公式。

    重置“Calculation_Result”表后,在上面的代码中替换它。这是未经测试,可能需要一些调整。

    With wSQL
    
        Dim ERow As Long
        ERow = .Range("B" & .Rows.Count).End(xlUp).Row
    
    End With
    
    With wCalc
    
        With .Range("A2")
            .Formula = "=IF(SQLResults!B2=Parameters!$A$3,SQLResults!B2,IF(SQLResults!B2=Parameters!$A$4,SQLResults!B2*.03,SQLResults!B2*.25))"
            .Copy
    
            With .Resize(ERow, 1)
                .PasteSpecial
                .Copy
                .PasteSpecial xlPasteValues
            End With
    
        End With
    
    End With
    

答案 1 :(得分:1)

你的剧本非常混乱。

尝试使用以下内容,我刚刚尝试将脚本连接起来。

' calculate in Excel the shock
With ThisWorkbook
    If Len(Sheets("Calculation_Results").Name) = 0 Then Else: Sheets("Calculation_Results").Delete

    Dim wss As Worksheet

    ' With ThisWorkbook
    Set wss = .Sheets.Add(After:=.Sheets("SQLresults"))
    wss.Name = "Calculation_Results"
    '
    ' local_calculation Macro
    '

    '
    '

    With Sheets("Calculation_Results")
    For Each element In Worksheets("SQLresults").Range("B")

        If element.Value = Worksheets("Parameters").Range("A3").Value Then

            Worksheets("Calculation_Results").Range("A").Cells = Worksheets("SQLresults").Range("A").Value

        ElseIf element.Value = Worksheets("Parameters").Range("A4").Value Then

            Worksheets("Calculation_Results").Range("A").Cells = 0.03 * Worksheets("SQLresults").Range("B3").Value

        Else

            Worksheets("Calculation_Results").Range("A").Cells = 0.25 * Worksheets("SQLresults").Range("B3").Value

        End If
    Next
    End With 
End With

答案 2 :(得分:1)

{{1}}

范围(“B”)未定义

使用范围(“B2:B”& rows.count)完成指定的标准

答案 3 :(得分:0)

' calculate in Excel the shock
With ThisWorkbook
    If Len(Sheets("Calculation_Results").name) = 0 Then Else: Sheets("Calculation_Results").Delete

    Dim wss As Worksheet

    ' With ThisWorkbook
    Set wss = .Sheets.Add(After:=.Sheets("SQLresults"))
    wss.name = "Calculation_Results"
    '
    ' local_calculation Macro
    '

    '
    '

    i = 2
    With Sheets("Calculation_Results")

   Dim ERow As Long
ERow = Worksheets("SQLresults").Range("B" & .Rows.Count).End(xlUp).Row
For Each element In .Range("B2:B" & ERow)
        If element.Value = Worksheets("Parameters").Range("A3").Value Then
            Worksheets("Calculation_Results").Range("A" & i) = Worksheets("SQLresults").Range("A" & i).Value
        ElseIf element.Value = Worksheets("Parameters").Range("A4").Value Then
            Worksheets("Calculation_Results").Range("A" & i) = 0.03 * Worksheets("SQLresults").Range("A" & i).Value
        Else
            Worksheets("Calculation_Results").Range("A" & i) = 0.25 * Worksheets("SQLresults").Range("A" & i).Value
        End If
        i = i + 1
    Next
    End With
End With