Find cell, then select data in that column and find & replace

lilmisspink07

New Member
Joined
Aug 17, 2011
Messages
4
Hello,

Hopefully someone can help me with this as it has been driving me nuts! :-)

I am writing a macro to format a file. The number of rows can change and also, there may not always be data in every cell so I need to make sure this is covered in the macro.

I have this little extract that I use earlier when auto-filling to ensure I get all the rows:

' Dynamic autofill success counts for all samples
LastRow = Range("A65536").End(xlUp).Row
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B" & LastRow), Type:=xlFillDefault

Now in a later part, I need to find a cell in the header row, then select the column down to the last row and perform a find and replace. I can't figure out how to get my LastRow variable into the selected range though?

This is where it needs to go....

' GTtoAC on DQ381153 & highlight pink
Cells.Find(What:="DQ381153", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="T", Replacement:="A", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="G", Replacement:="C", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
With Selection.Interior
.ColorIndex = 38
.Pattern = xlSolid
End With

I'm guessing that LastRow needs to go somewhere in the red bit but I'm new to VB and struggling to figure it out!!

Thanks in advance!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You should use

La
Code:
stRow = Range("A" & rows.count).End(xlUp).Row
so the code is compatible with later versions of xl.

try:

Code:
Sub test()
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Range("B2").AutoFill Destination:=Range("B2:B" & LastRow), Type:=xlFillDefault
    'Now in a later part, I need to find a cell in the header row, then select the column down to the last row and perform a find and replace. I can't figure out how to get my LastRow variable into the selected range though?
    
    This is where it needs to go....
    
    ' GTtoAC on DQ381153 & highlight pink
    Set rng = Cells.Find(What:="DQ381153", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    With Range(rng, Cells(lr, rng.Column))
        .Replace What:="T", Replacement:="A", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        .Replace What:="G", Replacement:="C", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        With .Interior
            .ColorIndex = 38
            .Pattern = xlSolid
        End With
    End With
End Sub
 
Upvote 0
Thank you for your help! The tip about making sure it works for later versions then 2003 which I am using is great, I didn't even think of that.

However the second bit of code doesn't seem to work right - it doesn't do anything to the column I need formatting (approximately AM1), instead it highlights the whole of column C in pink....

I am trying various things along the lines of the below but it breaks on the line where I define the range as I'm sure I'm not doing it right. Any ideas where I am going wrong??

' GTtoAC on DQ381153 & highlight pink
Set StartCell = Cells.Find(What:="DQ381153", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Range(StartCell, LastRow).Select
Selection.Replace What:="T", Replacement:="A", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Selection.Replace What:="G", Replacement:="C", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
With Selection.Interior
.ColorIndex = 38
.Pattern = xlSolid
End With
 
Upvote 0
I wouldn't 'select' everything - there's no need, but

Code:
Range(StartCell, cells(lastrow,startcell.column)).Select
ought to work
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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