Open Multiple Excel and update Row value based on Header

RickSorkin

New Member
Joined
Mar 1, 2022
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
I am new to this- I was trying to find a way to update multiple sheets in folder with values in column based on header in the sheets:
Scenario:
I have Multiple excel files at location and want to update the rows data based on Header-
Example-
Data to be update ="1-New"
Header in the Multiple excel- "Status"

So I want to open each and every excel and update the column data to "1-New" if the Header is Status till the last row in excel

Previously it was hardcoded to column but I want to update based on header because in every file the Header position changes.

Need Help!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
This macro assumes the headers are in row 1. Change the folder path (in red) and the sheet name (in blue) to suit your needs.
Rich (BB code):
Sub UpdateRows()
    Application.ScreenUpdating = False
    Dim desWB As Workbook, LastRow As Long, fnd As Range
    Const strPath As String = "C:\Test\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set desWB = Workbooks.Open(strPath & strExtension)
        With desWB.Sheets("Sheet1")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            Set fnd = .Rows(1).Find("Status", LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                .Cells(2, fnd.Column).Resize(LastRow - 1).Value = "1-New"
            End If
        End With
        desWB.Close savechanges:=True
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This macro assumes the headers are in row 1. Change the folder path (in red) and the sheet name (in blue) to suit your needs.
Rich (BB code):
Sub UpdateRows()
    Application.ScreenUpdating = False
    Dim desWB As Workbook, LastRow As Long, fnd As Range
    Const strPath As String = "C:\Test\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set desWB = Workbooks.Open(strPath & strExtension)
        With desWB.Sheets("Sheet1")
            LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            Set fnd = .Rows(1).Find("Status", LookIn:=xlValues, lookat:=xlWhole)
            If Not fnd Is Nothing Then
                .Cells(2, fnd.Column).Resize(LastRow - 1).Value = "1-New"
            End If
        End With
        desWB.Close savechanges:=True
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
Thank you for your response. This solved my problem. Thank you for taking time and helping out.
 
Upvote 0
You are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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