Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

VBA Macro that finds and replace the column name

I am new to macro I am trying to find and replace the column name. I have the code that works but I have a doubt. I am confused with For X = loop. I want the macro to loop in the first row only but my codes are like it loops the entire sheet which I dont want
Excel file

These are my codes

I need to fix the for loop that should read/loop only the first row not the entire sheet.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

below are my codes

Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
Dim data As Variant

Set sht = ActiveWorkbook.Worksheets("Final Exclusion")

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")

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

Please help me fix this "For x = LBound(fndList) To UBound(fndList)" where I want to code only for the first row A1: till end of 1st row. For loop should only loop and find and replace the array value from first Row A1 till end of 1 row

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading