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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
 
Upvote 0
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
 
Upvote 0
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"
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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