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 :)
 
Try this, you can interchange the columns

Book1
ABCDE
1Customer NumberDateModel NumberSizeModel Color
2100-0112/01/2019SkySmallBlue
3100-02MediumNo color
4100-0312/01/2019C480000B9147-0009-08Large--Check--
5100-0412/11/2019LargeNo color
6100-05GroundYou don't tell me nothing
7100-0612/01/2019HillSmallYou don't tell me nothing
8100-0712/01/2019No color
9100-08SkyBlue
Sheet1


VBA Code:
Option Explicit
'by Hernan Torres (Mikel ERP)
'December 22, 2019
'Refer to Sheet1
'Question by VBAProIWish
'topic: https://www.mrexcel.com/board/threads/populate-one-cell-based-on-another.1118613/#post-5392628

Sub Populate_Dynamically()
Dim lastCol As Long
Dim colF, colT As Integer 'Column Find, Target
Dim colL As String 'Column Letter (know the last column letter)
Dim rangeF As Range 'range to find Column match
Dim b() As Variant
Dim r As Long

Application.ScreenUpdating = False
'Run ("check_dictionary") 'good idea, but only if you want it
Sheets("Sheet1").Activate
lastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set rangeF = Range("A1:" & Cells(1, lastCol).Address) 'I prefer set a range to will use in second argument of Match in next line
colF = Application.Match("Model Number", rangeF, 0)
colT = Application.Match("Model Color", rangeF, 0)
colL = Split(Cells(1, lastCol).Address, "$")(1)
b = Sheets("Sheet1").Range("A2:" & colL & Range(Split(Cells(1, colF).Address, "$")(1) & Rows.Count).End(xlUp).Row).Value2

For r = 1 To UBound(b)
Select Case b(r, colF) 'if you have several values to check, I would prefere use a dictionary
Case "Sky"
Cells(r + 1, colT) = "Blue"
Case IsNull(b(r, colF))
Cells(r + 1, colT) = "No color"
Case IsNumeric(b(r, colF))
Cells(r + 1, colT) = "Yellow"
Case "Ground"
Cells(r + 1, colT) = "You don't tell me nothing"
Case "Hill"
Cells(r + 1, colT) = "You don't tell me nothing"
Case Else   'you could avoid this else statement, building a dictionary
            'or put here cases like "C480000B9147-0009-08"
Cells(r + 1, colT) = "--Check--"
End Select
Next r
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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".

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.

I have doubt in the data that you put as an example: C480000B9147-0009-08.
Do you have it stored as a number in the cell? or is it stored as text?
The requirement would be any data that contains a number within the text?
 
Upvote 0
DanteAmor,

All I can say is WWwwoowwWW!

This is exactly what I was looking for!

Thank you so very much!

Wow, coding is so much fun.

Thanks again! :)
 
Upvote 0
Hernan, I just saw your code after I saw Dante's code and while Dante's code look a bit more user friendly for newbies like myself, I absolutely love the detailed explanations that you give after many of the lines. I will study this and know that I will learn from your "Laymen's Term" explanations.

Thank you so much!
 
Upvote 0
No problem, both codes are get same result, in my case you can apply one worksheet with key words (Dictionary) for add color, column names, that was my focus.
Here an full example with both suggestion
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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