VBA:根据

时间:2016-03-07 23:29:44

标签: excel vba excel-vba macros

我会尝试解释我的问题。 我需要根据单元格(列)中的四个值之一来填充值。 我有一个代码

  Sub testmacro()


    If Sheets("List1").Range("A1") = "Keyword1" Then
Sheets("List1").Range("b1") = "60"
Sheets("List1").Range("c1") = "630"
Sheets("List1").Range("d1") = "0.7"
Sheets("List1").Range("e1") = "0.7"
        ElseIf Sheets("List1").Range("A1") = "Keyword2" Then
Sheets("List1").Range("b1") = "1500"
Sheets("List1").Range("c1") = "15750"
Sheets("List1").Range("d1") = "1.46"
Sheets("List1").Range("e1") = "1"
        ElseIf Sheets("List1").Range("A1") = "Keyword3" Then
Sheets("List1").Range("b1") = "1500"
Sheets("List1").Range("c1") = "15750"
Sheets("List1").Range("d1") = "2.98"
Sheets("List1").Range("e1") = "1"
        ElseIf Sheets("List1").Range("A1") = "Keyword4" Then
Sheets("List1").Range("b1") = "1500"
Sheets("List1").Range("c1") = "15750"
Sheets("List1").Range("d1") = "2.38"
Sheets("List1").Range("e1") = "1"    
    End If

    If Sheets("List1").Range("A2") = "Keyword1" Then
Sheets("List1").Range("b2") = "60"
Sheets("List1").Range("c2") = "630"
Sheets("List1").Range("d2") = "0.7"
Sheets("List1").Range("e2") = "0.7"
        ElseIf Sheets("List1").Range("A2") = "Keyword2" Then
Sheets("List1").Range("b2") = "1500"
Sheets("List1").Range("c2") = "15750"
Sheets("List1").Range("d2") = "1.46"
Sheets("List1").Range("e2") = "1"
        ElseIf Sheets("List1").Range("A2") = "Keyword3" Then
Sheets("List1").Range("b2") = "1500"
Sheets("List1").Range("c2") = "15750"
Sheets("List1").Range("d2") = "2.98"
Sheets("List1").Range("e2") = "1"
        ElseIf Sheets("List1").Range("A2") = "Keyword4" Then
Sheets("List1").Range("b2") = "1500"
Sheets("List1").Range("c2") = "15750"
Sheets("List1").Range("d2") = "2.38"
Sheets("List1").Range("e2") = "1"    
    End If

。 。 。等等 。 。 。 正如您在此代码中看到的那样,只有2行可以工作,但如果我需要10000行呢?没有办法像这样写。我需要一些可以在A列中查看的内容,注意关键字然后用正确的值填充行的其余部分。 谢谢您的帮助! (是的,我对VBA很新)

2 个答案:

答案 0 :(得分:2)

您可以使用With语句来组织内容,从而简化操作。还要添加Select Case语句以清除If - Elseif - End If表示法。然后添加For循环以便于输入。循环时,请注意您可以使用.Cells notatation而不是.Range循环遍历列和行(例如Sheets(1).Range("B5")Sheets(1).Cells(5, 2)

相同

例如:

For row = 1 to 2
    With Sheets("List1")
        Select Case .Cells(row,1)
            Case "Keyword1"
                .Cells(row,2)  = "60"
                .Cells(row,3)  = "630"
                .Cells(row,4)  = "0.7"
                .Cells(row,5)  = "0.7"
            Case "Keyword2"
                .Cells(row,2) = "1500"
                .Cells(row,3) = "15750"
                .Cells(row,4) = "1.46"
                .Cells(row,5) = "1"
            Case "Keyword3"
                .Cells(row,2) = "1500"
                .Cells(row,3) = "15750"
                .Cells(row,4) = "2.98"
                .Cells(row,5) = "1"
            Case "Keyword4"
                .Cells(row,2) = "1500"
                .Cells(row,3) = "15750"
                .Cells(row,4) = "2.38"
                .Cells(row,5) = "1"     
        end select
    end with
next col

要为任意数量的行执行此操作,只需将For行更改为以下内容:

For row = 1 to 10000 ' if you know the exact amount or...

numRows = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets("List1").Columns(1))
For row = 1 to numRows
     'do the rest

