无模式UserForm不允许用户更改窗口

时间:2014-04-23 23:12:09

标签: vba userform modeless

我有一个脚本,它将循环浏览500多个文件,并使用查找和替换程序进行少量编辑。但是,如果找不到该字符串,我希望代码打开Modeless UserForm,以便进行手动编辑。我打算在打开UserForm后执行不同的子例程,但由于我使用的是For Each循环,我无法退出例程然后返回。

该脚本有效,除非打开Save_User时,其行为就像Modal一样。有两个窗口将打开,但我可以选择它们,并且任务栏中每个窗口的预览都是白色的。关于为什么会发生这种情况的任何想法?

Option Explicit
Public WB As Workbook, Template As Workbook
Public FindSelection As Range, ReplaceSelection As Range, FirstRow As Range, SecondRow As Range, StaticRange() As String
Public Target As Range, LastRow As Integer
Public FindRange As String, ReplaceRange As String, FindText As String, ReplaceText As String
Public Count As Integer, Updated As Integer
Public Const FilePath = "C:\GenericPath"

Public Sub AddApplicable()

Dim FSO As Object
Dim Folder As Object
Dim File As Object

Set FSO = CreateObject("Scripting.FileSystemObject")

Set Folder = FSO.GetFolder(FilePath)

Count = 0
Updated = 0

For Each File In Folder.Files
    If File.Type Like "Microsoft Excel*" Then

        Application.ScreenUpdating = False

        Set WB = Workbooks.Open(File.Path)

        Set FindSelection = WB.Worksheets("Repair Instruction").Range("K17:W40")
        Set ReplaceSelection = WB.Worksheets("Repair Instruction").Range("X17:AJ40")

        LastRow = WB.Worksheets("Repair Instruction").Range("K40:W40").End(xlUp).Row

        Set Target = FindSelection.Find(FindText, LookAt:=xlWhole)

        If Target Is Nothing Then
            If LastRow = 39 Then

                Save_User.Show vbModeless
                While Save_User.Visible
                    DoEvents
                Wend

                GoTo NextAdd

            Else
                If LastRow Mod 2 = 0 Then
                    WB.Worksheets("Repair Instruction").Range("K" & LastRow + 1, "W" & LastRow + 1) = FindText
                    WB.Worksheets("Repair Instruction").Range("X" & LastRow + 1, "AJ" & LastRow + 1) = _
                    Add_User.txtFirst.Value
                    WB.Worksheets("Repair Instruction").Range("X" & LastRow + 2, "AJ" & LastRow + 2) = _
                    Add_User.txtSecond.Value

                Else
                    WB.Worksheets("Repair Instruction").Range("K" & LastRow + 2, "W" & LastRow + 2) = FindText
                    WB.Worksheets("Repair Instruction").Range("X" & LastRow + 2, "AJ" & LastRow + 2) = _
                    Add_User.txtFirst.Value
                    WB.Worksheets("Repair Instruction").Range("X" & LastRow + 3, "AJ" & LastRow + 3) = _
                    Add_User.txtSecond.Value

                End If

                WB.Close True
                Updated = Updated + 1

            End If

        Else

            Set FirstRow = WB.Worksheets("Repair Instruction").Range("X" & Target.Row, "AJ" & Target.Row)
            Set SecondRow = WB.Worksheets("Repair Instruction").Range("X" & Target.Row + 1, "AJ" & Target.Row + 1)
            FirstRow.Value = Add_User.txtFirst.Value
            SecondRow.Value = Add_User.txtSecond.Value
            WB.Close True

        End If

    End If

NextAdd:
    Count = Count + 1
    Next File

    Unload Applicable_User
    Unload Add_User
    Unload Ref_User
    Unload Replace_User

MsgBox (Updated & " of " & Count & " files updated.")

End Sub

修改

大卫,谢谢你!我甚至不认为这是原因,但我很感激它相对简单!我不知道任何其他功能会隐藏循环Excel文件,所以我编辑If语句Save_User为:

 If LastRow = 39 Then
            Application.ScreenUpdating = True
            WB.Activate
            Save_User.Show vbModeless
            While Save_User.Visible
                DoEvents
            Wend

            Application.ScreenUpdating = False
            GoTo NextAdd
 ...
 End If

循环Excel窗口现在出现用于编辑,但我实际上无法进行任何更改(我的光标现在是十字准线,我可以选择单元格但不更改它们的值)。没有一本工作簿应该受到保护,那么我有什么遗漏吗?或者也许是在后台隐藏活动的更好方法(因为我不希望500多个文件出现并消失)?

1 个答案:

答案 0 :(得分:2)

这是你的罪魁祸首:

Application.ScreenUpdating = False

这可以防止用户输入。