Call 'Move or Copy' Dialog - excel

In EXCEL there is an on-board 'Move or Copy' dialog, which can be started by clicking right on one or more sheets. The advantage of this feature is that a little window (see picture) is opened, and the user can select the destination. I wanted to add this routine to an existing sub. I know how to copy a sheet using VBA, that's not a problem, I wanted the same little window, and I was wondering if I can use the on-board routine. I have tried recording, but the window for selecting the destination did not occur in the code. Of course, I can simply rebuilt it, but why reinventing the wheel?
Is there a way of calling the routine?

It's a built-in dialog:
application.Dialogs(xldialogworkbookmove).Show
You can also use xldialogworkbookcopy and you should basically get the same dialog but with the checkbox at the bottom checked.

Related

Excel VBA - Multiple buttons pointing to same event

I am currently working on some excel document to automate it as much as possible.
For this I have to add the same button (let's call it an Erase All button for example) in each of the sheets.
Till here is OK. I have added it and all works fine. The problem is that the onClick event code is replicated in all the different sheets as a new Microsoft Excel Object code. What I would like is all the buttons to point to a single Module so I dont have to replicate code anywhere.
I come from a .Net environment and I know that VBA may have some restrictions or different usage but wondering whether I can avoid the onClick event in every sheet and put this onClick on a single generic module.
As a workaround I added my functionality as a SUB in the module and I call it from each of the different onClick events but if it is possible to directly clean all the sheet-specific code then it would be great :)
Any clue on that?
Hope is enough clear but in case need some more description please let me know and will try to add some graph/pic
Thanks a lot in advance!!
You can assign the same macro to each of the buttons. (Just right click on the button and 'assign macro')
As long as you designate the ActiveWorksheet as the sheet to process within the macro, this should be fine.
Alternatively you could instead create a non-modal userform with the required generic buttons which then has code to work on the active worksheet when pressed

Macro button under customized ribbon tab tries to open old Excel file

I created a custom ribbon tab on my Excel like Excel_app_v1.xlsm, and a button under this ribbon tab is connected to a macro. So when I click this button, the macro does some table importing applications.
The first strange thing is that I created this ribbon tab and the button for only this Excel file, but the ribbon tab and the button appear in all other Excel files, even if the original Excel file Excel_app_v1.xlsm is not open.
The second problem is that I created a second version of my previous Excel file with "Save-as" option. So the new Excel file is like Excel_app_v2.xlsm. When I click the button under the ribbon tab, it opens the first Excel file Excel_app_v1.xlsm, even if it is not open. I deleted the first Excel file, but then I got an error like "Couldn't find the Excel_app_v1.xlsm on the path".
So obviously the macro button under the customized ribbon tab is linked to the first Excel file, but I couldn't find the menu option to change this. I added ThisWorkbook before all the sheet expressions in the vba code, but it didn't solve the problem. The button-click is still trying to open the old excel file.
The VBA code is below. The button is linked to the Sub ImportTable. Firstly it asks the user if the user wants to continue with the process. It opens the previous Excel file right after clicking on the button, at the same time as the Message Box appears.
Sub ImportTable()
Application.ScreenUpdating = False
YearMonth = ThisWorkbook.Sheets("tab1").Cells(11, 2).Value
' The Macro button opens the previous Excel file before clicking Yes or No on the message box
answer = MsgBox("Warning! Brings the newest source file. You want to continue?", vbYesNo + vbQuestion, "")
If answer = vbYes Then
RunSASCodeViaBatFile ' Another Sub which runs bat file to run a SAS-code. But it doesn't matter. Because the problem happens before I click on Yes or No.
InsertSASFileIntoExcel
Else ' Nothing happens if clicking No on the Message Box
End If
End Sub
The clue to fixing this quickly was posted below by roncruiser, with one slight twist.
Everyone on the web seems to feel that PERSONAL.XLSB is the key here — nope. In fact, playing with that file only confounded me for even longer. Here's what I did instead:
Right click the Ribbon and select Customize The Ribbon;
Navigate to the offending macros that you've installed with buttons;
Find and click on Import/Export;
Export your custom buttons (the macros will go right along just fine);
Open that resulting file, and edit out the offending references to the other file that's causing you so much grief — example:
<mso:button idQ="x1:HideRows_0_EA10D6" label="HideRows" imageMso="_3DPerspectiveDecrease" onAction="!HideRows" visible="true"/>
I took out everything after idQ-"x1... up to the actual name of the macro. I also took out the same external reference found in onAction="... Take everything up to the bang mark.
Save this under whatever name you wish, but with the same extension (for my setup, it was called ExportedCustomizations.exportedUI (yes, that long an extension));
Repeat the first few steps here, but this time import your edited file.
Voila, all is golden.
No messing around with wiping out existing work and starting all over. Worked a charm for me, so a big tip o' The Hat to roncruiser for the clue.
Just to confirm what sumgain have write above.
It works perfectly just do as he said : remove the part after the "x1:" that refers to a specific workbook until the begining of the maccro's name.
example :
When you export your custom ribbon with the maccro attached to it it will be write like below :
idQ="x1:C:_FolderName_Filename.xlsm_Fill_Formulas_Cells"
THen you remove the part mentionned and it will become like that :
idQ="x1:Fill_Formulas_Cells"
Same for onAction keep only the Maccro Name
Then it will works perfectly as long as you the maccro's name in the workbook stay consistent if you modified the Macros name then you have to modified it in the exportedUI file.
Then when you will reload the new file you can check in the Excel Options customize ribbon on the customize button if you put the pointer on you will see "Maccro: Name of your maccro"
And not the path of the file the maccro was from.
No need to use custom UI editor or any other things such as personnal maccro at least for that and if you are not bother to have custom ribbon in all of your woorkbook.
As well it is obvious but still good to remind it, you need to have the maccro in the workbook this procedure is just there to call the maccro that are associate to the workbook, it doesn't contain the code of the maccro.
Cheers
Romain
Does this still work? I have done this in the past with success but can't seem to get it to work now.
I export the file, edit it and import it back in.
it appears to work, but when i close the Ribbon options pane, my custom buttons disappear.
Same exact thing happened to me. There's a way to get around this.
By default, when you create a macro in Excel and run that macro through a custom ribbon button, that ribbon button macro works only in the workbook that contains it.
To get around this and have the button macros work in all workbooks, you'll need to create a Personal Macro Workbook. Then any macros that you store in your personal workbook on a computer become available to you in any workbook whenever you start Excel on that same computer.
Create a Personal Macro Workbook
To get the same ribbon button macros to work on another computer, you'll need to copy the Personal Macro Workbook to another computer and store it in the XLSTART folder. The link above has all the information you'll need.
Note: Delete the old ribbon button macros. Make sure you create new ribbon button macros that reference the macros from your Personal Macro Workbook.

