Consulting

Results 1 to 5 of 5

Thread: VBA - how to change Text Box to date format

  1. #1
    VBAX Regular
    Joined
    Oct 2023
    Posts
    11
    Location

    VBA - how to change Text Box to date format

    Hi,

    I need help with the text box changing to the date format.

    I have 8 text boxes in sheet1, which link to the cell of B6 to B13 on Sheet 2, and I named the text boxes from "TextBoxSD1" to "TextBoxSD8", how can I mass change the text in the text boxes in date format? Do I need to specify the text boxes on sheet 1?


    e.g. Date input on Sheet 2: 02/11/23
    Date showing on Textbox on Sheet1: 45232


    Here is the code I tried to write:

    Sub Date_Format()
    Dim i As Long
    For i = 6 To 13
        Sheet2.Cells(i, 2) = Format(Me.Controls("TextBoxSD" & i - 5, "dd/mm/yy")
    Next i
    End Sub

  2. #2
    VBAX Mentor
    Joined
    Nov 2020
    Location
    Cochin, Kerala
    Posts
    314
    Location
    attaching a sample file would help.
    I tried your example, and the text box appears to be in date format for me.

  3. #3
    VBAX Regular
    Joined
    Oct 2023
    Posts
    11
    Location
    Tried many times but still came out with error. See attached.
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Short answer:
    Sheet2.Cells(i, 2) = Format(Sheet1.OLEObjects("TextBoxSD" & i - 5).Object.Value, "dd/mm/yy")
    Long answer:
    Problems.
    You're going to end up with plain text in those cells, not real dates.
    Compounded by you already having each of the textbox's linked cell set as those cells. Changing what's in those cells will change what's in the text boxes and vice versa.

    So what are you trying to do?

    A guess: Unlink all the textboxes linked cells and use:
    Sheet1.OLEObjects("TextBoxSD" & i - 5).Object.Value = Format(Sheet2.Cells(i, 2), "dd/mm/yy")
    in your loop
    Last edited by p45cal; 10-22-2023 at 10:03 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Regular
    Joined
    Oct 2023
    Posts
    11
    Location
    Now I got it. The issue was due to my linked cells.

    It works now with your solution provided thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •