I really appreciate the gratitude. I don't receive this from most.
Creating a button can be a sophisticated process. So
below is code that will create one for you (and in a place that you might like it to be created). So
[1] Copy this code to the standard code module where you have put previous code (
not the code that's in the sheet itself . . . where the actual copy subroutine is). The place where you put:
VBA Code:
Sub Copy_And_Paste_To_Sheet(sourceSheetName As String, destinationSheetName As String)
For simplicity, put the code above it.
VBA Code:
Sub Test__Create_Button()
Call Create_Button("Maturity Assessment", "G3:G4", "Test__Copy_And_Paste_To_Sheet")
End Sub
Sub Create_Button(sheetName As String, cellAddress As String, macroNameToRun As String)
Sheets(sheetName).Buttons.Delete
With Sheets(sheetName).Range(cellAddress)
Dim btn As Button
Set btn = Sheets(sheetName).Buttons.Add(.Left, .Top, .Width, .Height)
End With
With btn
.Font.Size = 12
.Font.Bold = False
.Font.Name = "Calibri"
.OnAction = macroNameToRun
.Caption = "Copy"
.Name = "Copy Button"
End With
End Sub
Sub Test__Copy_And_Paste_To_Sheet()
Call Copy_And_Paste_To_Sheet("Maturity Assessment", Sheets("Maturity Assessment").Range("F4").Value)
End Sub
[2] Remove/delete (or comment it all out by selecting all with Ctrl A and pressing this button at the top of the VBA code window
the code that's in the sheet itself:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$4" Then
Call Copy_And_Paste_To_Sheet("Maturity Assessment", Range("F4").Value)
End If
End Sub
All you have to do is (in the code window where this code is):
VBA Code:
Sub Test__Create_Button()
Call Create_Button("Maturity Assessment", "G3:G4", "Test__Copy_And_Paste_To_Sheet")
End Sub
Left click on any of the above (3 lines of) code (preferably the middle line), and then click on the green Run/Play button.
A button like this should now be next to the drop-down.
- If you would like to color it, it's not possible from this simple process to make a button. You should probably start another thread/topic on that, as I have little experience with that. (I actually prefer to use cells as buttons. Double clicking on a cell or right clicking on one to trigger a macro to run.)
- If you would like to rename the button, you can simply find the text "Copy" in the code I just gave you. That's the "Caption" in this code segment (of which you can make other obvious changes to the look of the font).
VBA Code:
With btn
.Font.Size = 12
.Font.Bold = False
.Font.Name = "Calibri"
.OnAction = macroNameToRun
.Caption = "Copy"
.Name = "Copy Button"
End With
If at any time you would like to delete the button, just right click on it and select
cut. You can always do the same thing (run this program to recreate it).