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
Here's the start of a solution, it will probably need to be refined as you give feedback
Sub DoStuff()
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)
'' Variables for keeping track opened files,worksheets, and ranges
Dim openedWB As Workbook
Dim srcSheet As Worksheet
Dim srcRange As Range
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
'' Commented out due to being a small task
'' Good practice though for longer running Macros
'Application.EnableEvents = False
'Application.DisplayAlerts = False
'Application.ScreenUpdating = False
'' Keep track of your opened workbook with a variable
Set openedWB = Workbooks.Open(TemplateLocation)
'' No need to activate now that we are accessing it through a variable
''Windows(TemplateName).Activate
Set SourceWorkBook = ActiveWorkbook
With openedWB
'Sheets("QRA Download").Activate
'' Added a period to utilize the 'With' Statement
'' Saved to a variable instead of activating it
'' I'm also guessing this is where the Error was happening
Set srcSheet = .Sheets("QRA Download")
''ActiveSheet.Range("D2:D199902").Select
''If you are looking to get the whole column there's a better way - 'D:D'
Set srcRange = srcSheet.Range("D:D") '' if D2:D199902 is intentional feel free to change it
''' We are going to bypass the clipboard altogether
'Application.CutCopyMode = False
'Selection.Copy
'paste the data in the current location
'' By CurrentFile did you intend for that to be the workbook with this code in it or some other workbook?
'' The way it was originally coded you would be activating Windows(TemplateName) again
'' due to the line Windows(TemplateName).Activate and then CurrentFile = ActiveWorkbook.Name
'CurrentFile = ActiveWorkbook.Name
'' I'm not sure where you wanted to copy this
'' So I'm having it copied to the currently active sheet for now
ActiveSheet.Range("C:C").Value = srcRange.Value
End With
End If
Next
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.