簡體   English   中英

Excel 2010用戶格式:在文本框3中輸入的日期將自動在文本框7中記錄星期幾(即星期一)

[英]Excel 2010 Userform: Date entered in Textbox 3 will automatically record day of the week in Textbox 7 (i.e. Mon)

我有一個用戶窗體,我需要用戶在文本框3中輸入日期,然后在文本框7中指定實際的星期幾(縮寫為ddd)。Textbox3中的示例12/7/2014將在其中顯示“ Sun”文字框7。

有人能幫忙嗎?

為此,您將需要一些不同的組件。 另外,請注意,為了將來的讀者,我在此示例中使用了TextBox1和TextBox2。 請在適用的地方應用您自己的文本框名稱。

  • 輸入值后,您將需要一個事件處理程序來運行代碼。 “_AfterUpdate”

  • 確保文本框中包含文本。 理想情況下,您還要確保它也是一種日期格式。 為了這個例子,我只是檢查一下Textbox是否為空。

  • 將他們輸入的文本轉換為日期。 CDATE(文本)

  • 格式化為“ ddd”

  • 將值返回到TextBox2

測試:

Private Sub TextBox1_AfterUpdate()

Dim tDate As Date

    If TextBox1.Text <> "" Then
        tDate = CDate(TextBox1.Text)
        TextBox2.Text = Format(tDate, "ddd")
    End If

End Sub

結果

總結列出的功能:

TextBox1_AfterUpdate   'Handles the event that when the user is done updating, code runs.  As 
                       'opposed to _Change, where it runs as soon as you type at all.

CDate(text)   

Format(tdate, "ddd")   

其他一些選擇:

Weekday(date, firstDayOfWeek (Optional))   ' There are other options for this as well

WorksheetFunction.Text(text, format)       'This worksheet function has many uses.  See link.

鏈接:

格式功能

平日功能

文字功能

編輯:添加鏈接-更新答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM