Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用对话框选择文件并对其进行操作_Excel_Excel 2010_Vba - Fatal编程技术网

Excel 使用对话框选择文件并对其进行操作

Excel 使用对话框选择文件并对其进行操作,excel,excel-2010,vba,Excel,Excel 2010,Vba,我有一个代码,可以比较不同工作簿中两个工作表之间的标题,并在主工作簿中复制粘贴数据 'lastCol = Worksheets("Dashboard").Cells(3, Columns.Count).End(xlToLeft).Column lastCol = 15 lastrow = Worksheets("Dashboard").Cells(Rows.Count, 1).End(xlUp).Row Set cmpRng = Range(Cells(1, 1), Ce

我有一个代码,可以比较不同工作簿中两个工作表之间的标题,并在主工作簿中复制粘贴数据

   'lastCol = Worksheets("Dashboard").Cells(3, Columns.Count).End(xlToLeft).Column
   lastCol = 15
   lastrow = Worksheets("Dashboard").Cells(Rows.Count, 1).End(xlUp).Row
   Set cmpRng = Range(Cells(1, 1), Cells(3, lastCol))
   a = cmpRng
   i = Cells(Rows.Count, 1).End(xlUp).Row

   Set Wbk = Workbooks.Open("Z:\RMG\RMG Data Master\Global_HEADCOUNT.xls")
   Worksheets("GLOBAL_HEADCOUNT").Select
   Mastcol = Cells(1, Columns.Count).End(xlToLeft).Column
   j = Cells(Rows.Count, 1).End(xlUp).Row
   Set mastRng = Range(Cells(1, 1), Cells(1, Mastcol))
   b = mastRng

For k = 1 To lastCol
    For n = 1 To Mastcol
        If UCase(a(3, k)) = UCase(b(1, n)) Then
        Windows("Global_HEADCOUNT").Activate
            Worksheets("GLOBAL_HEADCOUNT").Range(Cells(2, n), Cells(j, n)).Copy
            Windows("Dashboard.xlsm").Activate
            Worksheets("Dashboard").Select
            Cells(i + 1, k).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            Exit For
        End If
    Next
Next
Call Wbk.Close(False)
现在的问题是,每次文件名更改时,用户都必须输入代码并进行更改,这可能会导致问题,因此我希望避免为他们提供使用对话框选择文件的替代方法

我所知道的:

我对如何做到这一点略知一二

'The folder containing the files to be recap'd
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialFileName = "G:\Work\" '<----- Change this to where the files are stored.
fd.InitialView = msoFileDialogViewList
'allow multiple file selection
fd.AllowMultiSelect = False
fd.Filters.Add "Excel Files", "*.xls*"
filechosen = fd.Show
'Create a workbook for the recap report
Set Master = ThisWorkbook
If filechosen = -1 Then
“包含要重述的文件的文件夹”
Set fd=Application.FileDialog(msoFileDialogFilePicker)

fd.InitialFileName=“G:\Work\”这是我通常用来强制用户选择需要打开的工作簿的代码:

Dim f As Object, fso As Object, flder As Object
Dim myfile As String
Dim wb As Workbook, ws As Worksheet
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Set fso = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFilePicker)
    .Show
    If .SelectedItems.Count = 0 Then
        MsgBox "Cancel Selected"
        End
    End If
    myfile = .SelectedItems(1)
End With
Set Wbk= Workbooks.Open(myfile)
或者,您可以将所有文件位置存储在第一张工作表的单元格中,这样一来,它们只需使用新文件位置更新电子表格,而不必每次都编辑代码或选择工作簿

要将其与您的代码集成,它将如下所示:

'lastCol = Worksheets("Dashboard").Cells(3, Columns.Count).End(xlToLeft).Column
lastCol = 15
lastrow = Worksheets("Dashboard").Cells(Rows.Count, 1).End(xlUp).Row
Set cmpRng = Range(Cells(1, 1), Cells(3, lastCol))
a = cmpRng
i = Cells(Rows.Count, 1).End(xlUp).Row

Dim f As Object, fso As Object, flder As Object
Dim myfile As String
Dim wb As Workbook, ws As Worksheet
Set wb = ActiveWorkbook ' this variable lets us go back to our original workbook
Set ws = ActiveSheet ' this variable lets us go back to our original sheet
Set fso = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFilePicker)
    .Show
    If .SelectedItems.Count = 0 Then
        MsgBox "Cancel Selected"
        End
    End If
    myfile = .SelectedItems(1)
End With
dim wbk as workbook ' use this variable to reference the workbook we're opening
dim ghws as worksheet ' this variable should reference the new sheet that gets opened
Set Wbk= Workbooks.Open(myfile)   
set ghws = activesheet

ghws.Select
Mastcol = Cells(1, Columns.Count).End(xlToLeft).Column
j = Cells(Rows.Count, 1).End(xlUp).Row
Set mastRng = Range(Cells(1, 1), Cells(1, Mastcol))
b = mastRng

For k = 1 To lastCol
    For n = 1 To Mastcol
        If UCase(a(3, k)) = UCase(b(1, n)) Then
        wbk.Activate
            ghws.Range(Cells(2, n), Cells(j, n)).Copy
            wbk.Activate
            Ws.Select
            Cells(i + 1, k).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            Exit For
        End If
    Next
Next
Call Wbk.Close(False)

嗨,保罗,这就是我要找的。。。。正如您在我的示例中看到的,我需要不断引用其他工作簿中的数据。。。。如果没有问题的话,你能告诉我如何引用它们吗?我明白了,但谢谢你确认:)for loop headcount是我希望打开的文件,dashboard是主文件,这些行让我被难住了吗?抱歉,我现在明白了,使用wbk.activate激活headcount,wb.activate返回到您原来的答案。编辑我的答案,以便更好地指导如何整合它,希望这对您有所帮助。是的,这有帮助,但我看到您使用了工作表名称global headcount来复制我希望避免。。。。可以换成单张还是别的什么?