2011-12-16 34 views
2

我正試圖編寫一個快速的小宏,要求用戶輸入,然後將其複製到特定的單元格(Sheet1中的B14)。下面是我到目前爲止有:msgbox以特定格式要求用戶輸入

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 

我也想知道如果有一些方法可以讓我有一個檢查,以確保用戶輸入的是正確的格式,如果沒有,標誌了一個錯誤並要求用戶重新進入?

幫助非常感謝:)

回答

3

這樣的事情,你是非常接近的(而不是Inputbox你只需要編寫到工作表Sheet1 B14時使用vReply

更新去就去嗯:

  1. 使用Application.InputBox而不是'InputBox',因爲這爲編碼器提供了更多的選擇性。但很高興在這種情況下,而不是致命的
  2. 使用正則表達式來確保字符串的形式爲「Q [1-4]」,年份從2010-2020不等(更新至2011-2013使用"^Q[1-4]\s20[11-13]{2}$"該「q」的測試是不區分大小寫
  3. 我添加的「2011年第一季度」到calcuates使用currentd吃提示,Int((Month(Now()) - 1)/3) + 1 & " " & Year(Now())回報默認項2011年第四季度。如果需要的話
  4. A你可以刪除此提示Do循環用於測試無效字符串,如果提供的無效字符串比strTitle變量「請重試」「用於讓用戶知道之前的嘗試無效(msg不會顯示第一次通過作爲用戶還沒有犯錯誤)
  5. 按取消觸發一個單獨的退出消息,讓用戶知道代碼已終止早期

    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 
    
+0

謝謝,作品很好^^ – heavyarms 2011-12-16 10:56:22

+0

有沒有簡單的一行我可以添加incse用戶輸入q小寫? – heavyarms 2011-12-16 11:06:17

+0

我已經更新了代碼,以便msgbox將接受「2010年第二季度」或「2010年第二季度」,但會在「2010年第二季度」中輸入這兩種情況的結果 – brettdj 2011-12-16 11:31:12

3
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 
4

我有兩個較早的答案困難。

我同意驗證是必不可少的;如果用戶對提示認真考慮不周,用戶可能會輸入「2011-4」。檢查其格式是「Q#####」絕對是朝着正確方向邁出的一步。然而:

我會指出,這種檢查水平是不夠的。例如,「Q5 1234」會匹配這種格式。 「Q5 1234」會建議用戶試圖破壞系統,但「Q4 2101」是一個很容易犯的錯誤。

Like操作符是Excel 2003的唯一選擇,但對於更高版本,我建議考慮使用正則表達式。我一直在用VB 2010試驗它們。我不否認它們是一種理解的鬥爭,但它們爲你做了很多。或許重型汽車目前在他的盤子上有足夠的知識,但我仍然會建議查看一些關於其使用的問題。

正如前面的答案中所用,InputBox沒有達到重型槍械的目標。如果我輸入「Q4 2101」而不是「Q4 2011」,並且宏被增強以檢查不可能的日期,我不知道我的簡單錯誤,除非錯誤消息包含我輸入的值。此外,我無法將「Q4 2101」編輯爲我打算輸入的值。 InputBox的語法是vReply = InputBox(提示,標題,默認,...)。所以,如果我要推薦使用該運營商一樣的,我建議:

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 

最後,我很少使用的InputBox因爲形式,一旦掌握,是很容易創建和提供更多的控制權。