+ Reply to Thread
Results 1 to 14 of 14

Enhancing a Macro to detect which Row it's triggered from

  1. #1
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Enhancing a Macro to detect which Row it's triggered from

    Hi all,
    Hoping I could get some assistance with this - I usually only dabble in Macros, but this needs some VBA to enhance it.

    Below is a very simple Macro that Copies part of Row 20 (B20:H20), and pastes it into Row 1 at B.

    What I actually want to do is use Cell B in every Row on the sheet (including new 'Inserted' Rows) as a trigger to Copy that Row's range, say B:H, and ALWAYS paste it into the same target Row, say Row 1, at B.




    Sub CopyPasteExample()
    '
    ' CopyPasteExample Macro
    '

    '
    Range("B20:H20").Select
    Selection.Copy
    Range("B1").Select
    ActiveSheet.Paste
    End Sub




    Thanking you.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    This:
    Please Login or Register  to view this content.
    Could be just this:
    Please Login or Register  to view this content.
    However, I don't quite understand the wider objective.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Re: Enhancing a Macro to detect which Row it's triggered from

    Hi Trevor,
    The wider objective involves amending an Invoice's data - one Row per Invoice. The start of the process involves making a copy of an Invoice's data and placing that in a dedicated Row for actioning the amendment.
    So I want a Button (trigger cell) at the LH of each Row, that will copy that Invoice's data to the dedicated 'processing row'.

    Does that help to clarify?

    Regards.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    Ok, probably a Worksheet Selection Change Event handler monitoring column B and copying the selected row to row 1.

    Basically, if a cell in column B, other than row 1, is selected, the specified cells are copied to row 1, column B.

    If that's what you are looking for, it's not difficult. Unfortunately, I can't draft it right now as it's late (early) here and my computer is off.

    If you confirm that's what you want, I’ll code it tomorrow (later).

  5. #5
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Re: Enhancing a Macro to detect which Row it's triggered from

    Thank you Trevor, that sounds like what I need.

    Tiny bit of 'Scope Creep' if I may:
    Could you add a Confirmation Dialog box that will pop up -
    "Are you ready to copy this Invoice for Amendment processing?

    YES to proceed NO to cancel procedure"

    Also, could you tell me where/how to insert this code when I get it? I could search Google, but you'll be able to tell me quickly.
    And, is there a way to 'deactivate' this code easily? Rather than just delete it, I'm guessing a line of code inserted at the top could Deactivate it, but is there a Menu type option to Deactivate such a module/function?


    Regards.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    OK, please see the example workbook.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Re: Enhancing a Macro to detect which Row it's triggered from

    That looks great in the example file Trevor, thank you!!

    I'll try and port across to my worksheet.

    Kind regards.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    You're welcome.

    All you have to do is copy the above code. Then right click on the sheet tab and select View Code. Paste the code and you're done.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  9. #9
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Re: Enhancing a Macro to detect which Row it's triggered from

    Trevor,
    Bit of an unforeseen issue.
    Because the code is checking anytime Col B is selected/clicked, the Code is also triggered if the whole Row is selected by, for example, clicking on the Row number in the vertical LH heading.
    How best to get around this? Maybe checking that it's only a B column Cell that is clicked?

    Cheers.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    Try this:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Re: Enhancing a Macro to detect which Row it's triggered from

    Trevor, I posted it in to the code per below, but got Error Msg - rInterest variable not defined.


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Intersect(Target, rInterest) Is Nothing Then Exit Sub
    If Target.Cells.CountLarge > 1 Then Exit Sub

    Const msg As String = "Are you ready to copy this Invoice for Amendment processing?"

    Dim rInterest As Range
    Set rInterest = Range("B2:B" & Rows.Count)

    If Intersect(Target, rInterest) Is Nothing Then Exit Sub

    Dim vResponse
    vResponse = MsgBox(msg, vbYesNo, "Confirm")
    If vResponse <> vbYes Then Exit Sub

    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

    Range("B" & Target.Row).Resize(, 7).Copy Range("B1")

    End Sub

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    Sorry, it was meant to replace the existing single exit line (further down).

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    03-10-2021
    Location
    Miami, Gold Coast
    MS-Off Ver
    365 current latest
    Posts
    14

    Re: Enhancing a Macro to detect which Row it's triggered from

    Yes, thank you, I repositioned it before you posted, and got it working fine.
    (Maybe it'd be nice if Excel had a 'ClickSelect' function that would become active if a Cell was Clicked on, rather than being Selected in any other way...)

    Some 'Give Back' for the community:

    1. This blog post by influential and respected Packy McCormick "Excel Never Dies" is IMO wonderful:
    https://www.notboring.co/p/excel-never-dies

    2. Given that maybe 90% of Excel work looks pretty awful, aesthetically-wise, it's so nice to see that with just the tools embedded already in Excel, you can create lovely-looking stuff.
    And here's just one example I tripped over yesterday - (I've got no affiliations)
    https://www.datastudiopro.com/

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: Enhancing a Macro to detect which Row it's triggered from

    You're welcome.

    Thanks for the links.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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. How do I get a message box come up when a macro triggered
    By gareth7412 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2022, 09:25 AM
  2. Enhancing existing VBA.
    By baxius in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2019, 03:32 PM
  3. Enhancing multipage tabs
    By torachan in forum Tips and Tutorials
    Replies: 6
    Last Post: 10-31-2018, 03:58 AM
  4. [SOLVED] detect if worksheet_change is triggered by delete button
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-16-2015, 07:00 PM
  5. Macro Triggered By Combo Box
    By fredjones in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-21-2012, 07:44 AM
  6. Enhancing an Excel Database
    By CharlesEric in forum Excel General
    Replies: 1
    Last Post: 07-24-2009, 12:44 PM
  7. Macro triggered by an event
    By AussieAVguy in forum Excel General
    Replies: 2
    Last Post: 06-16-2005, 01:05 AM

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