CommandButton1_automatically update the date in ("A") column

mawar5530

New Member
Joined
Apr 5, 2020
Messages
38
Office Version
  1. 2016
Platform
  1. Windows
  2. Mobile
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
 

Attachments

  • 1.PNG
    1.PNG
    13 KB · Views: 8

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Do you want it to work with the button or would you prefer it to be automatic when you modify the cell in A then update the cell in B, also lock cell in A and highlight yellow.
 
Upvote 0
i can figure it out now, but 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.

Private Sub CommandButton2_Click()

Dim r As Range, r1 As Range

On Error Resume Next
Set r = Range("A14:A44").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
 
Upvote 0
Do you have the protected sheet?
Try the following, change "abc" for your password.

VBA Code:
Private Sub CommandButton2_Click()
  Dim r As Range, r1 As Range
  
  On Error Resume Next
  ActiveSheet.Unprotect "abc"
  Set r = Range("A14:A44").SpecialCells(xlCellTypeConstants, xlNumbers)
  On Error GoTo 0
  If r Is Nothing Then
    ActiveSheet.Protect "abc"
    Exit Sub
  End If
  
  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
  ActiveSheet.Protect "abc"
End Sub
 
Upvote 0
Do you have the protected sheet?
Try the following, change "abc" for your password.

VBA Code:
Private Sub CommandButton2_Click()
  Dim r As Range, r1 As Range
  
  On Error Resume Next
  ActiveSheet.Unprotect "abc"
  Set r = Range("A14:A44").SpecialCells(xlCellTypeConstants, xlNumbers)
  On Error GoTo 0
  If r Is Nothing Then
    ActiveSheet.Protect "abc"
    Exit Sub
  End If
  
  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
  ActiveSheet.Protect "abc"
End Sub

PERFECT !

Thank you so much DanteAmor
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top