Consulting

Results 1 to 4 of 4

Thread: CommandButton1_automatically update the date in ("A") column

  1. #1

    CommandButton1_automatically update the date in ("A") column

    Hi,

    i have try to code as below, but seem not reflecting.

    I want column A14 to a44, if any value in number for this column then the "B" will update the time.
    Next will lock the cell that has the value and color yellow.

    I need help on this.

    Private Sub CommandButton1_Click()

    If Range("A14:A44") Is Nothing Then

    On Error Resume Next

    If Target.Value = "" Then

    Target.Offset(0, 1) = ""

    Else

    Target.Offset(0, 1).Value = Format("HH:mm:ss")


    End If
    End If

    End Sub
    Attached Images Attached Images
    • File Type: jpg 1.jpg (20.9 KB, 5 views)

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. Range("A4:A14") always exists and hence will never be Nothing

    2. Target is not defined so On Error Resume Next will just keep on going

    3. Format("HH:mm:ss") doesn't have anything to format


    I'm guessing that you wanted something like this (to at least get you started)

    
    
    Option Explicit
    
    
    Private Sub CommandButton1_Click()
        Dim r As Range, r1 As Range
        
        On Error Resume Next
        Set r = Range("A4:A14").SpecialCells(xlCellTypeConstants, xlNumbers)
        On Error GoTo 0
        
        If r Is Nothing Then Exit Sub
        
        For Each r1 In r.Cells
            If Len(r1.Offset(0, 1).Value) = 0 Then
                r1.Offset(0, 1).Value = Format(Now, "HH:mm:ss")
                r1.Offset(0, 1).Locked = True
                r1.Resize(1, 5).Interior.Color = 13434879
            End If
        Next
    
    
    End Sub

    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi Paul.

    Yeah, thank you so much. Its run well.

    However the locked is not functioning, can please help.
    i try to do via the format cell , then the macro not working with the cell locked.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    All cells on the WS are Locked=False, except for B4:E14 which are Locked = True

    More that that, you'll have to figure out the other specifics

    Option Explicit
    
    
    
    
    Private Sub CommandButton1_Click()
        Dim r As Range, r1 As Range
        
        On Error Resume Next
        Set r = Range("A4:A14").SpecialCells(xlCellTypeConstants, xlNumbers)
        On Error GoTo 0
        
        If r Is Nothing Then Exit Sub
        
        Me.Unprotect "abc"
        
        For Each r1 In r.Cells
            If Len(r1.Offset(0, 1).Value) = 0 Then
                r1.Offset(0, 1).Value = Format(Now, "HH:mm:ss")
                r1.Offset(0, 1).Locked = True
                r1.Resize(1, 5).Interior.Color = 13434879
            End If
        Next
    
    
        Me.Protect Password:="abc", UserInterfaceOnly:=True
    
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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