我有以下宏:
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" sub并手动打开工作簿然后运行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
答案 0 :(得分:2)
我对您的代码做了一些更改,程序中的注释意图解释更改。我的建议是,除了从Stackoverflow等网站获得的内容之外,您还应阅读相应的文档,以便更深入地了解所使用的概念和资源。然而,在你开发编程技巧的过程中,不要犹豫提问。
下面的代码合并了您在一个过程中尝试执行的所有操作,在那里您将看到如何为一系列值(即本例中的工作表)运行重复代码
建议访问以下页面:
Variables & Constants,Application Object (Excel),Excel Objects
With Statement,For...Next Statement,For Each...Next Statement, If...Then...Else Statement
Worksheets Object (Excel),Worksheet Object (Excel),Range Object (Excel)
Sub Remove_Junk_Data()
Rem Use an Array Variable to List all the worksheets you want to work with
Dim aWsh As Variant, vItm As Variant
aWsh = Array("Master", "Change History", "Update", "ExistingOwnership")
Rem Declare Object Variables
Dim Wbk As Workbook
Dim Wsh As Worksheet
Dim lRowLst As Long
Dim lRow As Long
Rem Open Workbook & Set Workbook Object Variable
Set Wbk = Workbooks.Open("https://company.sharepoint.com/sites/project/subproject/subsubproject/subsubprojecttool/tooloutput/tooloutput.xlsx")
Rem Loop throught the worksheet list and process each one
For Each vItm In aWsh
Rem Set Worksheet Object Variable
Set Wsh = Wbk.Worksheets(vItm)
With Wsh
lRowLst = Cells(Rows.Count, "A").End(xlUp).Row
For lRow = lRowLst To 1 Step -1
With .Cells(lRow, 1)
If .Value2 = Empty And Not (.HasFormula) Then .EntireRow.Delete
End With: Next: End With: Next
Application.DisplayAlerts = False
Wbk.Save
Application.DisplayAlerts = True
Workbooks("Master_FM_Update.xlsx").Close
End Sub