答案 1 :(得分:2)

此代码应该完全符合您的要求。请更改顶部的关键字以满足您的要求。

Sub GenerateRandomContent()
Dim Keywords(1 To 4) As Variant
Dim RowNo As Long
RowNo = 1

'**************************************************
'*************** EDIT KEYWORDS HERE ***************
'**************************************************

            Keywords(1) = "Keyword1"
            Keywords(2) = "Keyword2"
            Keywords(3) = "Keyword3"
            Keywords(4) = "Keyword4"

'**************************************************
'**************************************************
'**************************************************

    With ThisWorkbook.Sheets("List1")

        Do While .Cells(RowNo, 1) <> ""

            If .Cells(RowNo, 1) = Keywords(1) Then
                .Cells(RowNo, 2) = "60"
                .Cells(RowNo, 3) = "630"
                .Cells(RowNo, 4) = "0.7"
                .Cells(RowNo, 5) = "0.7"
            ElseIf .Cells(RowNo, 1) = Keywords(2) Then
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "1.46"
                .Cells(RowNo, 5) = "1"
            ElseIf .Cells(RowNo, 1) = Keywords(3) Then
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "2.98"
                .Cells(RowNo, 5) = "1"
            ElseIf .Cells(RowNo, 1) = Keywords(4) Then
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "2.38"
                .Cells(RowNo, 5) = "1"
            End If
            RowNo = RowNo + 1
        Loop

    End With

End Sub

您也可以使用Case Select代替If

Sub GenerateRandomContent()
Dim Keywords(1 To 4) As Variant
Dim RowNo As Long
RowNo = 1

'**************************************************
'*************** EDIT KEYWORDS HERE ***************
'**************************************************

            Keywords(1) = "Keyword1"
            Keywords(2) = "Keyword2"
            Keywords(3) = "Keyword3"
            Keywords(4) = "Keyword4"

'**************************************************
'**************************************************
'**************************************************

    With ThisWorkbook.Sheets("List1")

        Do While .Cells(RowNo, 1) <> ""

            Select Case .Cells(RowNo, 1)
            Case Keywords(1)
                .Cells(RowNo, 2) = "60"
                .Cells(RowNo, 3) = "630"
                .Cells(RowNo, 4) = "0.7"
                .Cells(RowNo, 5) = "0.7"
            Case Keywords(2)
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "1.46"
                .Cells(RowNo, 5) = "1"
            Case Keywords(3)
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "2.98"
                .Cells(RowNo, 5) = "1"
            Case Keywords(4)
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "2.38"
                .Cells(RowNo, 5) = "1"
            End Select

            RowNo = RowNo + 1

        Loop

    End With

End Sub

甚至更简单

Sub GenerateRandomContent()
Dim RowNo As Long
RowNo = 1

    With ThisWorkbook.Sheets("List1")

        Do While .Cells(RowNo, 1) <> ""

            Select Case .Cells(RowNo, 1)
            Case "Keyword1"
                .Cells(RowNo, 2) = "60"
                .Cells(RowNo, 3) = "630"
                .Cells(RowNo, 4) = "0.7"
                .Cells(RowNo, 5) = "0.7"
            Case "Keyword2"
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "1.46"
                .Cells(RowNo, 5) = "1"
            Case "Keyword3"
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "2.98"
                .Cells(RowNo, 5) = "1"
            Case "Keyword4"
                .Cells(RowNo, 2) = "1500"
                .Cells(RowNo, 3) = "15750"
                .Cells(RowNo, 4) = "2.38"
                .Cells(RowNo, 5) = "1"
            End Select

            RowNo = RowNo + 1

        Loop

    End With

End Sub