1

I have an Excel spreadsheet where I need to amend a specific column.

Step 1. Find the column name

Step 2. Mark the all populated rows in this column

Step 3. Proceed with certain action (mostly find and replace or if other column is “this” then amend my column for “that).

I would like those first 2 steps specified and leave me the space to amend the code easily for proceeding with step 3.

I have VBA code which does a similar job. It searches for the specific column name, it marks all rows populated. It does not allow me to easily copy and paste other code, found on the internet, to the main code.

MACRO WHICH FINDS THE COLUMN NAME AND MARKS ALL RECORDS IN THIS COLUMN

Sub FindAddressColumn()
Dim rngAddress As Range
Set rngAddress = Range("A1:ZZ1").Find("Address")
If rngAddress Is Nothing Then
    MsgBox "Address column was not found."
    Exit Sub
End If
Range(rngAddress, rngAddress.End(xlDown)).Select
End Sub

Most of macros found on the internet have the column specified.

EXAMPLE OF CODE THAT I WOULD LIKE TO ADD TO THE MAIN CODE:

Sub GOOD_WORKS_Find_Replace_Commas_in_Emails()
Sheets("Data").Activate
Dim i As String
Dim k As String
i = ","
k = "."
Columns("R").Replace What:=i, Replacement:=k, LookAt:=xlPart, MatchCase:=False
Sheets("Automation").Activate
MsgBox "Removing commas in emails - Done!"
End Sub

I believe what I miss is the code which will “say” for the already marked columns rows…. And here you paste only the part of the code found on the internet.

2
  • Do you need to process ALL the matching cells in the desired column, or only specific? Example: you have column with 5 cells with this, but you want to replace only 3 of them, not 5 - based on some other criteria?
    – Peter L.
    Feb 15, 2013 at 10:40
  • In most of the cases the action would apply to ALL cells in the column. The only difference is that I will need one macro which will look to the values populated in column called Comments and amend the selected cell according to the comment. But as I said the same rule will apply to all cells in the selected column in the majority of cases
    – mgunia
    Feb 15, 2013 at 11:33

1 Answer 1

3

I think this code will do the job you want:

Sub ColumnReplace()

Dim TargetColumn As Range
Dim Header As String
Dim SearchFor As String
Dim ReplaceTo As String

Header = "ccc"
SearchFor = "111"
ReplaceTo = "99999"

Set TargetColumn = ThisWorkbook.ActiveSheet.Range("1:1").Find(Header, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
Set TargetColumn = Cells(1, TargetColumn.Column).EntireColumn
TargetColumn.Replace What:=SearchFor, Replacement:=ReplaceTo, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

End Sub

Adopt Workbook / Sheets names as well as strings for search / replace as you wish.

Sample file: https://www.dropbox.com/s/s7fghhlsmydjaf6/EntireColumnReplace.xlsm

4
  • Thanks Peter. Seems to be promising as the find & replace works. However, what amendments would I need to implement countif to this code?
    – mgunia
    Feb 15, 2013 at 14:31
  • Assuming the above code: k = Application.WorksheetFunction.CountIf(TargetColumn, ReplaceTo) However, that's another question. Sample file is available using the same link.
    – Peter L.
    Feb 15, 2013 at 14:39
  • Well not really as the question was about the flexible template, so that's why I was asking how should I use it properly. Anyway Peter, does the EntireColumn function marks every cell which is populated or it marks all cells in the column (including the blank ones)?
    – mgunia
    Feb 15, 2013 at 15:08
  • @mgunia it does not mark anything - it defines replace range (or simply range) as entire column, i.e. every cell in it reghardless of its content. For future - try to summarize all your wishes in initial question)
    – Peter L.
    Feb 15, 2013 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.