JeffGrant
Well-known Member
- Joined
- Apr 7, 2021
- Messages
- 558
- Office Version
- 365
- Platform
- Windows
Hi All,
I have adopted the following code from another user, Tmteast, in this forum and have a minor issue. On the whole it works great.
We set the table, array and ranges all ok.
All Variables are Dimensioned at the top of the module under Option Explicit
The issue is that when the the Find/Replace Sub is called, the code does the Find/Replace over the entire workbook, instead of just the set sheet. Which implies that actual source data in the tables is also being changed. Which it does.
The code seems to lose the set sheet name when the Find/Replace sub is called.
Why is the Find/Replace not being restricted to the set sheet?
All assistance is graciously received.
Thankyou
Sub Multi_FindReplace()
'Create variable to point to the Class table
Set tbl = Sheet27.ListObjects("tblFRTClassFull")
'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)
'Designate Columns for Find/Replace data
fndList = 1
rplcList = 2
Set shtJson = Sheet10
With shtJson
lrow = .Cells(Rows.Count, 1).End(xlUp).Row
lCol = .Cells(1, Columns.Count).End(xlToLeft).Column
Set rng1 = .Range("I1:I" & lrow) 'Track Name
Set rng2 = .Range("AC1:AC" & lrow) 'Track Name in Start Time Table
End With
'Create variable to point to the Track Name table
Set tbl = Sheet27.ListObjects("tblFRTTrackFull")
'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)
'Replace Track Name
Set rng = rng1
Call FindReplaceRangeWhole
'Replace Form Track Name
Set rng = rng2
Call FindReplaceRangeWhole
End Sub
Sub FindReplaceRangeWhole()
'Loop through each item in Array lists
For x = LBound(myArray, 1) To UBound(myArray, 2)
rng.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next x
End Sub
I have adopted the following code from another user, Tmteast, in this forum and have a minor issue. On the whole it works great.
We set the table, array and ranges all ok.
All Variables are Dimensioned at the top of the module under Option Explicit
The issue is that when the the Find/Replace Sub is called, the code does the Find/Replace over the entire workbook, instead of just the set sheet. Which implies that actual source data in the tables is also being changed. Which it does.
The code seems to lose the set sheet name when the Find/Replace sub is called.
Why is the Find/Replace not being restricted to the set sheet?
All assistance is graciously received.
Thankyou
Sub Multi_FindReplace()
'Create variable to point to the Class table
Set tbl = Sheet27.ListObjects("tblFRTClassFull")
'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)
'Designate Columns for Find/Replace data
fndList = 1
rplcList = 2
Set shtJson = Sheet10
With shtJson
lrow = .Cells(Rows.Count, 1).End(xlUp).Row
lCol = .Cells(1, Columns.Count).End(xlToLeft).Column
Set rng1 = .Range("I1:I" & lrow) 'Track Name
Set rng2 = .Range("AC1:AC" & lrow) 'Track Name in Start Time Table
End With
'Create variable to point to the Track Name table
Set tbl = Sheet27.ListObjects("tblFRTTrackFull")
'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)
'Replace Track Name
Set rng = rng1
Call FindReplaceRangeWhole
'Replace Form Track Name
Set rng = rng2
Call FindReplaceRangeWhole
End Sub
VBA Code:
'Loop through each item in Array lists
For x = LBound(myArray, 1) To UBound(myArray, 2)
rng.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next x
End Sub