IF 语句和输入框检查验证用户输入

IF statement and inputbox check validate user input

我有一段代码要求用户通过在输入框中输入名字来select一个sheet,然后我需要检查select输入的名字是否正确.

如何将 "if" 语句写回 return 回到输入框?

我在 Windows 7 中使用 MS Word。这是代码:

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Sub OpenExcelFile()

    Dim oExcel As Excel.Application
    Dim oWB As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim oneRange As Excel.Range
    Dim aCell As Excel.Range
    Dim intChoice As Integer
    Dim strPath As String
    Dim uiSheet As String

    Set oExcel = New Excel.Application

    'Select the start folder
    Application.FileDialog(msoFileDialogOpen _
    ).InitialFileName = ActiveDocument.path
    'Remove all other filters
    Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
    'Add a custom filter
    Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
    "Only Excel File Allowed", "*.xl*")
    'only allow the user to select one file
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        strPath = Application.FileDialog( _
            msoFileDialogOpen).SelectedItems(1)
    End If
    'open excel file and select sheet
    Set oWB = oExcel.Workbooks.Open(strPath)
    Dim strBuild As String
    'set Array for user input control
    Dim myArray() As Variant
    ReDim myArray(1 To oWB.Sheets.Count)

    'populate input box and array
    For Each xlSheet In oWB.Worksheets
        strBuild = strBuild & xlSheet.Name & vbCrLf
        For i = 1 To oWB.Sheets.Count
            myArray(i) = oWB.Sheets(i).Name
        Next i
    Next xlSheet

    'show inputbox with list of sheets
    strBuild = Left$(strBuild, Len(strBuild) - 2)

    uiSheet = InputBox("Provide a sheet name." & vbNewLine & strBuild)

    'check if User input match with sheet name
    If IsInArray(uiSheet, myArray) Then
        'show excel window
        oExcel.Visible = True

        'sort selected sheet by first column range
        oExcel.Worksheets(uiSheet).Activate
        Set oneRange = oExcel.Range("A1:A150")
        Set aCell = oExcel.Range("A1")
        oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
    Else
        MsgBox "Please enter a valid name!", vbCritical
    End If

End Sub

如果您将以 uiSheet = InputBox(..... 开头的代码替换为下面的代码,它应该可以工作。

'check if User input match with sheet name
Dim bSheetPresent As Boolean
bSheetPresent = False

Do Until bSheetPresent
    uiSheet = InputBox("Provide a sheet name." & vbNewLine & strBuild)
    If uiSheet = "" Then Exit Do
    If IsInArray(uiSheet, myArray) Then
        bSheetPresent = True
    Else
        MsgBox "Please enter a valid name!", vbCritical
    End If
Loop

If bSheetPresent Then
    'show excel window
    oExcel.visible = True

    'sort selected sheet by first column range
    oExcel.Worksheets(uiSheet).Activate
    Set oneRange = oExcel.Range("A1:A150")
    Set aCell = oExcel.Range("A1")
    oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes
End If

如果用户在输入框上按下取消,它将退出循环。

您也可以考虑构建一个带有预填充组合框的表单。这样用户就不会出错。

要使用工作表列表创建您自己的输入框,您可以这样做:

  1. 在 VBE 中创建用户窗体
  2. 命名为frmSelectSheet
  3. 在其上放置一个名为 cbSheets
  4. 的组合框
  5. 将以下代码添加到用户窗体的代码窗格中:

    Private Sub UserForm_Initialize()
    
        Dim oSheet As Worksheet
    
        For Each oSheet In ThisWorkbook.Sheets
            Me.cbSheets.AddItem oSheet.Name
        Next oSheet
    
    End Sub
    
    Private Sub cbSheets_Change()
    
        Me.Hide
    
    End Sub
    
  6. 添加一个模块并在其中添加以下代码:

    Public Function SheetInputBox() As String
    
        Dim ofrmSheetInput As New frmSelectSheet
    
        ofrmSheetInput.Show
        SheetInputBox = ofrmSheetInput.cbSheets
        Unload ofrmSheetInput
    
    End Function
    
  7. 像这样调用函数

? SheetInputBoxuiSheet = SheetInputBox