2013-12-16 58 views
1

我有一個由3個文本框和一個微調組成的日期的窗體。 我使用了3個文本框來解決國際日期問題,因此Excel無法根據不同的格式錯誤解釋預期日期。Excel VBA:如何使旋轉按鈕控制多個文本框?

無論如何... 當我單擊微調框時,下一個或上一個日期出現在3個框中。即如果是2013年2月28日,則將2013年3月1日。每次移動一天。

我的老闆要在此基礎上的文本框光標在它移動。所以,如果它是在txtMonth,日期應該從2013年2月28日至2013年3月28日,等

哪有我讓微調器檢測哪個文本框包含光標(如果有的話)?一旦我知道,我可以做數學。

Private Sub spinDate_SpinDown() 
    Dim bGoodDate As Boolean 
    Dim iMonth As Integer 

    Dim dDate As Date 
    If Not bIsValidDate(bGoodDate) Then ' Check that the 3 boxes make a valid date 
     GoTo ErrorExit 
    End If 
    iMonth = Month(DateValue("01-" & Me.txtMon & "-2014")) 
    dDate = DateValue(Me.txtDay & "-" & Me.txtMon & "-" & Me.txtYear) 

    dDate = DateAdd("d", -1, dDate) ' Decrement date 


    Me.txtDay = Format(Day(dDate), "00") 
    Me.txtMon = Format(dDate, "mmm") 
    Me.txtYear = Format(Year(dDate), "0000") 

    m_clsCRE.ETA = dDate 
    m_clsCRE.bChanged = True 
ErrorExit: 
End Sub 

*與Solution修訂BELOW ** 「該解決方案檢測光標是否是最後的一天,一個月或一年框。 「被選擇的量旋轉按鈕增量(日,月或年) 」這些箱子被連接,從而根據您選擇的是紡紗帶你到下一個有效日期

Private Sub spinDate_SpinDown() ' Same for SpinUp but used +1 
    Dim bGoodDate As Boolean 

    Dim dDate As Date 
    If Not bIsValidDate(bGoodDate) Then ' check that date user typed in boxes is valid 
     GoTo ErrorExit 
    End If 

    dDate = DateValue(Me.txtDay & "-" & Me.txtMon & "-" & Me.txtYear) 
    If Len(msDatePart) = 0 Or StrComp(msDatePart, "DAY", vbTextCompare) = 0 Then 
     dDate = DateAdd("d", -1, dDate) 
    ElseIf StrComp(msDatePart, "MONTH", vbTextCompare) = 0 Then 
     dDate = DateAdd("m", -1, dDate) 
    Else ' Year 
     dDate = DateAdd("yyyy", -1, dDate) 
    End If 


    Me.txtDay = Format(Day(dDate), "00") 
    Me.txtMon = Format(dDate, "mmm") 
    Me.txtYear = Format(Year(dDate), "0000") 

    m_clsCRE.ETA = dDate ' Save real date value (not text form) 
    m_clsCRE.bChanged = True 
ErrorExit: 
End Sub 

Private Sub txtDay_Enter() 
    ' Used with calendar spinner 
    msDatePart = "DAY" 
End Sub 
Private Sub txtMon_Enter() 
    ' Used with calendar spinner 
    msDatePart = "MONTH" 
End Sub 
Private Sub txtYear_Enter() 
    ' Used with calendar spinner 
    msDatePart = "YEAR" 
End Sub 
+1

順便說一句,你見過[這個](http://stackoverflow.com/questions/12012206/formatting-mm-dd-yyyy-dates-in-textbox-in-vba/12013961#12013961) –

+0

Siddharth,I下載你的日曆選取器並與之一起玩。做了一些我在另一篇文章中描述的改變。這非常有用!我可能最終會在這個項目或下一個項目中使用它。 –

回答

1

的名稱試試這個:

該代碼會檢測到最後TextBox具有焦點之前按SpinButton
請注意,Textbox或任何其他物體會在您將焦點設置在另一個物體上時失去焦點。所有文本框

Option Explicit 
Public LastActiveObject As String 'Declare a public variable to store the last active object name 

Private Sub SpinButton1_SpinDown() 'Here you test which Textbox was last active 

If LastActiveObject = "TextBox1" Then MsgBox "TB1" 
If LastActiveObject = "TextBox2" Then MsgBox "TB2" 
If LastActiveObject = "TextBox3" Then MsgBox "TB3" 

End Sub 

添加退出事件來存儲它的名字在LastActiveObject可變

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) 

LastActiveObject = TextBox1.Name 

End Sub 

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean) 

LastActiveObject = TextBox2.Name 

End Sub 

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean) 

LastActiveObject = TextBox3.Name 

End Sub 

希望這有助於你一點來完成你想要的東西。

+0

感謝L42和@ sam092,祝您有個美好的回答! –

1

我不知道如何檢測當前焦點對象。

作爲初步的解決方法,您可能會在下面看到我的代碼。我們不檢測焦點對象,而是檢測文本框何時由enter事件「輸入」。然後,我們使用全局變量來存儲文本框

Option Explicit 
'Declare a global variable here 
Dim onFocus As String 

Private Sub SpinButton1_SpinDown() 
    Dim obj As Object 
    Set obj = Controls(onFocus) 
    obj.Value = obj.Value + 1 
End Sub 

Private Sub TextBox1_Enter() 
    onFocus = "TextBox1" 
End Sub 

Private Sub TextBox2_Enter() 
    onFocus = "TextBox2" 
End Sub 

Private Sub TextBox3_Enter() 
    onFocus = "TextBox3" 
End Sub 

Private Sub UserForm_Initialize() 
    Controls("Textbox1").SetFocus 
End Sub