【发布时间】:2015-11-17 20:10:47
【问题描述】:
我有以下宏:
Sub Remove_Junk_Data()
Call Open_Workbook
Call Scrub_Master
Call Scrub_Change_History
Call Scrub_Update
Call Scrub_ExistingOwnership
Call Save_Scrubbed
End Sub
Sub Open_Workbook()
Workbooks.Open "https://company.sharepoint.com/sites/project/subproject/subsubproject/subsubprojecttool/tooloutput/tooloutput.xlsx"
Workbooks("tooloutput.xlsx").Activate
End Sub
Sub Scrub_Master()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("Master").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Scrub_Change_History()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("Change History").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Scrub_Update()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("Update").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Scrub_ExistingOwnership()
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
Sheets("ExistingOwnership").Select
For i = LastRow To 1 Step -1
Set r = Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End Sub
Sub Save_Scrubbed()
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:= _
"https://company.sharepoint.com/sites/project/subproject/subsubproject/subsubprojecttool/tooloutput/tooloutput.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = False
Workbooks("Master_FM_Update.xlsx").Close
End Sub
如果我注释掉“Open_Workbook”子并手动打开工作簿,然后运行 Remove_Junk_Data() 的其余部分,它可以完美运行。
如果我尝试在 Open_Workbook 处于活动状态的情况下运行 Remove_Junk_Data,则不会引发任何错误,但中间的 4 个潜艇不会做任何事情...
有没有人遇到过这样的事情?你找到解决方案了吗?我想点击一个按钮,让所有 6 个潜艇都正确地做他们的事情......
编辑:有了输入,新的宏,它就可以工作了!谢谢大家!:
Sub Remove_Junk_Data()
Workbooks.Open "https://company.sharepoint.com/sites/project/subproject/subsubproject/Subsubprojecttool/tooloutput/tooloutput.xlsx"
Dim myValue As String
Dim LastRow As Long
Dim i As Long
Dim r As Range
With Workbooks("tooloutput.xlsx").Sheets("Master")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
With Workbooks("tooloutput.xlsx").Sheets("Change History")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
With Workbooks("tooloutput.xlsx").Sheets("Update")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
With Workbooks("tooloutput.xlsx").Sheets("ExistingOwnership")
LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
myValue = ""
For i = LastRow To 1 Step -1
Set r = .Cells(i, 1)
If r.Value = myValue Then r.EntireRow.Delete
Next i
End With
End Sub
【问题讨论】:
-
您将需要限定宏应针对每个子操作的确切工作簿和工作表。它现在的工作方式是代码将针对运行代码的工作簿运行。 (除了
workbook_opensub ...例如,LastRow将始终在运行宏时找到工作表中的最后一行。如果该工作表没有数据,则您的行将为 1 并且那些永远不会发生循环。 -
"中间的 4 个子程序什么都不做" - 你有没有逐行运行代码来查看代码在做什么 [在 VBA 模块中,一次按 f8 一行] ?它在哪一行不再按预期/预期运行?
-
@Scott Holtzman 所以我会在每个子的开头而不是在第一个子的末尾使用“Workbooks("tooloutput.xlsx").Activate”来限定工作簿?
-
或者看看one question down^_^;
-
你也有所有做同样事情的潜艇。为什么不创建 1 个 sub 并将工作簿和工作表名称作为参数传递给您要在调用所有这些 sub 的主 sub 中
scrub的每个工作表?