+ Reply to Thread
Results 1 to 15 of 15

How do I assign a code to a button

Hybrid View

  1. #1
    Registered User
    Join Date
    08-19-2007
    Posts
    28

    How do I assign a code to a button

    I have had the following written for me


    Private Sub Worksheet_Change(ByVal Target As Range)
    For Each Cell In Target
    If Cell.Column = 4 and Cell.row >= 17 and Cell.row <= 20 Then
    If Cell <>"" Then
    Cell.Offset(0, 3) = Date
    Else
    Cell.Offset(0, 3) = ""
    End If
    End If
    Next Cell
    End Sub

    I wish to assign that code to a button on sheet 1.

    I normally just right click the buitton and assign the macro, but on this occasion I can't. I think it's to do with it being a private sub.

    All help much apprecitaed.

    Thanx

    Steve

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    That is a worksheet change event procedure which is automatically activated by the event of a change being made to the worksheet to which it is attached. You would need to put it in a standard code module and change to something like:

    Private Sub test
    For Each Cell In Selection
    If Cell.Column = 4 and Cell.row >= 17 and Cell.row <= 20 Then
    If Cell <>"" Then
    Cell.Offset(0, 3) = Date
    Else
    Cell.Offset(0, 3) = ""
    End If
    End If
    Next Cell
    End Sub
    Then you could assign to a button as normal.

  3. #3
    Registered User
    Join Date
    08-19-2007
    Posts
    28
    Hi Stephen

    Thanx for your quick reply, I have made the suggested changes and the macro still is not showing, however when I take the private bit out it does show but doesnt work.


    Steve

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    Sorry, yes remove the Private, and declare cell. Should work:

    Sub test()
    
    Dim cell As Range
    
    For Each cell In Selection
        If cell.Column = 4 And cell.Row >= 17 And cell.Row <= 20 Then
            If cell <> "" Then
                cell.Offset(0, 3) = Date
            Else
                cell.Offset(0, 3) = ""
            End If
        End If
    Next cell
    
    End Sub

  5. #5
    Registered User
    Join Date
    08-19-2007
    Posts
    28
    No mate doesnt work, as when I activate the macro it deletes the info in another cell


    What I would like is a macro to show the date that data was entered into G17:G20 and display the date that data was imput into cells D17:D20, but if any of the cells in G17:G20 are blank then display nothing.

    thanx


    Steve

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    You're currently checking column D and putting the date in G so amend as below. See attachment.
    Sub test()
    
    Dim cell As Range
    
    For Each cell In Selection
        If cell.Column = 7 And cell.Row >= 17 And cell.Row <= 20 Then
            If cell <> "" Then
                cell.Offset(0, -3) = Date
            Else
                cell.Offset(0, -3) = ""
            End If
        End If
    Next cell
    
    End Sub
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    08-19-2007
    Posts
    28
    Sorry Steve

    just a quickie - lets say i eneter data in G17 today and press the button, it will display todays date in D17, however if I enter data in G18 tomorrow and then press the button will D17's info change to tomorrows date as well, if it does is there a way to make that date static until I press the save button which clears those contents onto another page


    thank you as always


    Steve

  8. #8
    Registered User
    Join Date
    08-19-2007
    Posts
    28
    Hi once again Steve


    have just tried it out and changed the date on the Pc and cell D17 does change to whatever date I pressed the button.

    Appreciate all your help but was wanting to keep D17:D20 locked with the date that the data was first entered. So I could end up with 4 dates in D17:D20


    thank you

    Steve

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    Steve,

    See the re-modified attachment. Have gone back to the original approach using the Worksheet Change Event. When you enter something in G17:G20 you should get the date in D17:D20 and it shouldn't update whenever you enter new values.

    I also did some housekeeping and removed a few empty modules!

    Steve/Stephen
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    08-19-2007
    Posts
    28
    Steve

    many thanx indeed for that, works a treat, can I ask you one favour please, the worksheet that you got it working on was my 'test worksheet' I have attached my working sheet, could you please do the same for this one with the same code as your previous post, along with the housekeeping.

    Sorry but excel is obviously not one of my strong points.

    Or tell me on how to copy and paste the code into this workbook, as I do need to know I suppose.


    Cheers

    Steve (Co Durham)
    Attached Files Attached Files
    Last edited by Stevesmith; 08-26-2007 at 10:40 AM.

  11. #11
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    Steve - this should do it. For a worksheet event, right-click on the worksheet tab and select view code. The VB editor opens up and then you can just paste stuff in as normal.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1