簡體   English   中英

我正在編寫一個宏以從一個工作簿復制到另一個工作簿,但出現錯誤

[英]I am writing a macro to copy from one workbook to another and I am getting an error

我在下面編寫VBA宏,並且在我希望文件路徑中的工作表為活動工作表的地方不斷出現錯誤。 我已經能夠編寫代碼來打開工作表。 現在,我需要將表單復制到另一個表單。 請幫忙

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

我的下標超出范圍錯誤

這是解決方案的開始,在您提供反饋時可能需要對其進行完善

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM