2016-03-07 37 views
2

中的「隨機」內容填充單元格我將嘗試解釋我的問題。 我需要根據單元格(列)中的四個值中的一個填充值。 我有一個代碼VBA:根據列

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)

+3

你試過[循環](https://msdn.microsoft.com/en -us /圖書館/辦公室/ aa221353(v = office.11​​)的.aspx)? –

+0

謝謝你的線索。我還沒有嘗試過。我剛接觸VBA,不知道該找什麼,所以任何提示都非常受歡迎。 – loqan

+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 
+0

同樣你的代碼也需要用戶手動輸入'lastrow'數字。 「Do While」或「Do Until」循環更有效,並且需要用戶輸入更少的信息。 –

2

此代碼應該做的正是你所尋找的,請在頂部更改關鍵字來適應你。 r要求。

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 
+0

在這種情況下,確實不需要定義關鍵字數組,只是爲了簡化代碼。但它也起作用。 – teepee

+1

@TeePee它真的只是所以用戶可以修改代碼......因爲它們似乎是從VBA開始的,給予額外的靈活性有時可以提供幫助。 –