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
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

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"
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,516
Messages
5,511,754
Members
408,864
Latest member
cmajewsk

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top