i've created a macro to create a button when the sheet is activated. then i called it using the codes below: But everytime i go to different worksheet then back again to the sheet containing this button the macro does its job. i just want the macro to work when i clicked the button
Private Sub Worksheet_Activate()
Call sortData
End Sub
here's the code for macro that i've created:
Sub sortData()
'
'
'
'
ActiveSheet.Buttons.Add(689.25, 59.25, 133.5, 30).Select
Selection.OnAction = "sortData"
Selection.Characters.Text = "Sort Data"
With Selection.Characters(Start:=1, Length:=28).Font
.Name = "Times New Roman"
.FontStyle = "Bold"
.Size = 12
End With
Range("A1").Select
End Sub
now my problem is where will i put the codes shown below when this button is click? or simply how can i make this button worked?? i tried to put the codes in the same sheet where i called the macro but it is not working.
Sub sortData_Click() 'did i call the button right? but it is not working when i us it
'codes here
End Sub
in the VBA Editor, click insert new module (or you can just put this in the same module where your sortData is) then write this code into your new module:
Sub procedureName() 'procedure name is exactly the same as what you named in Selection.OnAction = "sortData"
'codes here
End Sub
what you actually done is making the sortData() call itself because you have set the Selection.OnAction = "sortData" ->>> change this name to whatever procedure name you want to perform
Related
I am trying to change the macro assigned to a button from a different sheet than where the button is. This is how far I've gotten:
Sub Macro1()
ActiveSheet.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, the above code requres me to be on the active sheet. I have tried the following:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).Select
Selection.OnAction = "Macro2"
End Sub
However, this will give me an "Object doesn't support this property or method" error.
Why doesn't it work when "ActiveSheet" is replaced with "Sheet1"?
And why can't I collect the two lines of code into one line?:
Sub Macro1()
Sheet1.Shapes.Range(Array("Button 1")).OnAction = "Macro2"
End Sub
Any help would be appreciated!
Please, simple try:
Sheet1.Shapes("Button 1").OnAction = "Macro2"
Of course, a macro named "Macro2" should exist in a standard module. If in a sheet code module, the sheet CodeName is necessary in front of the macro name ("Sheet1.Macro2")...
I have a userform which has multiple RefEdit controls. I need the user to select ranges from multiple sheets and the userform has to be complete before the rest of the code can run.
Issue: The activesheet is "Sheet1" when the userform is initiated. Each time I select a range on "Sheet2" and click into the next RefEdit the visible Excel sheet returns to "Sheet1". I'd like the sheet to remain on "Sheet2", since clicking between the sheets significantly increases the time it takes to select the data.
Because I need the userform to be completed before continuing with my code, using "vbModeless" doesn't appear to work.
I've tried to step through the userform events which appeared to be relevant but none were activated when I entered the RefEdit, selected the data, or left the RefEdit.
Thanks in advance for any help!
Edit: Using some input from the responses and doing some more research I think I've figured out the problem and a work around.
RefEdit events such as Change or Exit (I tried all of them I think) don't appear to trigger when a change occurs in the control. So I couldn't write code to manipulate the activesheet when I changed the control. A workaround found here: http://peltiertech.com/refedit-control-alternative/ uses a textbox and inputbox to simulate a RefEdit control and will actually trigger when changes are made! Code is below. To add other "RefEdit" controls you should repeat the code in the Userform_Initialize event for each control, then add another TextBox1_DropButtonClick and update TextBox1 to the name of the new control. In use when the control updates the workbook jumps to the previous activesheet and then returns the desired activesheet. Not as smooth as I'd like but much better than it was.
Code:
Private Sub CancelButton_Click()
Unload Me
End
End Sub
Private Sub OKButton_Click()
UserForm1.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
End
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.DropButtonStyle = fmDropButtonStyleReduce
Me.TextBox1.ShowDropButtonWhen = fmShowDropButtonWhenAlways
End Sub
Private Sub TextBox1_DropButtonClick()
Dim ASheet As String ' Active sheet
Me.Hide
'Use input box to allow user to select a range
On Error Resume Next
Me.TextBox1.Value = Application.InputBox("Select the range containing your data", _
"Select Chart Data", Me.TextBox1.Text, Me.Left + 2, _
Me.Top - 86, , , 0)
On Error GoTo 0
'Check if there is a sheet name - if the range selected is on the activesheet the output of the inputbox doesn't have a sheet name.
If InStr(1, Me.TextBox1.Value, "!", vbTextCompare) > 0 Then ' there is a sheet name
ASheet = Replace(Split(Me.TextBox1.Value, "!")(0), "=", "") ' extract sheet name
Else ' there is no sheet name
Me.TextBox1.Value = "=" & ActiveSheet.Name & "!" & Replace(Me.TextBox1.Value, "=", "") ' add active sheet name to inputbox output
ASheet = ActiveSheet.Name
End If
Worksheets(ASheet).Activate ' set the active sheet
Me.Show
End Sub
Have you tried something as simple as:
Sheets("Sheet2").Select
somewhere in the beginning of your form code ?
Since you haven't posted your code, it's hard to provide a good answer.
Hope this helps a little :)
This form module worked for me.
Private Sub CommandButton1_Click() 'Cancel Button
Unload Me
End Sub
Private Sub CommandButton2_Click() 'GO Button
Dim newSheet As Worksheet
abc = Split(RefEdit1.Value, "!")
cbn = abc(0)
Unload Me
Set newSheet = Worksheets(abc(0))
newSheet.Activate
End Sub
I have a macro that parses through a large file in Excel. I created a form that allows me to search for and provide a summary of specific messages. I can put
FormName.Show
at the end of the macro and it works. The form displays as the parsing macro completes. But I don't always want the form to appear and sometimes I want to call it again after I've saved the spreadsheet. So I wrote another function that can create a button that can open the form. In the .OnAction statement I have OnAction = "FormName.Show"
Sub Create_Button()
ActiveSheet.Buttons.Add(437.25, 72, 125.25, 47.25).Select
Selection.OnAction = "FormName.Show"
Selection.Characters.Text = "Search Messages"
End Sub
This doesn't work, it created the button but when I click on the button I get
"Cannot run the macro "xxxx.xlam'!FormName.Show' The macro may not be available in this workbook or all macros may be disabled.
Why does it work in the main macro but not in the button OnAction?
Thanks
Per MSDN, Shape.OnAction requires a macro name, not VBA code. You have tried to pass it VBA code. Instead try this:
Selection.OnAction = "showForm"
....
End Sub
Public Sub showForm()
FormName.Show
End Sub
.OnAction will not only run Macros not it will not evaluate code. Paste thiss in a standard module.
Sub ShowUserform()
FormName.Show
End Sub
Sub Create_Button()
With ActiveSheet.Buttons.Add(437.25, 72, 125.25, 47.25)
.OnAction = "ShowUserform"
.Characters.Text = "Search Messages"
End With
End Sub
i need a help here, i'm creating a code and one of it's functions will be to create a activeX command button but i need the code to create the button and assign the macro "Sub graft()" to it
So i need the macro to create the activeX button "Commandbutton1" and assign the code
Private Sub CommandButton1_Click()
Call graft()
End sub
i know how to create the button, but i can't find how to assign a code to it the button will be created on a worksheet called "Gráfico" on the ActiveWorkbook
Code of creation and placement of the button
ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, Left:=1200, Top:=20, _
Width:=100, Height:=30).Select
ActiveSheet.Shapes.Range(Array("CommandButton1")).Select
Selection.Cut
Cells(lline + 26, 1).Select
ActiveSheet.Paste
CutCopyMode = False
Try this out. You will have to modify your code a little, but this places the button where your code is placing it, gives the button a caption, and assigns the macro "graft" to it.
Using With to avoid select statements, then assign the properties of the command button however you see fit.
Sub CreateButton()
Dim newButton As Object
Set newButton = ActiveSheet.Buttons.Add(1200, 20, 100, 30)
With newButton
.OnAction = "graft"
.Caption = "Graft"
End With
End Sub
Forgive my ignorance (newby and little knowledge of VBA)...
I have developed some macros that are attached to buttons, and working in one worksheet in a workbook. The macros perform various jobs on a calendar. There is one calendar for each of 10 bedrooms in the wing of a hospital.
I now want to make identical worksheets with the same buttons and macros for each bedroom i.e. 10 worksheets.
But try as I might I cant get the macros to work in the other worksheets.
The macros are in the VBA code editor for the first worksheet (Bed1). I have copied the code into the "This Workbook" page within the VBA editor - but that had no effect, other than to stop them working at all.
This is a typical macro:
'============================================
Private Sub Prevw1_Click()
'============================================
' DAILY PATIENT TIMETABLE
' PRINT PREVIEW
'============================================
ActiveSheet.Select
ActiveSheet.AutoFilterMode = False
Range("_Daily").Select
ActiveSheet.PageSetup.PrintArea = "_Daily"
'
Call page_SetUp
'
' Variations for page setup
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(1.5)
.RightMargin = Application.InchesToPoints(0.9)
.Zoom = 75
End With
ActiveSheet.PrintPreview
ActiveSheet.PageSetup.PrintArea = ""
Range("H126, H126").Select
End Sub
Q. What have I done wrong that makes this only work in the Bed1 worksheet where it was developed first?
Kind regards
Russ
Take the code out of the ThisWorkbook module and put it in a normal code module. In Design Mode, in the Excel window (not VBE), right-click the button and do Assign Macro, then choose the macro "Prevw1_Click". That should work. You'll have to assign the macro to each button, or you could simply copy/paste the button to the other sheets.
If your button is an ActiveX Control, then I think you may need to have the subroutine for each button in the worksheet where the button resides. So, each worksheet may have an activeX command button called "CommandButton1", then each Worksheet code module should have a subroutine like:
Sub CommandButton1_Click()
Call ClickTheButton
End Sub
You will basically put all of this same code in each of the 10 worksheet code modules. Then, rename your routine in the ordinary code module, like:
Private Sub ClickTheButton()
'============================================
' DAILY PATIENT TIMETABLE
' PRINT PREVIEW
'============================================
ActiveSheet.Select
ActiveSheet.AutoFilterMode = False
Range("_Daily").Select
ActiveSheet.PageSetup.PrintArea = "_Daily"
'
Call page_SetUp
'
' Variations for page setup
With ActiveSheet.PageSetup
.LeftMargin = Application.InchesToPoints(1.5)
.RightMargin = Application.InchesToPoints(0.9)
.Zoom = 75
End With
ActiveSheet.PrintPreview
ActiveSheet.PageSetup.PrintArea = ""
Range("H126, H126").Select
End Sub
The reason I would do this, instead of copying the existing macro to each of 10 worksheets is simple: If you ever need to modify your subroutine, you only need to modify it in one place. Likewise, if you add a new worksheet(s) you need only copy 3 lines of code instead of 20. It's just easier to maintain this way, since each sheet's button is calling the same code, each sheet's button should just have a simple sub that calls the "main" procedure.