VBA macro to run a FindAndReplace on multiple columns

I’m new to VBA.

I have a workbook with multiple sheets. The first sheet, “feedbacktask”, contains the data I want to work with, while the other sheets are lists that I want to use for find and replace operations.

I have successfully run the Sub, replacing the data in one column (E) in my first sheet (“Feedbacktask”) with data from other sheets “Payroll” & “askHR”. Now, I need to perform a find and replace in another column (N) in the “Feedbacktask” sheet, using data from the “author list” sheet.

How can I do this?

This is how it looks so far, but it gives me an error message. If I only run it for one column (E) it works and replaces the text in the column (E) with the text from my lists in sheets “Payroll” & “askHR” :

Sub FindAndReplace()

    Dim FeedbacktaskWorksheet As Worksheet
    Dim searchRange As Range
    Dim replaceTable As Variant
    Dim findWhat As String
    Dim replaceWith As String
    Dim i As Long
    
    Set FeedbacktaskWorksheet = ThisWorkbook.Worksheets("Feedbacktask")
    
    With FeedbacktaskWorksheet
        Set searchRange = .Range("E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
            
    With ThisWorkbook.Worksheets("askHR DK")
        replaceTable = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
    
    With ThisWorkbook.Worksheets("Payroll")
        replaceTable = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
   
    Set FeedbacktaskWorksheet = ThisWorkbook.Worksheets("Feedbacktask")
    
    With FeedbacktaskWorksheet
        Set searchRange = .Range("N2:N" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
            
    With ThisWorkbook.Worksheets("Author list")
        replaceTable = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
     
    For i = 1 To UBound(replaceTable)
        findWhat = replaceTable(i, 1)
        replaceWith = replaceTable(i, 2)
        searchRange.Replace _
            What:=findWhat, _
            replacement:=replaceWith, _
            lookat:=xlPart, _
            MatchCase:=False
    Next i
    
End Sub

New contributor

user27385420 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Whenever you have a repetitive task it’s best to push each repeated part out into a separate method (Sub/Function). Refactoring your code a little it could work like this:

Sub FindAndReplace()

    Dim FeedbacktaskWorksheet As Worksheet, wb As Workbook
    
    Set wb = ThisWorkbook
    Set FeedbacktaskWorksheet = wb.Worksheets("Feedbacktask")
    
    'replacing col E values with list from "askHR DK"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("E2")), _
                GetRange(wb.Worksheets("askHR DK").Range("A1"), 2)
        
    'replacing col E values with list from "Payroll"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("E2")), _
                GetRange(wb.Worksheets("Payroll").Range("A1"), 2)
                    
    'replacing col N values with list from "Author list"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("N2")), _
                GetRange(wb.Worksheets("Author list").Range("A2"), 2)
                    
End Sub

'replace content in `searchrange` using pairs of values from range `replaceTable`
Sub ReplaceList(searchRange As Range, replaceTable As Range)
    Dim rw As Range
    For Each rw In replaceTable.Rows 'loop over pairs of values
        searchRange.Replace What:=rw.Cells(1).Value, _
                            Replacement:=rw.Cells(2).Value, _
                            lookat:=xlPart, MatchCase:=False
    Next rw
End Sub

'Return a range starting at `c` to the end of data in same column,
'   resized to specified number of columns (# of cols defaults to 1)
'Helper Function to avoid all those `.End(xlUp)` calls
Function GetRange(c As Range, Optional numCols As Long = 1) As Range
    With c.Parent
        Set GetRange = .Range(c, .Cells(.Rows.Count, _
                        c.Column).End(xlUp)).Resize(, numCols)
    End With
End Function

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

VBA macro to run a FindAndReplace on multiple columns

I’m new to VBA.

I have a workbook with multiple sheets. The first sheet, “feedbacktask”, contains the data I want to work with, while the other sheets are lists that I want to use for find and replace operations.

I have successfully run the Sub, replacing the data in one column (E) in my first sheet (“Feedbacktask”) with data from other sheets “Payroll” & “askHR”. Now, I need to perform a find and replace in another column (N) in the “Feedbacktask” sheet, using data from the “author list” sheet.

How can I do this?

This is how it looks so far, but it gives me an error message. If I only run it for one column (E) it works and replaces the text in the column (E) with the text from my lists in sheets “Payroll” & “askHR” :

Sub FindAndReplace()

    Dim FeedbacktaskWorksheet As Worksheet
    Dim searchRange As Range
    Dim replaceTable As Variant
    Dim findWhat As String
    Dim replaceWith As String
    Dim i As Long
    
    Set FeedbacktaskWorksheet = ThisWorkbook.Worksheets("Feedbacktask")
    
    With FeedbacktaskWorksheet
        Set searchRange = .Range("E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
            
    With ThisWorkbook.Worksheets("askHR DK")
        replaceTable = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
    
    With ThisWorkbook.Worksheets("Payroll")
        replaceTable = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
   
    Set FeedbacktaskWorksheet = ThisWorkbook.Worksheets("Feedbacktask")
    
    With FeedbacktaskWorksheet
        Set searchRange = .Range("N2:N" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
            
    With ThisWorkbook.Worksheets("Author list")
        replaceTable = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
     
    For i = 1 To UBound(replaceTable)
        findWhat = replaceTable(i, 1)
        replaceWith = replaceTable(i, 2)
        searchRange.Replace _
            What:=findWhat, _
            replacement:=replaceWith, _
            lookat:=xlPart, _
            MatchCase:=False
    Next i
    
End Sub

New contributor

user27385420 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Whenever you have a repetitive task it’s best to push each repeated part out into a separate method (Sub/Function). Refactoring your code a little it could work like this:

Sub FindAndReplace()

    Dim FeedbacktaskWorksheet As Worksheet, wb As Workbook
    
    Set wb = ThisWorkbook
    Set FeedbacktaskWorksheet = wb.Worksheets("Feedbacktask")
    
    'replacing col E values with list from "askHR DK"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("E2")), _
                GetRange(wb.Worksheets("askHR DK").Range("A1"), 2)
        
    'replacing col E values with list from "Payroll"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("E2")), _
                GetRange(wb.Worksheets("Payroll").Range("A1"), 2)
                    
    'replacing col N values with list from "Author list"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("N2")), _
                GetRange(wb.Worksheets("Author list").Range("A2"), 2)
                    
End Sub

'replace content in `searchrange` using pairs of values from range `replaceTable`
Sub ReplaceList(searchRange As Range, replaceTable As Range)
    Dim rw As Range
    For Each rw In replaceTable.Rows 'loop over pairs of values
        searchRange.Replace What:=rw.Cells(1).Value, _
                            Replacement:=rw.Cells(2).Value, _
                            lookat:=xlPart, MatchCase:=False
    Next rw
End Sub

'Return a range starting at `c` to the end of data in same column,
'   resized to specified number of columns (# of cols defaults to 1)
'Helper Function to avoid all those `.End(xlUp)` calls
Function GetRange(c As Range, Optional numCols As Long = 1) As Range
    With c.Parent
        Set GetRange = .Range(c, .Cells(.Rows.Count, _
                        c.Column).End(xlUp)).Resize(, numCols)
    End With
End Function

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

VBA macro to run a FindAndReplace on multiple columns

I’m new to VBA.

I have a workbook with multiple sheets. The first sheet, “feedbacktask”, contains the data I want to work with, while the other sheets are lists that I want to use for find and replace operations.

I have successfully run the Sub, replacing the data in one column (E) in my first sheet (“Feedbacktask”) with data from other sheets “Payroll” & “askHR”. Now, I need to perform a find and replace in another column (N) in the “Feedbacktask” sheet, using data from the “author list” sheet.

How can I do this?

This is how it looks so far, but it gives me an error message. If I only run it for one column (E) it works and replaces the text in the column (E) with the text from my lists in sheets “Payroll” & “askHR” :

Sub FindAndReplace()

    Dim FeedbacktaskWorksheet As Worksheet
    Dim searchRange As Range
    Dim replaceTable As Variant
    Dim findWhat As String
    Dim replaceWith As String
    Dim i As Long
    
    Set FeedbacktaskWorksheet = ThisWorkbook.Worksheets("Feedbacktask")
    
    With FeedbacktaskWorksheet
        Set searchRange = .Range("E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
            
    With ThisWorkbook.Worksheets("askHR DK")
        replaceTable = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
    
    With ThisWorkbook.Worksheets("Payroll")
        replaceTable = .Range("A1:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
   
    Set FeedbacktaskWorksheet = ThisWorkbook.Worksheets("Feedbacktask")
    
    With FeedbacktaskWorksheet
        Set searchRange = .Range("N2:N" & .Cells(.Rows.Count, "E").End(xlUp).Row)
    End With
            
    With ThisWorkbook.Worksheets("Author list")
        replaceTable = .Range("A2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
    End With
     
    For i = 1 To UBound(replaceTable)
        findWhat = replaceTable(i, 1)
        replaceWith = replaceTable(i, 2)
        searchRange.Replace _
            What:=findWhat, _
            replacement:=replaceWith, _
            lookat:=xlPart, _
            MatchCase:=False
    Next i
    
End Sub

New contributor

user27385420 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Whenever you have a repetitive task it’s best to push each repeated part out into a separate method (Sub/Function). Refactoring your code a little it could work like this:

Sub FindAndReplace()

    Dim FeedbacktaskWorksheet As Worksheet, wb As Workbook
    
    Set wb = ThisWorkbook
    Set FeedbacktaskWorksheet = wb.Worksheets("Feedbacktask")
    
    'replacing col E values with list from "askHR DK"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("E2")), _
                GetRange(wb.Worksheets("askHR DK").Range("A1"), 2)
        
    'replacing col E values with list from "Payroll"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("E2")), _
                GetRange(wb.Worksheets("Payroll").Range("A1"), 2)
                    
    'replacing col N values with list from "Author list"
    ReplaceList GetRange(FeedbacktaskWorksheet.Range("N2")), _
                GetRange(wb.Worksheets("Author list").Range("A2"), 2)
                    
End Sub

'replace content in `searchrange` using pairs of values from range `replaceTable`
Sub ReplaceList(searchRange As Range, replaceTable As Range)
    Dim rw As Range
    For Each rw In replaceTable.Rows 'loop over pairs of values
        searchRange.Replace What:=rw.Cells(1).Value, _
                            Replacement:=rw.Cells(2).Value, _
                            lookat:=xlPart, MatchCase:=False
    Next rw
End Sub

'Return a range starting at `c` to the end of data in same column,
'   resized to specified number of columns (# of cols defaults to 1)
'Helper Function to avoid all those `.End(xlUp)` calls
Function GetRange(c As Range, Optional numCols As Long = 1) As Range
    With c.Parent
        Set GetRange = .Range(c, .Cells(.Rows.Count, _
                        c.Column).End(xlUp)).Resize(, numCols)
    End With
End Function

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật