msgbox以特定格式要求用户input

我试图写一个快速的小macros,要求用户input,然后将其复制到特定的单元格(Sheet 1中的B14)。 这是迄今为止我所得到的:

Option Explicit Sub updatesheet() Dim vReply As String vReply = InputBox("Enter period (format: Q4 2010) to update, or hit enter to escape") If vReply = vbNullString Then Exit Sub Sheets("Sheet1").Activate ActiveSheet.Range("B14").Value = vReply End Sub 

我也想知道是否有一些方法可以包括一个检查,以确保用户input是正确的格式,如果没有,标志了一个错误,并要求用户重新input?

帮助非常感谢:)

像这样,你是非常接近的(而不是写入Sheet1 B14时只需使用vReply

更新大修去嗯:

  1. 使用Application.InputBox而不是“InputBox”,因为这为编码器提供了更多的select性。 但在这种情况下,而不是critcal很高兴
  2. 使用正则expression式确保string格式为“Q [1-4]”,年份从2010-2020年不等(更新至2011-2013使用"^Q[1-4]\s20[11-13]{2}$" 。”q“testing是不区分大小写的
  3. Int((Month(Now()) - 1) / 3) + 1 & " " & Year(Now())返回的计算结果中添加了“Q1 2011”的默认条目2011年第四季度 如果需要,您可以删除此提示
  4. Do循环用于testing无效的string,如果提供了一个无效的string,而不是strTitlevariables“Please retry”,用于让用户知道之前的尝试是无效的(msg不会显示第一次通过as用户还没有犯错误)
  5. 按下Cancel会触发一个单独的退出消息,让用户知道代码已经提前终止

      Option Explicit Sub Rattle_and_hmmmm() Dim strReply As String Dim strTitle As String Dim objRegex 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) End With Sheets("Sheet1").[b14].Value = UCase$(strReply) End Sub 

我对之前的答案都有困难。

我同意validation是至关重要的。 如果用户对提示不够深入,可能会input“2011-4”。 检查它的格式是“Q#####”绝对是正确的一步。 然而:

我会指出,这种检查水平是不够的。 例如,“Q5 1234”将匹配这种格式。 “Q5 1234”会build议用户试图打破系统,但“Q4 2101”是一个很容易的错误。

Like运算符是Excel 2003的唯一select,但是对于更高版本,我build议考虑使用正则expression式。 我一直在用VB 2010来试用。我不否认他们是一种理解的斗争,但他们为你做了这么多。 或许重型汽车目前在他的盘子上有足够的学习,但是我仍然会build议看一些最近关于它们使用的问题。

正如在前面的答案中所使用的那样,InputBox没有实现重武器的目标。 如果我input了“Q4 2101”而不是“Q4 2011”,并且macros被增强以检查不可能的date,我不知道我的简单错误,除非错误消息包含我input的值。 此外,我不能编辑“Q4 2101”到我打算input的值。 InputBox的语法是vReply = InputBox(提示,标题,默认,…)。 所以如果我要推荐使用Like运算符,我会build议:

 Sub updatesheet() Dim vReply As String Dim Prompt As String Dim Title As String Dim UpdateQuarter As Integer Dim UpdateYear As Integer ' I have found users respond better to something like "Qn ccyy" Prompt = "Enter period (format: Qn ccyy) to update, or hit enter to escape" ' I find a title that gives context can be helpful. Title = "Update sheet" vReply = InputBox(Prompt, Title) Do While True ' I have had too many users add a space at the end of beginning of a string ' or an extra space in the middle not to fix these errors for them. ' Particularly as spotting extra spaces can be very difficult. vReply = UCase(Trim(VReply)) vReply = Replace(vReply, " ", " ") ' Does not cater for three spaces If Len(vReply) = 0 Then Exit Sub If vReply Like "Q# ####" Then ' I assume your macro will need these value so get them now ' so you can check them. UpdateQuarter = Mid(vReply, 2, 1) UpdateYear = Mid(vReply, 4) ' The check here is still not as full as I would include in a macro ' released for general use. I assume "Q4-2011" is not valid because ' the quarter is not finished yet. Is "Q3-2011" available yet? I ' would use today's date to calculate the latest possible quarter. ' I know "You cannot make software foolproof because fools are so ' ingenious" but I have learnt the hard way that you must try. If UpdateQuarter >= 1 And UpdateQuarter <= 4 And _ UpdateYear >= 2009 And UpdateYear <= 2012 Then Exit Do Else ' Use MsgBox to output error message or include it in Prompt End If Else ' Use MsgBox to output error message or include it in Prompt End If vReply = InputBox(Prompt, Title, vReply) Loop End Sub 

最后,我很less使用InputBox,因为Forms一旦掌握,就很容易创build并提供更多的控制。

 Sub updatesheet() Dim vReply As String Do 'edit: added UCase on INputBox vReply = UCase(InputBox("Enter period (format: Q4 2010) to update, or hit enter to escape")) Loop Until Len(vReply) = 0 Or vReply Like "Q# ####" If vReply = vbNullString Then Exit Sub 'continue... End Sub