简体   繁体   English

VBA 从 Excel 中的另一个工作簿复制数据

[英]VBA to Copy Data from Another Workbook in Excel

Problem: A problem in making a cell reference in VBA for source workbook name.问题:在 VBA 中对源工作簿名称进行单元格引用时出现问题。 Error 9 subscripts out of range.错误 9 下标超出范围。

Task I am doing?我正在做的任务? Ex.前任。 I have to copy 32 columns out of 50 columns from a workbook(Master) into a new workbook.我必须将工作簿(主)中的 50 列中的 32 列复制到新工作簿中。 I am able to make a code to copy and paste the column in the required sequence in new workbook.我能够制作一个代码以在新工作簿中以所需的顺序复制和粘贴列。

The master workbook is a template of a register to take peoples information and it saved with a new name.主工作簿是一个寄存器模板,用于获取人们的信息,并以新名称保存。 I have more than 65 workbooks(Master) to copy.我有超过 65 个工作簿(主)要复制。 I was trying to make a cell reference where I paste the source workbook(Master) name.我试图在粘贴源工作簿(主)名称的位置进行单元格引用。 I am aware that source workbook has to be open will running VBA.我知道源工作簿必须打开才能运行 VBA。

I made icell as variable to fetch that value from cell B2, where I pasted workbook name but code is not running.我将icell 作为变量从单元格B2 中获取该值,在那里我粘贴了工作簿名称但代码没有运行。

Code attached Any suggestion is highly appreciated.附上代码 高度赞赏任何建议。

Sub Copy_Paste()
    Dim iCell As String  
    iCell = Workbooks("Crack it").Worksheets("Intro").Range("B2").Value
    'B2 will store the name of source workbook for copying data which will keep on changing 

    Workbooks("iCell").Worksheets("Register").Range("E2:E50").Copy
    Workbooks("Crack it.xlsm").Worksheets("Risk").Range("A2").PasteSpecial Paste:=xlPasteValues 'Refid

    Workbooks("iCell").Worksheets("Register").Range("H2:H50").Copy
    Workbooks("Crack it.xlsm").Worksheets("Risk").Range("B2").PasteSpecial Paste:=xlPasteValues 'Tags

    Workbooks("iCell").Worksheets("Register").Range("A2:A50").Copy
    Workbooks("Crack it.xlsm").Worksheets("Risk").Range("c2").PasteSpecial Paste:=xlPasteValues 'Name

    Workbooks("iCell").Worksheets("Register").Range("Z2:Z50").Copy
    Workbooks("Crack it.xlsm").Worksheets("Risk").Range("D2").PasteSpecial Paste:=xlPasteValues 'Element

    ...... code keeps on repeating till column 32th 
End Sub

I ahve somethign similar, I read all the files located on a folder for your case you will save all the 65 Workbooks in a folder, then read each one of them with a loop, once it takes the first book opened you will take the info:我有类似的东西,我为您的案例阅读了文件夹中的所有文件,您将所有 65 个工作簿保存在一个文件夹中,然后循环阅读每个工作簿,一旦打开第一本书,您将获取信息:

Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1) & sItem + "\"
    FilePathBox.Value = sItem
End With
NextCode:
GetFolder = sItem & "\"
Set fldr = Nothing

If you see my code above it taks the url of the folder, then I will start a process that will see file by file:如果你看到我上面的代码,它需要文件夹的 url,那么我将启动一个进程,该进程将一个文件一个文件地查看:

Private Sub UserForm_Activate()
    UserForm1.Top = (Application.Height / 2) - (UserForm1.Height / 2) + 45
    UserForm1.Left = (Application.Width / 2) - (UserForm1.Width / 2) + 200
    UserForm1.Label1.Visible = True
    Label1.Caption = ""
    '-----------------------------------------THIS IS THE LOOP OFR EACH FILE INTO THE FOLDER--------------------------------------------------
    MyPath = UserForm2.FilePathBox.Value
    Dim strFilename As String
    strFilename = Dir(MyPath & "*.txt", vbNormal)
    filesc = 1
    If Len(strFilename) = 0 Then Exit Sub
    Do Until strFilename = ""
        Application.DisplayAlerts = False
        If filesc >= 1 Then
            showBarName.Caption = showBarName.Caption & strFilename
            'Worksheets.Add(Worksheets(Worksheets.Count)).Name = "Data"
            Call ThisWorkbook.XY_Data((UserForm2.FilePathBox.Value & strFilename), (strFilename & ""))
            showBarName.Caption = "Generating XY Data for %PATH%/"
        End If
        filesc = filesc + 1
        counter = counter + cols
        strFilename = Dir()
    Loop
    '------------------------------------------END--------------------------------------------------------------------------------------------
    Worksheets("Spec").Visible = True
    For Each ws In ThisWorkbook.Worksheets
         If ws.Name = "Spec" Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    Next ws
    UserForm1.Hide
    showBarName.Caption = "Saving File"
    'THIS IS FOR XLSX
    Application.StatusBar = "Save your file into the PNL Project path."
    Application.DisplayAlerts = False
    Dim hoja As Worksheet
    For Each hoja In Sheets
        If ActiveSheet.Name = "Data" Then
            ActiveWindow.SelectedSheets.Delete
        End If
    Next hoja
    fileSaveName = Application.GetSaveAsFilename( _
    fileFilter:="Excel Workbooks (*.xlsx), *.xlsx")
    If fileSaveName <> False Then
        Application.ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=51
    End If
    showBarName.Caption = "Generating XY Data for %PATH%/"
    'This is to close the macro without saving
    Application.StatusBar = "XY Data Generated by Yazaki <<erik.floresdelfin@mx.yazaki.com>>"
    'ThisWorkbook.Close savechanges = False
    Application.DisplayAlerts = True
End Sub

Then on the above code in some part I take each file in txt format, and I call a method which contains the url of the file that I want to open, the rest should be taking what tou need to copy and paste on the actual file, the final code I show is how to save the file asking to the user, sorry for the trash code but I think you caould manage taking what you need.然后在上面的代码中,我以txt格式获取每个文件,并调用一个包含我要打开的文件的url的方法,其余部分应该是需要复制并粘贴到实际文件上的内容,我展示的最终代码是如何保存文件,询问用户,对于垃圾代码很抱歉,但我认为你可以设法获取你需要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM