[英]Comparing text from one column to determine the output of another column
好的。 這是我在A1:A100欄中的內容:
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
12V Automotive Products
A/V Cables
Accessories
Accessories
Accessories
Accessories
Accessories
Accessories
Accessories
Accessories
Accessories
Accessories
Accessories
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action
Action & Adventure
Action & Adventure
Adapters
Adapters
Adapters
Adapters
Adapters & Splitters
Adapters & Splitters
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
Adventure
這是代碼:
Sub FillColumnB()
Dim rng As Range, cl As Range
Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
For Each cl In rng
If cl = "12V Automotive Products" Then
cl.Offset(0, 1) = "tdexjxr"
ElseIf cl = "Accessories" Then
cl.Offset(0, 1) = "s6ii"
ElseIf cl = "Action" Then
cl.Offset(0, 1) = "7ks57k5k"
ElseIf cl = "Action & Adventure" Then
cl.Offset(0, 1) = "kxee5xskex"
ElseIf cl = "Adapters" Then
cl.Offset(0, 1) = "kxykk5ezw"
ElseIf cl = "Adobe Titles" Then
cl.Offset(0, 1) = "kz46yk78"
ElseIf cl = "Adventure" Then
cl.Offset(0, 1) = "l8rrzlez"
ElseIf cl = "All Toys" Then
cl.Offset(0, 1) = "ezlllels6"
ElseIf cl = "Animation" Then
cl.Offset(0, 1) = "988l7889l"
ElseIf cl = "Anti-Virus/Anti-Spyware" Then
cl.Offset(0, 1) = "wq3w"
ElseIf cl = "Applications" Then
cl.Offset(0, 1) = "jrd5j"
ElseIf cl = "Arcade" Then
cl.Offset(0, 1) = "drj76j"
ElseIf cl = "Arts & Humanities" Then
cl.Offset(0, 1) = "8l"
End If
Next
End Sub
我的問題是為什么上面的代碼不起作用?
首先, Select Case
塊比一系列ElseIf
語句更有意義。 至於為什么它“沒有工作”且沒有錯誤,可能有兩個問題:
1)您沒有訪問單元格對象的值。 它應該是隱式的,但指定它應該會有所幫助。
2)您不處理單元格的值與任何列出的文本都不匹配的情況。 添加最終的Else
案例應該可以解決這種可能性。 如果對rng
所有單元格都是如此, rng
進一步研究其內容。 可能存在需要刪除的前導或尾隨空白。
For Each cl In rng.cells
Dim outCell as Range
Set outCell = cl.offset(0,1)
Select Case cl.value
Case "12V Automotive Products"
outCell.value = "s6ii"
Case "Action"
outCell.value = "7ks57k5k"
'Case ...
' outCell.value = ...
Case Else
outCell.value = "Not Recognized Value"
End Select
Next cl
您需要確保您的范圍能夠捕獲所有內容,因為正如您提到的那樣,您的代碼適用於少量數據(10行)。 使用以下內容顯示一個消息框,通知您范圍選擇中的總行。 確保它正在檢查正確的行數:
Dim rng As Range, cl As Range
Dim rngCheck As Integer
Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
rngCheck = Range("A" & Rows.Count).End(xlUp).Row
MsgBox rngCheck
從上一張紙復制到新紙:
Sheets("Name of Sheet").Range(Range of Cells).Copy
Sheets("Destination Sheet").Range(Where you want it).PasteSpecial Paste:=xlPasteValues
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.