[英]Only allow sub to run after another has been run VBA
I've created a workbook that imports data from another file and then deletes any rows by finding a header string "Time". 我创建了一个工作簿,该工作簿从另一个文件导入数据,然后通过查找标题字符串“ Time”删除任何行。 However, if the macro that deletes the rows is run again it will remove the header as there's a sub-header called "Time" as well.
但是,如果删除行的宏再次运行,它将删除标头,因为还有一个名为“ Time”的子标头。
OR is there a way to limit the characters Find searches for ie say my subheader is Real Time and my char limit is 4, find should only return "real" and therefore ignore that? 或有没有一种方法来限制字符的查找,即搜索说我的子标题是实时且我的字符数限制是4,查找应该只返回“真实”,因此可以忽略它?
What I want to do is disable the Row Delete code unless there has been a new data import and the Row Delete hasn't already been run. 我想做的是禁用行删除代码,除非有新的数据导入并且行删除尚未运行。
Some psuedo code below 下面的一些伪代码
If (DataImport has been run){
If (rowDelete has been run since DataImport){
return;
}
else{
run rowDelete
}
}
Excel VBA Excel VBA
Sub ImportData()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sheet As Worksheet
Dim pastestart As Range
Set wb1 = ActiveWorkbook 'Sheets("Data")
Set pastestart = [Data!A1]
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a data file")
If FileToOpen = False Then
MsgBox "No File Specified.", vbExclamation, "error"
Exit Sub
Else
Set wb2 = Workbooks.Open(Filename:=FileToOpen)
For Each sheet In wb2.Sheets
With sheet.UsedRange
.Copy pastestart
Set pastestart = pastestart.Offset(.Rows.Count)
End With
Next sheet
End If
wb2.Close
End Sub
_____________________________________________________________________
Sub rowDelete()
Dim FindRow As Range
On Error Resume Next
With Sheets("Data")
Set FindRow = Cells.Find(What:="Time", After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= _
xlPart, MatchCase:=False)
End With
On Error GoTo 0
Range("A1", FindRow).EntireRow.Delete
End Sub
You could include a cell on a worksheet which states whether an import has been made since the last row delete; 您可以在工作表上包括一个单元格,该单元格说明自上一行删除后是否进行了导入; include a line of code at the end of your
ImportData
procedure which updates this cell. 在
ImportData
过程的末尾添加一行代码,以更新此单元格。
ThisWorkbook.Sheets(1).Range("A1") = "Imported"
Then at the end of the rowDelete
procedure you can update this cell to reflect that the rows have been deleted. 然后,在
rowDelete
过程结束时,您可以更新此单元格以反映行已被删除。
ThisWorkbook.Sheets(1).Range("A1") = "Rows deleted"
Then within the rowDelete
procedure you can check if the last procedure to be run was rowDelete
. 然后,在
rowDelete
过程中,您可以检查最后要运行的过程是否是rowDelete
。 If it was, do not run the procedure. 如果是这样,请勿运行该过程。
If ThisWorkbook.Sheets(1).Range("A1") = "Rows deleted" Then
Exit Sub
End If
If the cell shows that the ImportData
procedure was run last (because the cell shows "Imported") then the rowDelete
procedure can run. 如果该单元格显示
ImportData
过程最后一次运行(因为该单元格显示“已导入”),则可以运行rowDelete
过程。
How about using CustomDocumentProperties instead. 如何改用CustomDocumentProperties。 Makes it more invisible:
使它更加不可见:
ThisWorkbook.CustomDocumentProperties("Imported")
You can make this a yes/no property 您可以将此属性设置为是/否
YOu could use global variables to identify, will the below work for you? 您可以使用全局变量来识别,以下内容对您有用吗?
Make sure you call InitiParams at the begining of the cycle 确保在周期开始时调用InitiParams
Dim Option Explicit
'Module Level Variables
Dim boolDataImportComplete as boolean
Dim boolRowDeleteRunComplete as boolean
Sub InitParams()
boolDataImportComplete = False
boolRowDeleteRunComplete = False
End Sub
Sub ImportData()
''' Your Code here
boolDataImportComplete = True
End Sub
Sub RowDelete()
''' YOUR Code here
boolRowDeleteRunComplete = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.