简体   繁体   中英

Comparing text from one column to determine the output of another column

Okay. This is what I have in 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                                           

And this is the code:

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

My question is why is the above code not working?

First a Select Case block makes more sense than a series of ElseIf statements. As to why it's "Not working" with no error, two possible problems:

1) you aren't accessing the values of the cell objects. It should be implicit but specifying it should help.

2) you aren't handling the case where the cell's value doesn't match any of the listed text. Adding a final Else case should handle that possibility. If that is true for all of cells in rng then look into their contents more. Perhaps there is leading or trailing white space that will need to be removed.

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

You need to make sure your range is grabbing everything because as you mention, your code works for smaller amounts of data (10 rows). Use the following to display a message box which informs you of total rows in your range selection. Make sure it is checking the correct amount of rows:

    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

Copy from previous sheet to new sheet:

Sheets("Name of Sheet").Range(Range of Cells).Copy
Sheets("Destination Sheet").Range(Where you want it).PasteSpecial Paste:=xlPasteValues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM