Populate one cell based on another

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I am looking for code to do the following...

1. Find the column (in row 1) named "Customer Number"
2. Find the column (in row 1) named "Model Number"
3. Find the column (in row 1) named "Model Color"
4. For every cell in the "Model Number" column named "Sky", populate the cell in the "Model Color" column (same row), "Blue".
5. For every cell in the "Model Number" column that is blank, populate the cell in the "Model Color" column (same row), "No Color".
6. For every cell in the "Model Number" column with any combination of ONLY numbers (whole numbers, integers), populate the cell in the "Model Color" column (same row), "Yellow".
7. There will be blanks in the "Model Number" column, but the search needs to stop at the last populated cell in the "Customer Number" column, where there are no blanks.


Putting it more simply...
If the cell in the "Model Number" column says "Sky", make the cell in the "Model Color" column, "Blue", etc...

Please note the following...
1. Everything is based on row 2, downward. (Row 1 is for Column Headers).

2. Due to my columns never being in the same place, I would like this to find columns by name and not by column position.

3. This needs to work on whatever number format the "Model Number" column is currently in. Text, numbers, accounting, etc.
My code below works fine with text, but not on long numbers like this C480000B9147-0009-08.
It also isn't programmed by column header name.

4. The code needs to be cAsE INsENTivE. Sometimes in my lines of code, "Sky" for example, works, but "SKY" or "sky" doesn't work.


This code only works by column position and doesn't work if there are long numbers in column D
VBA Code:
Sub Populate_Dynamically
Dim lastRow As Long, icell As Long
lastRow = Range("C" & Rows.Count).End(xlUp).Row
For icell = 1 To lastRow
If Range("D" & icell).Value = "Sky" Then
Range("E" & icell).Value = "Blue"
End If
Next icell
End Sub

Thanks much to anyone that can help me with this :)
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Ok, I will try to do that. In the meantime, is there any way that you can get my existing code to work, even when there are large numbers instead of text?

Thanks
 
Upvote 0
Yeah, I already tried that and it didn't work. XL2BB works great! See below...

Book1
ABCDEFGHIJK
1BEFORE MACROAFTER MACRO
2Customer NumberDateModel NumberSizeModel ColorCustomer NumberDateModel NumberSizeModel Color
3100-0112/1/2019SkySmall100-0112/1/2019SkySmallBlue
4100-02Medium100-02MediumNo Color
5100-0312/1/2019C480000B9147-0009-08Large100-0312/1/2019C480000B9147-0009-08LargeYellow
6100-0412/11/2019Large100-0412/11/2019LargeNo Color
7100-05Ground100-05Ground
8100-0612/1/2019HillSmall100-0612/1/2019HillSmall
9100-0712/1/2019100-0712/1/2019No Color
10100-08Sky100-08SkyBlue
(7)


Sorry if you saw my first picture, I forgot to delete the values in the "before" side!
 
Upvote 0
Now, I'm in my cellphone, after dinner I will check your example.
But, Which is the difference between two ranges (Before and After macro)?
 
Upvote 0
Ahh, I guess I didn't make the changes fast enough! Sorry about that! I edited my original post to take the values away in the "before" section.

Thanks!
 
Upvote 0
I should have mentioned that I want the cells to have the words "Blue", "Yellow" and "No Color" as shown in my picture above and not color the cell. I just realized that it might be confusing.

Thanks to anyone that can help me figure this toughie out! :)
 
Upvote 0
Try this. I hope this is what you need.

VBA Code:
Sub Populate_Cells()
  Dim c1 As Range, c2 As Range, c3 As Range, lr As Long, lc As Long, sh As Worksheet
 
  Application.ScreenUpdating = False
 
  Set sh = ActiveSheet
  Set c1 = sh.Rows(1).Find("Customer Number", , xlValues, xlWhole)
  Set c2 = sh.Rows(1).Find("Model Number", , xlValues, xlWhole)
  Set c3 = sh.Rows(1).Find("Model Color", , xlValues, xlWhole)
 
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  If Not c1 Is Nothing And Not c2 Is Nothing And Not c3 Is Nothing Then
    lr = sh.Cells(Rows.Count, c1.Column).End(xlUp).Row
    lc = sh.Cells(1, Columns.Count).End(xlToLeft).Column
    sh.Range("A1", sh.Cells(lr, lc)).AutoFilter Field:=c2.Column, Criteria1:="<>"
    sh.AutoFilter.Range.Range(sh.Cells(2, c3.Column), sh.Cells(lr, c3.Column)).SpecialCells(xlCellTypeVisible).Value = "Yellow"
    sh.Range("A1", sh.Cells(lr, lc)).AutoFilter Field:=c2.Column, Criteria1:="Sky"
    sh.AutoFilter.Range.Range(sh.Cells(2, c3.Column), sh.Cells(lr, c3.Column)).SpecialCells(xlCellTypeVisible).Value = "Blue"
    sh.Range("A1", sh.Cells(lr, lc)).AutoFilter Field:=c2.Column, Criteria1:="="
    sh.AutoFilter.Range.Range(sh.Cells(2, c3.Column), sh.Cells(lr, c3.Column)).SpecialCells(xlCellTypeVisible).Value = "No color"
    sh.ShowAllData
  Else
    If c1 Is Nothing Then MsgBox "Model color column does not exists"
    If c2 Is Nothing Then MsgBox "Model Number column does not exists"
    If c3 Is Nothing Then MsgBox "Customer Number column does not exists"
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top