简体   繁体   English

VBA仅允许子程序在另一个子程序运行之后运行

[英]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.

相关问题 VBA 有一个子运行另一个子 - VBA have a sub run another sub 仅在之前未运行过宏的情况下运行它吗? - Run a macro only if it has not been run before? 我可以根据另一个 Sub 是否运行来运行一个 Sub 吗? - Can I run a Sub based on whether another Sub has run? Excel Advanced筛选器运行速度很慢,但是仅在运行自动筛选器之后 - Excel Advanced Filter Very slow to run, but only after autofilter has been run VBA字典 <out of context> 在Sub运行之后 - VBA dictionary <out of context> after Sub is run 我们可以通过单击具有公式的单元格来运行vba sub吗? - Can we run vba sub by clicking a cell which has a formula? 在清除工作表中的所有单元格后,不要运行Sub Worksheet_Change(ByVal Target As Range) - Don't run Sub Worksheet_Change(ByVal Target As Range) after all cells in the worksheet has been cleared 如何控制VBA仅允许1个application.ontime实例运行 - How to control VBA to only allow 1 instance of application.ontime to run 如何使一个功能仅在写入文件后运行? (带有excel.js库的node.js) - How can I make one function only run after a file has been written ? (node.js with excel.js library) VBA关闭已经使用另一个子错误文件名错误打开的word文档 - VBA Closing a word document which has already been opened using another sub, bad file name error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM