你好我第21行的宏观波纹有问题,
它告诉我,一旦结束第一个循环,它就不会改变shtname字符串的值 我想知道代码发生了什么:
Sub lsConsolidarPlanilhas()
Dim lWorkbook As Workbooks
Dim lWorksheet As Worksheet
Dim lUltimaLinhaAtiva As Long
Dim lControle As Long
Dim lUltimaLinhaAtiva2 As Long
Dim lUltimaLinhaAtiva3 As Long
Dim lUltimaLinhaAtiva4 As Long
Dim shtname As String
Application.DisplayAlerts = False
Application.ScreenUpdating = False
lUltimaLinhaAtiva = Worksheets("Configuração").Cells(Worksheets("Configuração").Rows.Count, 1).End(xlUp).Row
lControle = 2
While lControle <= lUltimaLinhaAtiva
If (Workbooks("Macros.xlsm").Worksheets("Configuração").Range("B" & lControle).Value <> "") Then
shtname = Range("B" & lControle).Text
Worksheets.Add.name = shtname
End If
If (Workbooks("Macros.xlsm").Worksheets("Configuração").Range("B" & lControle).Value <> "") Then
Workbooks.Open Filename:=Worksheets("Configuração").Range("A" & lControle).Value
Set lworkbooks = ActiveWorkbook
If (ActiveWorkbook.Sheets.Count > 1) Then
Workbooks(lworkbooks.name).Worksheets("<LVC>").Activate
lUltimaLinhaAtiva2 = Worksheets("<LVC>").Cells(Worksheets("<LVC>").Rows.Count, 1).End(xlUp).Row
Worksheets("<LVC>").Select
Worksheets("<LVC>").Range("A1:AI18").Select
Selection.Copy
lUltimaLinhaAtiva3 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
For i = 1 To ActiveWorkbook.Sheets.Count
Workbooks(lworkbooks.name).Worksheets(i).Activate
lUltimaLinhaAtiva2 = Worksheets(i).Cells(Worksheets(i).Rows.Count, 1).End(xlUp).Row
Worksheets(i).Select
Worksheets(i).Range("A19:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva4 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
End If
Workbooks(lworkbooks.name).Worksheets("<LVC>").Activate
lUltimaLinhaAtiva2 = Worksheets("<LVC>").Cells(Worksheets("<LVC>").Rows.Count, 1).End(xlUp).Row
Worksheets("<LVC>").Select
Worksheets("<LVC>").Range("A1:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva3 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Else:
Workbooks.Open Filename:=Worksheets("Configuração").Range("A" & lControle).Value
Set lworkbooks = ActiveWorkbook
If (ActiveWorkbook.Sheets.Count > 1) Then
For i = 1 To ActiveWorkbook.Sheets.Count
Workbooks(lworkbooks.name).Worksheets(i).Activate
lUltimaLinhaAtiva2 = Worksheets(i).Cells(Worksheets(i).Rows.Count, 1).End(xlUp).Row
Worksheets(i).Select
Worksheets(i).Range("A19:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva4 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next
End If
Workbooks(lworkbooks.name).Worksheets("<LVC>").Activate
lUltimaLinhaAtiva2 = Worksheets("<LVC>").Cells(Worksheets("<LVC>").Rows.Count, 1).End(xlUp).Row
Worksheets("<LVC>").Select
Worksheets("<LVC>").Range("A19:AI" & lUltimaLinhaAtiva2).Select
Selection.Copy
lUltimaLinhaAtiva3 = Workbooks("Macros.xlsm").Worksheets(shtname).Cells(Workbooks("Macros.xlsm").Worksheets(shtname).Rows.Count, 1).End(xlUp).Row + 1
Workbooks("Macros.xlsm").Worksheets(shtname).Activate
Workbooks("Macros.xlsm").Worksheets(shtname).Range("A" & lUltimaLinhaAtiva3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Workbooks(lworkbooks.name).Close
lControle = lControle + 1
Wend
Worksheets("Configuração").Select
Worksheets("Configuração").Range("A1").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "Planilhas consolidadas!"
End Sub
答案 0 :(得分:0)
非常感谢@Cor_Blimey问题,一旦我为shtname指定了工作簿,它就完美了
“非限定Range()函数隐式引用可能不是预期的活动工作表。通过添加工作簿(”Macros.xlsm“)使工作表完全合格。工作表(”Configuração“)。在它前面。一般来说,我建议重构代码以消除所有.Select和.Activate,然后分配给临时工作表变量,使其透明并清除正在执行的操作(例如,设置sourceWb = ...设置sourceWs = sourceWb。工作表(1)......等 - Cor_Blimey 22小时前“