+ Reply to Thread
Results 1 to 5 of 5

repetitive macros-solved

Hybrid View

  1. #1
    Registered User
    Join Date
    12-03-2009
    Location
    yorkshire, england
    MS-Off Ver
    Excel 2003
    Posts
    15

    repetitive macros-solved

    I have a work book with several (couple of hundred) buttons, which when clicked display the contents of a range of cells in a message box as shown below;

    Sub v103_Click()
     
    MsgBox Join(Application.Transpose(Application.Transpose(Range("Sheet1!A103:Sheet1!J103"))), Chr(10))
     
    End Sub
    
    Sub v104_Click()
     
    MsgBox Join(Application.Transpose(Application.Transpose(Range("Sheet1!A104:Sheet1!J104"))), Chr(10))
     
    End Sub
    
    Sub v105_Click()
     
    MsgBox Join(Application.Transpose(Application.Transpose(Range("Sheet1!A105:Sheet1!J105"))), Chr(10))
     
    End Sub
    Does anyone know of a more efficent way of coding this rather than copy,paste,edit,copy,paste,edit,copy,paste,edit........ad infinitum.
    Last edited by henrysmith; 12-06-2009 at 12:26 PM.

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: repetetive macros

    use excel itself
    in a blank sheet
    in a1 put
    ="Sub v"&CEILING(ROWS($A$1:A307)/3,1)&"_Click()"
    in a2
    ="MsgBox Join(Application.Transpose(Application.Transpose(Range(""Sheet1!A"&CEILING(ROWS($A$1:A307)/3,1)&":Sheet1!J"&CEILING(ROWS($A$1:A307)/3,1)&"""))),chr(10))"
    in a3
    End Sub
    then select a1:a3 and drag down as many as you need
    copy paste the lot into your vba
    then again there might be a vba way that is far cleverer but beyond me lol
    Last edited by martindwilson; 12-06-2009 at 12:00 PM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: repetetive macros

    henrysmith,

    Add a new button, and put the following code in it:


    
    Sub NewButton_Click()
    
    Dim WhatRow As Variant
    WhatRow = InputBox("What row number do you want to display?")
    If IsNumeric(WhatRow) Then
      MsgBox Join(Application.Transpose(Application.Transpose(Range("Sheet1!A" & WhatRow & ":Sheet1!J" & WhatRow & ""))), Chr(10))
    Else
      MsgBox "You did not enter a valid integer number - macro terminated!"
    End If
    
    End Sub
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Registered User
    Join Date
    12-03-2009
    Location
    yorkshire, england
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: repetetive macros

    Thanks guys....

    I am stunned and amazed, both fantastic replies.

    I will go for stanleydgromjr's solution, so much less work by asking the user for a little input.

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,524

    Re: repetetive macros-solved

    If you are using the buttons from the Forms toolbar, you could label the buttons with the row number, and assign all the buttons to this Macro.

    rw = ActiveSheet.Buttons(Application.Caller).Caption
    
    MsgBox Join(Application.Transpose(Application.Transpose(Range("Sheet1!A" & rw & ":Sheet1!J" & rw & ""))), Chr(10))

+ 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