Excel VBA Macro find and replace nextRow nextColumn

yousufj56

Board Regular
Joined
May 22, 2014
Messages
51
Hi there,

Here is my task:

Sheet2: Column A will contain string that i want to find in Sheet1 and replace with the values given in Columns B to myLastColumn.

Sheet1: Column A will contain identical string from row 1 to myLastRow. Each cell will contain string found in Sheet2, column A. I want to replace this string with values from Sheet2, Columns B to myLastColumn.

However;

Sheet1, Cell A1 will be replaced by values in Sheet2, column B (10 rows).

Then, Sheet1, Cell A2 will be replaced by Sheet2, column C (10 rows).

Then, Sheet1, Cell A3 will be replaced by Sheet2, Column D (10 rows).

This will continue until the lastrow is reached in Sheet1 and the last column is reach in Sheet2.


Code:
Sub myReplace()


    Dim myDataSheet As Worksheet
    Dim myReplaceSheet As Worksheet
    Dim myLastRow As Long
    Dim myRow As Long
    Dim myColumn As Long
    Dim myLastColumn As Long
    Dim myFind As String
    Dim myReplace As String
    
'   Specify name of Data sheet
    Set myDataSheet = Sheets("Sheet1")
    
'   Specify name of Sheet with list of replacements
    Set myReplaceSheet = Sheets("Sheet2")
    
'   Assuming list to search start in column A on row 2, find last entry in list
    myLastRow = myReplaceSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
'   Assuming list of replacement start in column B on row 2, find last entry in list
    myLastColumn = myReplaceSheet.Cells(2, Columns.Count).End(xlToLeft).Column
    
    Application.ScreenUpdating = False
    
'   Loop through all list of replacments
    For myRow = 2 To myLastRow
    For myColumn = 2 To myLastColumn
'       Get find and replace values (from columns A and B)
        myFind = myReplaceSheet.Cells(myRow, "A")
        myReplace = myReplaceSheet.Cells(myRow, myColumn)
'       Start at top of data sheet and do replacements
        myDataSheet.Activate
        Range("A1").Select
'       Ignore errors that result from finding no matches
        On Error Resume Next
'       Do all replacements on column A of data sheet
        Columns("A:A").Replace What:=myFind, Replacement:=myReplace, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
'       Reset error checking
        On Error GoTo 0
    Next myColumn
    Next myRow
    
    Application.ScreenUpdating = True
    
    MsgBox "Replacements complete!"
    
End Sub


Thanks in advance!!:)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Are you meaning by:
However;

Sheet1, Cell A1 will be replaced by values in Sheet2, column B (10 rows).

Then, Sheet1, Cell A2 will be replaced by Sheet2, column C (10 rows).

Then, Sheet1, Cell A3 will be replaced by Sheet2, Column D (10 rows).
that, as an example:
Contents of A1 = "A B C D E F G H I J", the contents of 10 rows of Col B Sheet2,
Contents of A2 = "D I G K N L I F E R", the contents of 10 rows of Col C Sheet2,
Contents of A3 = "G H J R W Q Y I O" , the contents of 10 rows of Col D Sheet2,
etc?

Maybe an image of what you intend might be an assist.
 
Upvote 0
Wait sorry, i would like to make one revision to your example:

Its more like this:

Contents of A1 = "A B C D E F G H I J", the contents of 10 rows of Col B Sheet2,
Contents of A2 = "A B C D E F G H I J", the contents of 10 rows of Col C Sheet2,
Contents of A3 = "A B C D E F G H I J", the contents of 10 rows of Col D Sheet2,
 
Upvote 0
I therefore get the impression you wish to concatenate into one cell the contents of 10 consecutive cells as you go down the column.

If that is so, I think that if you modify my code it will meet your needs (I haven't looked for the last row or column confining myself to a specific grid).
Consider my image as an overlay of Sheet1 (where the result is to appear) and Sheet2 where the column data is stored.
Code:
Sub Buildrow()
'
' Buildrow Macro
'

'
    Dim a, b, c, d, e, f As Long
    Dim thislist As String
    For a = 2 To 7
    thislist = Cells(a, 1).Value
    For b = 2 To 4
    Sheets("Sheet2").Select
    thislist = thislist & "," & Cells(a, b).Value
    
    Next b
   
     Sheets("Sheet1").Range("A" & a).Value = thislist
     Next a
End Sub

Excel 2013/2016
ABCDEF
1SHEET1SHEET2
2Pppp,A,W,ZPpppAWZ
3Qqqq,D,E,DQqqqDED
4Rrrr,F,R,FRrrrFRF
5Jjjj,G,T,GJjjjGTG
6Llll,H,Y,HLlllHYH
7Bbbb,J,U,JBbbbJUJ
Sheet1
 
Upvote 0
Stop. I mixed up my rows and columns. changing the order of that graphic shows three rows as such:
Pppp,A,D,F,G,H,J
Qqqq,W,E,R,T,Y,U
Rrrr,Z,D,F,G,H,J

The code for that is:
Code:
Sub Buildrow()
'
' Buildrow Macro
'

'
    Dim a, b, c, d, e, f As Long
    Dim thislist As String
    Sheets("Sheet2").Select
    For a = 2 To 4
    thislist = Cells(a, 1).Value
    For b = 2 To 7
    
    thislist = thislist & "," & Cells(b, a).Value
    
    Next b
   
     Sheets("Sheet1").Range("A" & a).Value = thislist
     Next a
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,575
Messages
6,131,501
Members
449,654
Latest member
andz

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