How to delete a large number of command buttons in excel

I have a macro that I have designed to retrieve information from the internet and then to process that information into formats I want. My first worksheet in the workbook functions as my command page and all of the subsequent worksheets are populated based on what I tell the command page I want.
I have assigned control buttons on the command page to all of the various functions I want the macro to execute on the data retrieved from the web so I can select which functions I want applied. However, I believe that somewhere along the way I told my code to make copies of 1 certain command button.
There are now over 7000 command buttons for one function. Thank fully they are all stacked on one another so it isn't obvious, however it is starting to slow my excel down.
Is there any way to manage all of you buttons and then to select the ones you wish to delete?
Maybe a macro that deletes all duplicate command button names would do the trick? Before I tried this method, I wanted to see if there was an easier fix to my problem.
This should work for you:
Shift+Ctrl+Click on the button to select it
Press Ctrl+a to have it select all of them
Then Ctrl+Click the button to de-select the top-most button
Press the Del key to delete all selected objects.

events and macros in excel vba not linking

I have an existing excel sheet which has some macros written in modules. The sheet has various button kind of controls (check 1st scenario), but not a button (as I could not see the button control selected in even in design mode, as it is in the case of 2nd scenario).
So I was wondering what control it is and how can I link it with macros defined?? Also the coding is in modules and not in sheet, why??
I even tried to link a macro with the button created in scenario 2, but it didnt work out. I am new to excel vba, pls suggest some solution.
Also I would like to know more about macro, like why do we need a macro if we can write event based code on button click??
First off "Macro" is the generic term excel uses for VBA code. VBA is the language you are writing in when you "writing a macro". You can write VBA in a subroutine as part of the sheet object, workbook object, an independent module, class, or form. Events are part of a workbook, worksheet, or form object that can trigger code that is contained within the event's VBA routine.
Your Scenario 1 looks like maybe those are shapes drawn on the worksheet. Shapes can have "macros" assigned to them. You right click on the shape, and go to "Assign Macro". It will write the event subroutine in that worksheet's code in your Visual Basic Editor (VBE). You can then write the code inside that subroutine that was generated for you which may call other subroutines or functions that you have written in a module.
You can also tie subroutines, regardless of which object they are written in (module, worksheet, workbook) to a CommandButton, like in your second scenario. This particular button you added is an ActiveX control type button. They tend to be a bit more complicated. You should probably just go with a normal "Form Control" button as it will ask you, after you draw it, which SubRoutine you want to attach it to.
So to answer you question: Go to Developer>>Insert>>Form Control>>Button and draw it on. Then point it to your VBA subroutine that has the code you want to execute when you hit the button. Nice and simple.

Excel Listbox in a Sheet with FreezePanes

I have an Listbox Multiselect ActiveX control in a Sheet with Freeze Panes. The problem is that when I try to select items the mouseover icon is the standard excel cross instead of the arrow. I know that this question was asked before, but I didn't find any answer.
Thank u in advanced for help
I tested, and I came up with a few interesting observations.
In the ribbon, go to the developer tab. Make sure that the Design
Mode is not on.
When I first froze panes on the sheet, the freeze line went through the listbox I created. This caused the behavior you are describing with the cross-hairs and being unable to select.
Then I moved the listbox. Still same problem. I tried clicking on various cells around the sheet and in the ribbon area, with no change in behavior.
When I clicked to another sheet and back again, it started working as expected. Normal pointer mouse. I could select multiple, and unselect as needed.
Based on what I've tried, it seems that making sure Design Mode is off, and clicking to another sheet and back again may fix the problem.

Resources