Excel VBA:如何使旋转button控制多个文本框?

我有一个由3个文本框和一个微调组成的date的表格。 我使用了3个文本框来解决国际date的问题,所以Excel不能根据不同的格式错误地解释预期的date。

无论如何…当我点击微调,下一个或上一个date出现在3个框中。 即如果是2013年2月28日,将会在2013年3月1日前进行。

我的老板希望它根据光标所在的文本框移动。所以如果是在txtMonth,那么date应该从2013年2月28日到2013年3月28日。

我怎么让微调器检测哪个文本框包含光标(如果有的话)?一旦我知道,我可以做math。

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 

*以下解决scheme更新** '此解决scheme检测光标是否在date,月份或年份中最后一个。 “旋转button按所选数量(日,月或年)递增”方框已链接,因此旋转会根据您的select将您带到下一个有效date

 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 

尝试这个:

这个代码会在按下SpinButton之前检测到有焦点的最后一个TextBox
请注意,一旦您将焦点设置在另一个对象上, 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上添加Exit事件以将其名称存储在LastActiveObjectvariables中

 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 

希望这可以帮助你完成你想要的东西。

我不知道如何检测当前焦点的对象。

作为一个初步的解决方法,你可能会看到我的代码如下。 而不是检测焦点上的对象,我们检测到enter事件“input”文本框的时间。 然后我们使用全局variables来存储该文本框的名称

 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