Hi!
I am trying to copy a formula from range A1:H2 in sheet 1 of my master workbook and paste it into the range C1:J2 of the first sheet in all .xls files in a user-defined directory. The .xls files are all uniquely named and have unique sheet names. Thanks in advance for your help!!
This is the code I've cobbled together but it isn't working:
'Declaring variables
Dim FileName, FolderPath, FileArray() As String
Dim Count1, i As Integer
Dim SourceWB, DestWB As Workbook
Set FileDiag = Application.FileDialog(msoFileDialogFilePicker)
With FileDiag
.AllowMultiSelect = True
.Show
End With
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
'Getting the file name from the folder
FileName = Dir(FolderPath & "*.xls")
Count1 = 0
'Creating an array which consists of file name of all files in the folder
While FileName <> ""
Count1 = Count1 + 1
ReDim Preserve FileArray(1 To Count1)
FileArray(Count1) = FileName
FileName = Dir()
Wend
Set SourceWB = ThisWorkbook
For i = 1 To UBound(FileArray)
'Opening the workbook
Set DestWB = Workbooks.Open(FolderPath & FileArray(i))
'Pasting the required header
SourceWB.Worksheets(1).Range("A1:H2").Copy DestWB.Worksheets(1).Range("C1:J2")
'Closing the workbook
DestWB.Close True
Next
Set DestWB = Nothing
Set SourceWB = Nothing
End Sub
I am trying to copy a formula from range A1:H2 in sheet 1 of my master workbook and paste it into the range C1:J2 of the first sheet in all .xls files in a user-defined directory. The .xls files are all uniquely named and have unique sheet names. Thanks in advance for your help!!
This is the code I've cobbled together but it isn't working:
'Declaring variables
Dim FileName, FolderPath, FileArray() As String
Dim Count1, i As Integer
Dim SourceWB, DestWB As Workbook
Set FileDiag = Application.FileDialog(msoFileDialogFilePicker)
With FileDiag
.AllowMultiSelect = True
.Show
End With
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
'Getting the file name from the folder
FileName = Dir(FolderPath & "*.xls")
Count1 = 0
'Creating an array which consists of file name of all files in the folder
While FileName <> ""
Count1 = Count1 + 1
ReDim Preserve FileArray(1 To Count1)
FileArray(Count1) = FileName
FileName = Dir()
Wend
Set SourceWB = ThisWorkbook
For i = 1 To UBound(FileArray)
'Opening the workbook
Set DestWB = Workbooks.Open(FolderPath & FileArray(i))
'Pasting the required header
SourceWB.Worksheets(1).Range("A1:H2").Copy DestWB.Worksheets(1).Range("C1:J2")
'Closing the workbook
DestWB.Close True
Next
Set DestWB = Nothing
Set SourceWB = Nothing
End Sub