+ Reply to Thread
Results 1 to 11 of 11

Copying the macro button to another sheet

Hybrid View

Heyy Copying the macro button to... 02-17-2021, 02:10 PM
dangelor Re: Copying the macro button... 02-17-2021, 02:51 PM
MarvinP Re: Copying the macro button... 02-17-2021, 03:38 PM
Heyy Re: Copying the macro button... 02-18-2021, 06:36 AM
dangelor Re: Copying the macro button... 02-18-2021, 07:46 AM
Heyy Re: Copying the macro button... 02-18-2021, 08:04 AM
dangelor Re: Copying the macro button... 02-18-2021, 08:09 AM
Heyy Re: Copying the macro button... 02-18-2021, 08:33 AM
dangelor Re: Copying the macro button... 02-18-2021, 09:09 AM
Heyy Re: Copying the macro button... 02-18-2021, 09:31 AM
dangelor Re: Copying the macro button... 02-18-2021, 09:32 AM
  1. #1
    Registered User
    Join Date
    01-22-2021
    Location
    Europe
    MS-Off Ver
    2012
    Posts
    18

    Copying the macro button to another sheet

    Hi,

    I am trying to copy a macro button to another sheet so that the macro could be used in the "Report" sheet without a need to return back to the "Analysis" sheet. I just tried to copy the macro button to the another sheet but then the macro does not work on the "Report" sheet and it gives an error when I try to run it. Same thing happens when I created a new button and assigned the same macro to the new button in the "Report" sheet. Do you have any tips how I could copy the macro button to the "Report" sheet from the "Analysis" sheet?
    Last edited by Heyy; 02-18-2021 at 09:53 AM.

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,316

    Re: Copying the macro button to another sheet

    Assuming the two worksheets are the same (they are not in the posted workbook), possibly...
    Public Sub Breakeventest()
    
        'Not Tested
    
        With ActiveSheet
            .Range("F2").Value = .Range("B2").Value
            .Range("D14").ClearContents
            .Range("H25").GoalSeek Goal:=0, ChangingCell:=.Range("B2")
            .Range("H27").Value = .Range("B2").Value
            .Range("E2").ClearContents
            .Range("B2").Value = .Range("F2").Value
            .Range("F2").ClearContents
            .Range("G2").Select
        End With
    End Sub

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,379

    Re: Copying the macro button to another sheet

    Hey Heyy,

    When you record a macro or write a normal macro it defaults to the ACTIVE worksheet. If you copy a button that works on one sheet to another, the button will call the VBA code but the sheet you press the button from is also very important!!!!

    If your macro copies a cell in Range("A2") from a sheet it is from the Active sheet. If you want the value on Sheets1, you need to use something like Worksheets("Sheet1").Range("A2")

    I recently learned that using the "Set" statement very useful when writing code that uses other sheets. Something like:

    Dim WS1 as worksheet
    Set WS1 = Worksheets("Sheet1") ' and then do you code like:
    TempVar = WS1.Range("A2").Value

    Hope this helps explain your concern.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    01-22-2021
    Location
    Europe
    MS-Off Ver
    2012
    Posts
    18

    Re: Copying the macro button to another sheet

    Hi,

    Thank you for your answers. I am very beginner with macros and thus, I wasn't able to get the macro working on the second sheet despite your advices.

    The case is that the analysis sheet is the main sheet and the macro is recorded there and the main button is there. But I would need to copy the same button to the report sheet so that the user can run the same macro from the report sheet without need to return to the analysis sheet. The macro's result is visible in the cell H27 and in the analysis sheet I just have done a formula =Analysis!H27 so the macro's result is copied there. Now I just would need to get the same button working in the report sheet so it would run the macro in the analysis sheet and copy the outcome to the report sheet. Any other tips? I hope my expiation wasn't too confusing.

  5. #5
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,316

    Re: Copying the macro button to another sheet

    Try...
    Sub Breakeventest()
        '
        ' Breakeventest Macro
        '
    
        '
        With Worksheets("Analysis")
            .Activate
            .Range("B2").Select
            Selection.Copy
            .Range("F2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("D14").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("D13").Select
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            .Range("H25").GoalSeek Goal:=0, ChangingCell:=.Range("B2")
            .Range("B2").Select
            Selection.Copy
            .Range("H27").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("E2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("F2").Select
            Selection.Copy
            .Range("B2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("F2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("G2").Select
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    01-22-2021
    Location
    Europe
    MS-Off Ver
    2012
    Posts
    18

    Re: Copying the macro button to another sheet

    Quote Originally Posted by dangelor View Post
    Try...
    Sub Breakeventest()
        '
        ' Breakeventest Macro
        '
    
        '
        With Worksheets("Analysis")
            .Activate
            .Range("B2").Select
            Selection.Copy
            .Range("F2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("D14").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("D13").Select
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            .Range("H25").GoalSeek Goal:=0, ChangingCell:=.Range("B2")
            .Range("B2").Select
            Selection.Copy
            .Range("H27").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("E2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("F2").Select
            Selection.Copy
            .Range("B2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("F2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("G2").Select
        End With
    End Sub
    Wow with this code the macro button is now working on the report sheet also! Thank you for that! But now one more issue arise, when I run the macro on the report sheet, it will automatically switch to the analysis sheet. Is there any trick to avoid excel automatically switching back to the analysis sheet when I run the macro on the report sheet?

  7. #7
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,316

    Re: Copying the macro button to another sheet

    Sub Breakeventest()
        '
        ' Breakeventest Macro
        '
    
        '
        Dim shName As String
        shName = ActiveSheet.Name
        
        With Worksheets("Analysis")
            .Activate
            .Range("B2").Select
            Selection.Copy
            .Range("F2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("D14").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("D13").Select
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            .Range("H25").GoalSeek Goal:=0, ChangingCell:=.Range("B2")
            .Range("B2").Select
            Selection.Copy
            .Range("H27").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("E2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("F2").Select
            Selection.Copy
            .Range("B2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("F2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("G2").Select
        End With
    
        Worksheets(shName).Activate
    End Sub

  8. #8
    Registered User
    Join Date
    01-22-2021
    Location
    Europe
    MS-Off Ver
    2012
    Posts
    18

    Re: Copying the macro button to another sheet

    Quote Originally Posted by dangelor View Post
    Sub Breakeventest()
        '
        ' Breakeventest Macro
        '
    
        '
        Dim shName As String
        shName = ActiveSheet.Name
        
        With Worksheets("Analysis")
            .Activate
            .Range("B2").Select
            Selection.Copy
            .Range("F2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("D14").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("D13").Select
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            Application.CutCopyMode = False
            .Range("H25").GoalSeek Goal:=0, ChangingCell:=.Range("B2")
            .Range("B2").Select
            Selection.Copy
            .Range("H27").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("E2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("F2").Select
            Selection.Copy
            .Range("B2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                   :=False, Transpose:=False
            .Range("F2").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = ""
            .Range("G2").Select
        End With
    
        Worksheets(shName).Activate
    End Sub
    Thank you again! One more question (sorry for asking so many questions) ; now if I press the macro button on the analysis sheet, it will jump automatically to the report sheet. Is there any way to code that if the macro button is pressed in the analysis sheet, excel will stay on that sheet and if the macro button is pressed in the report sheet, excel will stay on that sheet?

  9. #9
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,316

    Re: Copying the macro button to another sheet

    ??? Works for me...
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    01-22-2021
    Location
    Europe
    MS-Off Ver
    2012
    Posts
    18

    Re: Copying the macro button to another sheet

    Quote Originally Posted by dangelor View Post
    ??? Works for me...
    Ah yes, I had done a mistake. I thought that I need to replace that (shName) with the sheet's actual name so that was messing up the code. Thank you so much for your help! You're so great!

  11. #11
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,316

    Re: Copying the macro button to another sheet

    Glad I could help!


    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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] Copying ActiveX Command Button from master sheet to subsheets. command button code missing
    By popper08 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-13-2020, 10:15 AM
  2. Replies: 1
    Last Post: 05-12-2016, 03:36 AM
  3. Replies: 2
    Last Post: 05-12-2016, 03:08 AM
  4. Replies: 34
    Last Post: 08-12-2014, 04:02 AM
  5. Copying a button to another sheet
    By damaple in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-05-2013, 03:03 AM
  6. Replies: 0
    Last Post: 03-13-2013, 03:14 PM
  7. Code Under Button Not Recongnizing New Sheet When Copying
    By SEOT in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-16-2008, 11:16 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