你可以试试这个填充一个名为Sheet3
有两个字符串表的A1
[更新按msgbox asking for user input in specific format是防弹使用正则表达式]
Option Explicit
Sub Rattle_and_hmmmm2()
Dim strReply As String
Dim strTitle As String
Dim objRegex As Object
Dim objRegMC As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.ignorecase = True
.Pattern = "^Q([1-4])\s20[10-20]{2}$"
Do
If strReply <> vbNullString Then strTitle = "Please retry"
strReply = Application.InputBox("Enter period (format: Q4 2010) to update, or hit enter to escape", strTitle, "Q" & Int((Month(Now()) - 1)/3) + 1 & " " & Year(Now()), , , , , 2)
If strReply = "False" Then
MsgBox "User hit cancel, exiting code", vbCritical
Exit Sub
End If
Loop Until .test(strReply)
Set objRegMC = .Execute(strReply)
End With
Select Case objRegMC(0).submatches(0)
Case 2, 4
Sheets("Sheet3").[a1] = "insert text2"
Case 1, 3
Sheets("Sheet3").[a1] = "insert text1"
End Select
Sheets("Sheet1").[b14].Value = UCase$(strReply)
End Sub
如果你看一下我的回答你刚才的问题,你会看到,我剥出牛逼他每季度和每年对整数变量进行整数化处理,以便在使用前进行检查。 – 2011-12-16 16:09:17
下面的正则表达式现在可以确保这个季度是正确的(1-4)[先前的代码做到了这一点],并且年份范围是2010-2020在一次拍摄中[之前的代码没有这样做] – brettdj 2011-12-17 05:21:23