vba code to add a command button in current sheet - excel

What is the vba code to add a command button on currentworksheet, then insert a code to run whenever that button is clicked?
advance thanks.
Note: the task for the command button click is : select workbook name 123(which is already opend), select worksheet name ABC then select range a123.

I have an example here to add buttons and assign macros to them.
Create Buttons and assign macros to them

Related

Userform textbox control can't exit using Enter key at the 1st run, but working at the 2nd run in Excel

I assigned a macro to a shape in a worksheet to run a Userform, but unfortunately I can't exit any of my textboxes inside the form using the Enter key, but you can jump within the textbox by mouse click.
When I exit/terminate the form and run it again, then it will work.
I set a filter to the textbox to search for each changes, and the listbox doesn't work too, but it works on the 2nd time I run the userform.
What could be the problem here and how can I fix it?

Opening a new frame with a CommandButton in Excel

Hello I just started programming and I have a question: I want to open a new frame with the command button, it worked on the UserForm but not on the sheet in Excel. Can anyone help me?
With an ActiveX button:
Insert a button into your sheet from the Developer tab -> Insert -> ActiveX Controls.
Double-click on the button and it will take you to the VBA editor, in the Click event of that button.
Add the code UserForm.Show in the event code where UserForm is the name of your form.
With a form control:
Create a Sub in a module where you call the Show method of your UserForm (or frame as you call it).
Then insert a button into your sheet from the Developer tab -> Insert -> Form Controls. It will ask you what macro you want to assign to that button. Choose the Sub from step 1.
Sub showForm()
UserForm1.Show
End Sub
I think what could be done is to create a kind of shape or button on your worksheet, then, attach a vba script to activate the frame when clicked. It's just the same way on the UserForm only that you are using a shape or button in which you have attached a macro to on your worksheet.

Stop macro to allow the workbook to be edited

I have written a code to open a workbook and filter data by the number in a particular column. I need to filter "0" first and then edit the data and then filter between "+8" and "-8".
I added a message box to pause the macro but I am not able to edit the opened workbook while the message box is displayed. I tried with "Application.waiting" option.
I need to pause the macro automatically and start it manually.
Sub Filter_data()
Workbooks.open"D:\Reposrts\AAA.csv"
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:="0"
Activesheet.columns("A:Z").Autofit
MsgBox"Task ok" 'Here I need to pause the macro and allow for edit opened wb and then manually start macro for below line'
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:=">8", Opersator:=xlAnd, Criterial:="<-8"
End Sub
Split your existing Macro into two parts
The first part contains everything up to have the MsgBox"Task ok" line
The second part contains everything after that point
Create a Modeless (or "Non-Modal") User Form with your label and an "OK" button to call the second part of your split macro
"Modeless" means you can edit the workbook while the User Form is open. MsgBox is "Modal", which is why you can't edit anything. More details available from Microsoft
Replace the MsgBox"Task ok" line in the first part of your split macro with a line that opens your User Form.
Once the first half-macro has finished, and the User Form is waiting for you to click "OK", you will be able to edit workbooks. Once you click "OK", the second part will start.
It is probably best to first consider if the "edits" you need the user to make are possible via VBA, a User Form, or a DialogBox (which include the likes of the "Select Range" DialogBox, or the "Colour Picker" DialogBox)
I would recommend you not to try to edit the Workbook while a macro is running- I don't even think that is possible at all. Furthermore, try not to refer to ranges by activating/selecting ranges.
Activesheet.Range("I1:I100").Autofilter 'Instead of this use the code below
With Workbooks("AAA.csv")
.Sheets("NAME").Range("I1:I100).Autofilter
'More code
End With
Like Chillin mentioned, you could assign hotkeys to your filter macros- split them in two. Other than that, you can use buttons to activate the macros.
To assign a keyboard shortcut to a macro:
Press ALT+F8 to open the macro dialog box. Select the macro, and click on Options. In the window that opens you can assign a keyboard shortcut to the macro you selected.

User Form action in Excel Sheets

I got confused about something. How can i control the excel sheet via userform?
For example:
Open user form by clicking form button on excel sheet
Create new excel sheet by clicking the button in user form.
Do i need to write the required code in the "module" section of vba or the "userform" section?
First, Create a new Module, and inside add a Sub, like the code below:
Public Sub Main()
' call a Use_ Form1
User_Form1.Show
End Sub
Second , in your Sheet, once adding a Button, you will get a message box to add Macro Name, Select Main. Now, once you click on your Sheet's Button, your Main module will run, and it will Call the User_Form1.Show.
Third, in your User_Form, add a button (in this example it's Btn_1), once you click it, add the simple code below (just for testing purposes).
Private Sub Btn_1_Click()
' add a Sheet to this workbook, after the last one, and name it "Test"
ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "Test"
End Sub
And that's an example how to connect between the 2 objects.

Excel VBA macro runs after an object has been moved

I know how to assign a macro to a cell and launch when it has changed.
I know how to assign a macro to a 'button' and launch it when clicked.
I know how to assign a macro to a worksheet and launch when any cell has changed.
But now ..... I have a text box in a chart and wish to have a macro launch AFTER the text box has been moved to a new location. Currently, I click on a 'button' after I move the text box and this works well. I would like to eliminate the final step in order to avoid a problem if the user fails to click the button.
If there is no built-in Event associated with a worksheet property or characteristic you wish to monitor, you can always use an Application.OnTime macro to monitor the item.
For example the TopLeftCell.Address of a Shape can be monitored from call to call and action taken if the property changes.
Some some information see OnTime
I think you can found what you want Here.

Resources