0

I wanted to add Button to each row against each activities from 11 to 35, button is in column 'K11' while clicking on button macro will automattically add user name and time in cell Q11 and enter text as success in cell N11

I have created below button on row 11.

Sub addDetail()

Sheets("sheet1").Range("Q11").Value = _
        Environ("username") & " - " & Format(Now, "mm/dd/yyyy HH:mm:ss")
Range("N11").Value = "Success"
End Sub

do i have to create this code for 25 times to add code to each button in each row or there is any other method?

3 Answers 3

2

Consider using a loop:

Sub addDetail()
    Dim roww As Long

    With Sheets("sheet1")
        For roww = 11 To 35
            .Range("Q" & roww).Value = Environ("username") & " - " & Format(Now, "mm/dd/yyyy HH:mm:ss")
            .Range("N" & roww).Value = "Success"
        Next roww
    End With
End Sub

In a similar fashion, you can create a button maker routine that uses a loop to place buttons on rows 11 through 35.

EDIT#1:

I put a small Forms button on row#11 and assigned this macro to it:

Sub Button1_Click()
    Dim s As Shape
    shapename = Application.Caller
    Set s = ActiveSheet.Shapes(shapename)
    Call addDetail(s.TopLeftCell.Row)
End Sub

The macro can be used for all the buttons.

The macro determines the row it is on and calls the addDetails() sub with the correct row information. We must modify addDetails() to accept the row:

Sub addDetail(roww As Long)
    With Sheets("sheet1")
            .Range("Q" & roww).Value = Environ("username") & " - " & Format(Now, "mm/dd/yyyy HH:mm:ss")
            .Range("N" & roww).Value = "Success"
    End With
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Hi when i click on 1st button it adds data to each row not on single row
@khyatidedhia Do you want each button to only change the cells on the same row it resides ??
Hi Gary, Yes i need each button to change only same row
@khyatidedhia See my EDIT#1
1

Application Caller feat. Buttons

Option Explicit

Sub addDetail_Click()
    On Error GoTo CleanExit
    Dim btnRow As Long
    With ActiveSheet
        btnRow = .Shapes(Application.Caller).TopLeftCell.Row
        .Cells(btnRow, "Q").Value = _
          Environ("username") & " - " & Format(Now, "mm/dd/yyyy HH:mm:ss")
        .Cells(btnRow, "N").Value = "Success"
    End With
CleanExit:
End Sub

Comments

1

Having 25 buttons next to each other is probably not the best approach. If you need this, each Shape has TopLeftCell attribute, so you can use the very same code for all 25 buttons and replace the row in the two ranges with this attribute.

If you would be happy with one button only, what about creating a set-up like this

enter image description here

So that you can change the row that you want to edit and just need one button?

Sub addDetail()
    dim rowA as string
    rowA = range("B2").value

    Sheets("sheet1").Range("Q" & rowA).Value = _
        Environ("username") & " - " & Format(Now, "mm/dd/yyyy HH:mm:ss")
    Range("N" & rowA).Value = "Success"
End Sub

1 Comment

Thank you, but these all activities are performed by different individual so needed different button for each activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.