I am writing the VBA macros below and i keep getting an error where i want the sheet in the file path to be the active sheet. I have been able to write a code to open the sheet. Now i need to copy form the sheet to another. Please help
Dim Templatepath As String
Dim CurrentFile As String
Dim cells As Range
Dim SourceWorkBook As Workbook
Dim FilesName As Range
Dim SheetToReplace As String
Dim SheetToCopy As String
Dim OpenTitle As String
Dim FileToOpen As String
Dim FileName As String
'Get the default Template path and change to it.
Templatepath = ThisWorkbook.Sheets("Actual Opex From QRA").Range("Q1").Value
FilePathLength = Len(Templatepath)
FilePathLength = FilePathLength - 1
Templatepath = Left(Templatepath, FilePathLength)
FilePathLength = FilePathLength - 1
Templatepath = Right(Templatepath, FilePathLength)
'to make the file active
For Each FilesName In Worksheets("Actual Opex From QRA").Range("Q2:Q4")
If FilesName.Value <> "" Then
CurrentSheetName = FilesName.Value
TemplateName = FilesName + ".xlsx"
TemplateLocation = Templatepath + "\" + TemplateName
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open (TemplateLocation)
Windows(TemplateName).Activate
Set SourceWorkBook = ActiveWorkbook
With ActiveWorkbook
Sheets("QRA Download").Activate
ActiveSheet.Range("D2:D199902").Select
Application.CutCopyMode = False
Selection.Copy
'paste the data in the current location
CurrentFile = ActiveWorkbook.Name
Windows(CurrentFile).Activate
ActiveSheet.Range("c9:c199902").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
End If
Next
End Sub
I am getting the subscript out of range error