我想創建一個小的輸入框功能,用戶的輸入只能是一年中的幾個月(一月、二月、三月等),我想這可以通過利用一個變數(months)來實作。然而,我得到了一個型別不匹配的錯誤,我想實作的目標是否可以實作? 謝謝。
Sub test()
Dim NewName As Variant, Months As Variant
Months = Split("January,February,March,April,May,June,July,August,September,October,November,December")
Re_Enter_NewName:
NewName = InputBox("Please Write Month- Case Sensitive", "Month", vbOKCancel)
For Each Months In NewName ''Type MismatchIf NewName = Months Then
Exit Sub
ElseIf NewName <> Months Then
MsgBox "請輸入一年中的一個月份"。
GoTo Re_Enter_NewName:
Else
End If
下一個月
End Sub
uj5u.com熱心網友回復:
我將使用兩個函式:
一個函式來創建包含有效月份的陣列--請查看下面的代碼:你必須使用陣列而不是分割。或者,你可以從作業表中讀取有效的月份名稱,或者自動創建串列等。一個通用函式來檢查一個值是否在一個陣列內
Option Explicit
Sub testGetMonthsName()
Dim MonthSelected As Variant, arrMonths As Variant
arrMonths = getMonthArray
Re_Enter_NewName:
MonthSelected = InputBox("Please Write Month- Case Sensitive", "Month")
If MonthSelected = vbNullString Then.
'取消或為空。
Exit Sub
ElseIf isValueInArray(MonthSelected, arrMonths) Then
'MonthSelected是有效的。
Exit Sub
Else[/span]'MonthSelected is not valid[/span]。
MsgBox "Please Enter a Month of the Year" GoTo Re_Enter_NewName:
End If
結束 子
Private Function getMonthArray() As Variant
getMonthArray = Array("January", "February", "March", "四月"/span>, "五月"/span>, "六月"/span>。"七月"/span>, "八月"/span>, "九月"/span>。"十月"/span>, "十一月"/span>, "十二月"/span>)
結束 功能
Private Function isValueInArray(value As Variant, arrValues As Variant) As Boolean
Dim i As Long
For i = LBound(arrValues) To UBound(arrValues)
If arrValues(i) = value Then
isValueInArray = 真
退出 為
End If
下一步 下一步
結束 功能
基于@ChristanBuse的評論--一個更高級的版本:
Option Explicit
Sub test_getMonthFromUser()
Dim strMonth As String, cancelAs Boolean
做
strMonth = getMonthNameFromUser(取消)
If cancel = True Then Exit Sub
If isMonthNameValid(strMonth) = False Then
If MsgBox("Please enter a valid month name", vbOKCancel vbExclamation) = vbCancel 然后
退出 做
Else
strMonth = vbNullString
End If
End If
Loop Until LenB(strMonth) > 0
IfLenB(strMonth) > 0 Then
MsgBox "選擇的有效月份。" & strMonth
結束 若
End Sub
Private Function getMonthNameFromUser(ByRef 取消 As Boolean) As String As
Dim strMonth As String
strMonth = InputBox("Please Write Month- Case Sensitive", "Month")
如果StrPtr(strMonth) = 0 Then
cancel = True Then
Exit Function[/span
End If
getMonthNameFromUser = strMonth
結束 功能
Private Function isMonthNameValid(strMonth As String) AsBoolean
'將根據系統語言檢查。
'e.g. in German: Januar - in English: 一月
Dim i AsLong
For i = 1 To 12
If MonthName(i) = strMonth Then
isMonthNameValid = True[/span]。
退出 為
End If
下一步 下一步
結束 功能
uj5u.com熱心網友回復:
你還應該處理用戶取消輸入框的情況,以防止無休止的作業
Sub test()
Dim NewName As Variant, Months As Variant, mname, UserMonth
UserMonth = ""/span>
'Months = Split("January,February,March,April,May,June,July,August,September,October,November,December")
'Months is Variant/String array (1 element)
'請務必指定分隔符(逗號)。
Months = Split( _
"一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月", _
",")
'months is Variant/String array (12 elements)。
Re_Enter_NewName:
NewName = InputBox("Please Write Month- Case Sensitive", "MONTH" , vbOKCancel)
'For Each Months In NewName 'Type Mismatch: for each array in string.
For Each mname In Months
If NewName = mname Then
UserMonth = mname
End If
Next mname
If UserMonth = ""/span> Then
MsgBox "Please Enter a Month of the Year"
GoTo Re_Enter_NewName:
End If
結束 子
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307513.html
標籤:
