vba - 你能中断vba代码来选择工作表吗?

标签 vba excel

我会尽力在描述中尽可能清晰,所以这里什么都没有:

我创建了一个代码,用户在其中选择他的 Excel 文件,然后宏将 Sheet 从该文件复制到我的宏 Workbook 中。

MyFile = Application.GetOpenFilename()
    Workbooks.Open (MyFile)
        ActiveSheet.Copy After:=wbook.Sheets(1)
        ActiveSheet.Name = "Selected file"
    Workbooks.Open (MyFile)
ActiveWorkbook.Close SaveChanges:=False

这是有效的,但我意识到,有时所选文件可能有多个工作表

有没有一种方法可以编写宏,如果我选择的文件有一张工作表,它会运行上面的代码,如果它有多个工作表,让我选择我想要的工作表,然后运行其余的代码?

最佳答案

编辑:

  • 我想到了另一种方法来处理这个问题 - 也许更接近您正在寻找的方法。 。 。

    img

这只是我偶尔使用的基本暂停例程的扩展。

这是我的“常规”Pause例程(使用 Timer function):

Sub Pause(seconds As Single)
    Dim startTime As Single
    startTime = Timer                          'get current timer count
    Do
        DoEvents                               'let Windows "catch up"
    Loop Until Timer > startTime + seconds     'repeat until time's up
End Sub

...所以,它给了我一个想法。

老实说,我有点惊讶地发现这有效,因为它基本上同时运行两段代码

<小时/>

代码 WaitForUserActivity :

这是我在上面的演示中使用的代码:

Option Explicit
Public isPaused As Boolean

Sub WaitForUserActivity()                   'THE 'RUN DEMO' BUTTON runs this sub.

    Dim origSheet As String
    isPaused = True                                   'flag "pause mode" as "on"
    origSheet = ActiveSheet.Name                'remember current worksheet name

    MsgBox "This will 'pause' code execution until you" & vbLf & _
           "click the 'Continue' button, or select a different a worksheet."
    Application.StatusBar = "PAUSED: Click ""Continue"", or select a worksheet."

    Do                                       'wait for button click or ws change
        DoEvents        'yield execution so that the OS can process other events
    Loop Until (Not isPaused) Or (ActiveSheet.Name <> origSheet)

    If isPaused Then                           'the active worksheet was changed
        MsgBox "Worksheet '" & ActiveSheet.Name & "' was selected." _
              & vbLf & vbLf & "Now the program can continue..."
    Else                                                 'the button was clicked
        MsgBox "The 'Continue' button was clicked." _
              & vbLf & vbLf & "Now the program can continue..."
    End If
    Application.StatusBar = "Ready"
End Sub

Sub btnContinue()                          'THE 'CONTINUE' BUTTON runs this sub.
    isPaused = False                                 'flag "pause mode" as "off"
End Sub

运行演示:

  • 将上述代码放入常规模块
  • 确保工作簿至少有两个工作表
  • 创建两个命令按钮:
    • 一个用于“运行演示”按钮,指定宏:WaitForUserActivity
    • 一个用于“继续”按钮,指定宏:btnContinue
  • 点击“运行演示”按钮
<小时/>

代码中的关键命令是 DoEvents Function ,它“让出执行,以便操作系统可以处理其他事件。”

DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.

DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component - and the operating system takes care of multitasking and time slicing.

Any time you temporarily yield the processor within an event procedure, make sure the procedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results.

Further details (and warnings) at the source.

<小时/> <小时/>

原始答案:

一些建议的解决方案:

  1. 您可以提示用户指定哪个工作表,而不是“停止”代码。

    • 最简单的方法是使用 InputBox 用户可以在其中输入 ID 号或以其他方式识别工作表。

    • 更复杂但更强大且更专业的外观是在用户窗体的帮助下的自定义对话框。网上有几个示例和教程,例如 this one .

  2. 您可以通过简单的计时器循环“暂停”执行,为用户提供一定的时间来选择工作表,您甚至可以检查工作表名称以查看用户是否选择了新的工作表,如下所示:

    Dim startTime As Single, shtName As String
    If ThisWorkbook.Worksheets.Count = 1 Then
        MsgBox "There is only one worksheet in this workbook."
    Else
        shtName = ActiveSheet.Name 'get name of active sheet
        MsgBox "You have 5 seconds to select a worksheet after clicking OK.", _
            vbOKOnly + vbInformation, "Select a worksheet... fast!"
        startTime = Timer
        Do
            DoEvents
        Loop Until Timer > startTime + 5
    
        'check if user picked a new worksheet
        If ActiveSheet.Name = shtName Then
            MsgBox "You didn't select a new worksheet!"
        Else
            MsgBox "Thanks for selecting a new worksheet!"
        End If
    End If
    

    这有点做作,但可以工作,特别是如果进行适当的检查以确保您现在拥有正确的工作表。

  3. 我想您可以创建一个工作表事件过程,该过程将在激活工作表时运行,并检查全局变量以查看您的“导入过程”是否 正在运行,如果是这样,恢复您的代码...但这会很困惑且令人困惑,并且需要代码存在于您正在“导入”的工作簿中。

  4. 或者,比其中任何一个都更好是根据工作表的内容以编程方式/逻辑地确定您需要哪个工作表。有标题吗?某个日期?也许是最新的工作表?某个细胞里有什么东西?一定有一些东西与其他的不同。

希望这能为您提供一些关于非线性解决方案的想法。 😉

关于vba - 你能中断vba代码来选择工作表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52531785/

相关文章:

vba - 如何使用 VBA 仅替换文件名中的日期?

vba - 如何在 VBA (Mac) 中将文件保存到桌面

VBA 无法实现 IComparable.CompareTo

python - 如何更正导致列集中出现 "shifted"数据的缺失单元格/缺失分隔符?

Excel - 工作簿很慢并且经常崩溃

excel - 运行时错误13类型错误匹配VBA Excel 2010

vba - 无法让 Application.Run 工作

vba - 使用 IIf 时出现语法错误

python - 使用 python pandas 或任何其他更好的库在工作表中添加一列并在整列中应用此 excel 公式

java - 诡异的 Java 反射错误