Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
Vba 查找上次使用的列时出现运行时错误_Vba_Excel - Fatal编程技术网

Vba 查找上次使用的列时出现运行时错误

Vba 查找上次使用的列时出现运行时错误,vba,excel,Vba,Excel,我有一个excel VBA代码,用于合并文件夹工作簿中的工作表。首先,它希望将所有单元格从第一张工作表复制到输出工作表。下一张工作表之后,它希望从第二行复制到最后使用的行。输入表的列标题的顺序可能不同。它在调试下一行以查找最后使用的列时显示了一个自动错误 **lco = ws2.Cells(1, Columns.Count).End(xlToLeft).Column** 整个代码如下所示: Application.ScreenUpdating = False directory = "

我有一个excel VBA代码,用于合并文件夹工作簿中的工作表。首先,它希望将所有单元格从第一张工作表复制到输出工作表。下一张工作表之后,它希望从第二行复制到最后使用的行。输入表的列标题的顺序可能不同。它在调试下一行以查找最后使用的列时显示了一个自动错误

   **lco = ws2.Cells(1, Columns.Count).End(xlToLeft).Column** 
整个代码如下所示:

Application.ScreenUpdating = False
directory = "C:\Users\Desktop\MYExcel\Input\"
fileName = Dir(directory & "*.xl??")
  i = 0
  j = 0
     'create new output file
   Set Wk = Workbooks.Add
            With Wk
                .Title = "All Sheets"
                .SaveAs fileName:="C:\Users\Desktop\MYExcel\Output\AllSheets.xlsx"
                .Close
             End With

 Do While fileName <> ""
           If i = 0 Then
           Set x = Workbooks.Open(directory & fileName) 'Opening the first workbook in directory
           Set y = Workbooks.Open("C:\Users\Desktop\MYExcel\Output\AllSheets.xlsx") 'opening the output workbook
     Set ws2 = y.Sheets(1)
        If j = 0 Then
              Set ws1 = x.Sheets(1)

                With ws1
                    .Cells.Copy ws2.Cells 'Copying all cells to output sheet for s
                    y.Close True
                    'x.Close False
                End With
                j = j + 1
        End If
        If j > 0 Then
            For Each sheet In x.Worksheets
                'Set ws2 = y.Sheets(1)
               ' lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column
                 lci = sheet.Cells(1, Columns.Count).End(xlToLeft).Column
                 **lco = ws2.Cells(1, Columns.Count).End(xlToLeft).Column**
                 lri = sheet.Range("A65536").End(xlUp).Row
                 lro = ws2.Range("A65536").End(xlUp).Row
                For Each cell In rng
                    For Each cell2 In rng2
                         l = ActiveCell.Column
                        If cell.Value = cell2.Value Then
                            With sheet
                                .Cells(cell, 2).EntireColumn.Copy ws2.Cells(cell2).Range(lro)
                            End With
                        End If
                    Next cell2
                Next cell
            Next sheet
        End If
    Workbooks(directory & fileName).Close
    fileName = Dir()
    i = i + 1

Else

    Set d = Workbooks.Open(directory & fileName)
    Set f = Workbooks.Open("AllSheets.xls*")
    'Windows("Book3.xlsm").Activate
    For Each sheet In x.Worksheets
            Set ws4 = f.Sheets(1)
             lci = sheet.Cells(1, sheet.Columns.Count).End(xlToLeft).Column
             lco = ws4.Cells(1, ws4.Columns.Count).End(xlToLeft).Column
             lri = sheet.Range("A65536").End(xlUp).Row
             lro = ws4.Range("A65536").End(xlUp).Row

            Set rng = sheet.Range("A1:A" & lci)
            Set rng2 = ws4.Range("A1:A" & lco)

            For Each cell In rng
                For Each cell2 In rng2
                     l = ActiveCell.Column
                    If cell.Value = cell2.Value Then
                         With sheet
                            .Cells(cell, 2).EntireColumn.Copy ws4.Cells(cell2).Range(lro)
                         End With
                    End If
                Next cell2
            Next cell
    Next sheet
 End If
Loop

用于获取LastColumn的代码是绝对正确的。它在我这边起作用

只需检查您是否对该行进行了注释

Set ws2 = y.Sheets(1)

请取消对该工作簿的注释并进行检查,它必须工作。

请向我们显示错误消息。在该行上画一个分隔符,然后进行检查,以确保工作簿已打开,并且ws2已设置为正确的工作表和工作簿。@RonRosefeld您能告诉我将“ws2”设置为活动工作簿活动工作表的代码以及它要设置的位置吗?