Results 1 to 8 of 8

Adding the same header row to multiple spreadsheets

Threaded View

  1. #7
    Registered User
    Join Date
    01-31-2009
    Location
    Romania, Eastern Europe
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Adding the same header row to multiple spreadsheets

    I was hoping to find a way to do this without using a macro, but I doubt this is possible, anyway I managed to do this in the meantime, Hans from Microsoft Answers kindly provided the code for the following macro which does just what I was asking for:

    Sub SetHeaderRow()
      Dim strPath As String
      Dim strFile As String
      Dim wbk As Workbook
      Dim wsh As Worksheet
      ' Let user select a folder
      With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
          strPath = .SelectedItems(1)
        Else
          MsgBox "No folder selected", vbInformation
          Exit Sub
        End If
      End With
      Application.ScreenUpdating = False
      If Right(strPath, 1) <> "\" Then
        strPath = strPath & "\"
      End If
      ' Loop through the Excel workbooks in the folder
      strFile = Dir(strPath & "*.xls*")
      Do While strFile <> ""
        ' Open the workbook
        Set wbk = Workbooks.Open(Filename:=strPath & strFile, AddToMRU:=False)
        ' Loop through the worksheets in the workbook
        For Each wsh In wbk.Worksheets
          ' Insert row
          wsh.Range("A1").EntireRow.Insert
          ' Set some values
          wsh.Range("A1") = "This"
          wsh.Range("B1") = "That"
          ' ...
          wsh.Range("L1") = "Finally"
        Next wsh
        ' Save and close the workbook
        wbk.Close SaveChanges:=True
        ' On to the next
        strFile = Dir
      Loop
      Application.ScreenUpdating = True
    End Sub
    Thanks again Marcol, and again apologies for the cross-posting.
    Last edited by longtalker; 03-24-2011 at 07:39 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1