如何使用Select Case插入数据行

时间:2019-04-12 17:20:26

标签: excel vba

当我尝试将数据从Sheet1插入Sheet2时,我的VBA脚本错误。脚本代码仅提供“ Case 2-3”行编号,第一个“ Case”没有输入到工作表2中。想知道VBA脚本中还应包含哪些内容才能完成流程?

我的VBA脚本:

Sub CopyFromSheet1()
    Dim i As Long
    For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F

  Select Case CStr(Sheet1.Cells(i, 3).Value) ' Looks at the Value in Column C

    Case "Due From"
        Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
    Case "TOTAL1"
        Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
    Case "TOTAL2"
        Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value

    End Select


Next i


End Sub

1 个答案:

答案 0 :(得分:0)

这是怎么回事:您所做的一切,只是针对C列。然后针对D列再次进行吗?

Sub CopyFromSheet1()
    Dim i As Long
    Dim col as Long

    for col = 3 to 4
        For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F

            Select Case CStr(Sheet1.Cells(i, col).Value) 

                Case "Due From"
                    Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
                Case "TOTAL1"
                    Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
                Case "TOTAL2"
                    Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value

            End Select


        Next i

    next col
End Sub