+ Reply to Thread
Results 1 to 6 of 6

Automatically add button when data added to cell and apply macro to button?

  1. #1
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    Automatically add button when data added to cell and apply macro to button?

    I've got this code which adds a button into the next cell across from where text is entered, i.e if I enter text in A5 the button appears in B5. This Code is entered in workbook 2

    Public Sub Worksheet_Change(ByVal Target As Range)
    Dim col As Integer
    Dim row As Integer
    col = Target.Column
    row = Target.row
    If Not IsNull(Target.Value) And Not IsEmpty(Target.Value) Then
    Application.EnableEvents = False
    Buttons.Add Cells(row, col + 1).Left, Cells(row, col + 1).Top, Cells
    (row, col + 1).Width, Cells(row, col + 1).Height
    Application.EnableEvents = True
    End If
    End Sub


    I want these buttons to automatically have a macro assigned to them so that it copies the text from the cell prior to the one they are in, i.e. the button is in cell B5 and the text is in A5, and then pastes it to another worksheet (In workbook 1).

    Currently I assign the macro to buttons manually. This is the macro I use.

    Sub Macro2()
    'Application.ScreenUpdating = False
    Text = Range("A5")
    Windows("Workbook 1.xlsm").Activate
    Dim i As Range
    Range(Range("C4"), Range("C4")).Select
    For Each i In Selection
    i.Value = i.Value & Text & " "
    Next i
    Windows("Workbook 2.xlsm").Activate
    'Application.ScreenUpdating = True
    End Sub

    The range A5 represents where the text is copied from and this changes depending on which text I want to copy.

    Range C4 is where I'm posting to in Workbook 1 and this remains constant.


    Can anyone advise what changes I need to make to the macro so that it always takes the text from the cell prior to where the button is located, rather than having to have a seperate macro for every cell?

    Also, I've been told I could use function Selection.OnAction = . I've done a bit of reading but I just don't understand it?

    Thanks very much in advance.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Automatically add button when data added to cell and apply macro to button?

    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 04-13-2016 at 11:22 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    Re: Automatically add button when data added to cell and apply macro to button?

    Excellent, Just what I wanted.

    One last question...

    If I wanted to name the button something othat than 'Button x' within the same code how would I go about it?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Automatically add button when data added to cell and apply macro to button?

    You're welcome.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    Re: Automatically add button when data added to cell and apply macro to button?

    Excellent, Thank you.

  6. #6
    Registered User
    Join Date
    03-30-2016
    Location
    Manchester
    MS-Off Ver
    2013
    Posts
    93

    Re: Automatically add button when data added to cell and apply macro to button?

    OK, for some reason the macro has stopped working.

    I get the following error

    'Run-Time error ‘1004’ – Unable to get the Add property of the Buttons Class'

    and the debugger highlights this text in the macro

    Please Login or Register  to view this content.
    Help very much appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] macro works but when added to button macro, it fails, dont know why
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-06-2016, 01:20 PM
  2. Replies: 0
    Last Post: 06-10-2014, 10:44 PM
  3. Multiple Lines added as Macro button
    By benbaker9876 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-02-2013, 07:01 AM
  4. [SOLVED] If I apply formula to my column can the cell be blank until data is added?
    By KELLIS in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 08-01-2012, 03:39 PM
  5. Replies: 0
    Last Post: 01-08-2012, 02:31 PM
  6. Replies: 2
    Last Post: 07-04-2006, 02:10 PM
  7. how to apply button text from macro?
    By David in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-29-2005, 05:05 PM

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