'How to find and replace the column name?
I am trying to find and replace the column name.
I want to loop in the first row only but my code loops the entire sheet.
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
Dim data As Variant
Set sht = ActiveWorkbook.Worksheets("ERT")
fndList = Array("colon.", _
"Selec:", _
"Sele, _
"Submi")
rplcList = Array("A", "A1", "S1", "D1")
With sht
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
'For Each sht In ActiveWorkbook.Worksheets("Exp")
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
'Next sht
Next x
End With
I want to only find and replace the array value from first Row A1 till end of 1 row.
Solution 1:[1]
Please, try the next way. It will try matching the first array elements on the first row headers and replace the found one with the corresponding string from the second:
Sub ReplaceHeaders()
Dim shT As Worksheet, lastCol As Long, fndList, rplcList, rngHd As Range, mtch, i As Long
Set shT = ActiveWorkbook.Worksheets("Final Exclusion")
lastCol = shT.cells(1, shT.Columns.count).End(xlToLeft).Column
fndList = Array("Enter one or more MSOs or ESOs to be excluded. Separate multiple values using a semicolon.", _
"Select the datasets from which you wish to exclude these MSOs:", _
"Select the reason code for the exclusion:", _
"Submitter's CWSID")
rplcList = Array("MSO", "SOCS", "Reason's", "Submitter")
Set rngHd = shT.Range(shT.cells(1, 1), shT.cells(1, lastCol))
For i = 0 To UBound(fndList)
mtch = Application.match(fndList(i), rngHd, 0)
If Not IsError(mtch) Then
rngHd(1, mtch).Value = rplcList(i)
End If
Next i
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | FaneDuru |