Set a value for every cell in a range

kwagner1

Active Member
Joined
Jun 10, 2003
Messages
445
Greetings,

i'm looking for a way to set a columns value to "US". I only want to populate the records that are on my worksheet. So, if i get a file that has 1000 rows on it, then populate every row with the "US" value in the Location column. If tomorrows file has 500 rows in it, then only the 500 rows will be populated with "US", etc... so Lastrow type of logic is needed. The column is always blank - so i am looking for my macro to update it (along with many other updates my macro is currently doing) thanks!

here is my code:

Sub TranslateLocation()
' populate LOCATION column with "US"
Cells.Find(What:="Location", After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select ' probably dont want to select entire column???

' NEED CORRECT CODE HERE...
Set myrange = Range(ActiveCell, Range("A65536").End(xlUp))
For Each Cell In myrange
Cell.Value = "US"
Next

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Mr_Roscoe

Board Regular
Joined
Mar 28, 2006
Messages
200
kwagner1,

The last row find and fill I use heaps is:

LR = ActiveSheet.UsedRange.Rows.Count 'finds the last row used or
LR = Cells(Rows.Count, 1).End(xlUp).Row 'finds the last row in column A or
LR = Cells(Rows.Count, 3).End(xlUp).Row 'finds the last row in column C etc

then

Range("A2:A" & LR).Value = "US" 'Fill "US" to last row
 

kwagner1

Active Member
Joined
Jun 10, 2003
Messages
445
thanks - that will work (kind of)... how do i return the COL letter when I find "Location". The column "BV" below will be dynamic based on the column i find "Location" in (BV today, tomorrow "Location" maybe in COl AA - so it needs to be dynamic)

Sub TranslateLocation()
' Routine to populate LOCATION column with "US"

' FIND THE COL THAT HAS "LOCATION" IN IT
Cells.Find(What:="Location", After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate

LR = ActiveSheet.UsedRange.Rows.Count 'finds the last row used or
Range("BV2:BV" & LR).Value = "US" 'Fill "US" to last row

End Sub
 

kwagner1

Active Member
Joined
Jun 10, 2003
Messages
445
figured it out (with your help!) thanks!

Sub TranslateLocation()
' populate LOCATION column with "US"

Cells.Find(What:="Location", After:=Range("A1"), LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate

MyColumnNum = ActiveCell.Column ' returns column number - 74 = BV, etc...
LR = ActiveSheet.UsedRange.Rows.Count 'finds the last row used or
Range(Cells(2, MyColumnNum), Cells(LR, MyColumnNum)).Value = "US"
 

Watch MrExcel Video

Forum statistics

Threads
1,133,278
Messages
5,657,817
Members
418,414
Latest member
ECMdusty

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
Top