Copy tabs/sheets from one workbook to another with macro/vba code (1 button) - vba

two (hopefully) easy questions -
Can I copy 2 tabs from File1.xlsm to a master.XLSX file and still run the macro code? I assume not?
Assuming I cannot do #1, can I copy the 2 tabs into another master.xlsm file? If so, please help I've tried the following.
File1.xlsm just has a grid with a 1 button that calls 'File1.xlsm'!getData which populates the grid.
I was able to right click the tabs in File1.xlsm and move/copy them successfully into master.xlsm but without the vba getData Sub(). When I click the button it said it couldn't find the !getData code. Is there a clean way to copy the 2 tabs, and !getData macro code into the Master.xlsm?
Thanks a bunch in advance!

To accomplish this task, you should consider adding the relevant code into a module within your File1 workbook. You will need to enable the Visual Basic extensibility within your File1 workbook library.
You would then need to copy the module along with the two sheets into the new workbook. You would do this with commands similar to the following.
ActiveWorkbook.VBProject.VBComponents("module1").Export Path
SomeWorkbook.VBE.VBProject.VBComponents.Import Path
Where path is a string variable to a place where you would like to place the module, and someworkbook would be your Master.xlsm workbook object.

with ActiveWorkbook.VBProject
.VBComponents("sheet1").codemodule.addfromstring .VBComponents("Sheet1").codemodule.lines(1,.VBComponents("Module1").codemodule.CountofLines)
end with
sheets(array("sheet1","sheet2")).copy

Related

Preserve settings in the file

I am creating my excel add-in that saves the current file as csv into user-specified folder. I would like my program to ask for the folder path the first time the program is launched and to remember that folder in the future. I am wondering is there any way to preserve the data within the program? I figured that I could write the path into .txt-file but that feels a little hack-like solution and would clutter the addin folder.
I use the GetSetting and SaveSetting functions in my VB 6 apps. Rather than cover them in detail, take a look at this excellent web page that illustrates how to use in with Excel
Excel Tips From John Walkenbach
Create a Worksheet, and store the values in cells. Then in the VBA Editor find the Worksheet in the Project Explorer (Ctrl + R) and set "Visible" to "2 - xlSheetVeryHidden" in the Properties Pane (F4) so that it is not readily visible to users.
You can then set/retrieve the data in with code in the format SheetName.Cells(row,column).Value, e.g.
MyPath = Sheet1.Cells(1,2).Value 'Get data from cell B1
Sheet1.Cells(2,2).Value = NewPath 'Set data in cell B2
There are multiple ways to approach this. Besides the hidden sheet approach, already described, you can
Write the information to a CustomXMLPart. This is a xml file stored in the workbook's ZIP file where information can be stored.
For something as short and simple as a file path, you could also use a CustomDocumentProperty

Sheet.Activate changes sheet but continues to edit data on previous sheet

I have a dialog box with a couple of buttons that launch macros to activate and change to different sheets.
The problem I am having is after I click the button, the macro activates the new sheet and I see it. But when I go to delete data, add data or try to delete a row "Nothing happens" the data on the screen is still there. If I go back to the previous sheet, the cells and rows that I had intended to delete were deleted in that sheet. It is very wierd and never seen anything like that. It appears that my macro code is note doing enough to actually change to the new sheet. I do not have this problem if I click a different sheet tab to change to it. Or if I click the dialog button to go to the new sheet and quickly do a ctrl-pgDown and Ctrl-PgUp to change from another tab and back that seems to fix the problem.
This is the code in my macro I am using to try to change to the desired tab.
Private Sub Report1Button_Click()
On Error GoTo Handler
Sheets("Report1").Activate
If StayOpenBox.Value = False Then
Unload MainMenu
End If
Exit Sub
Handler:
MsgBox "Sheet 'Report1' not found or renamed"
End Sub
Thanks for any help or suggestions
UPDATE:
Here is code that I use to call the dialog box. I have a shape on the other sheet that is assigned to this macro to open dialog box
Sub ShowMainMenu()
With UserForm1
.Show
End With
End Sub
Also there is no further code to make edits to the new sheet. My Button click simply switches to the other sheet and when I attempt to make edits manually, they are actually done on the previous sheet which is not the one I am currently looking at. So anything I do, Bold text, delete text, delete row, etc, is not done on the current sheet I am looking at, but when I return to the previous sheet the changes where made there. Im on Excel 2013, I have reproduced this problem in 2 separate files, but I will try on a different computer and older version of excel. Screenshot of my situation is below.
UPDATE 2:
I ran this xlsm file on a 2nd computer with Xls 2007 and was not having the problem. So I ran the macro on a 3rd computer that also has Excel 2013 and it is experiencing the same problem. So it is not computer specific and seems to be a problem in XLS 2013 but not in XLS 2007. I will try to find a computer with Excel 2010 to test as well, but something about this code is causing a problem in 2013 but not in older versions of excel.
When you run VBA code, it will default to using the ActiveSheet if you don't define the Sheet. When you have objects/methods that you want performed on a specific sheet, you should always specify! You can do that one of two ways:
Sheets("Report1").[Object].[Method]
'or
Sheets("Report1").[Method]
or you can pass the Sheet name to a variable and use that for shorter code
Dim Report1 As Worksheet
Set Report1 = Sheets("Report1")
Report1.[Object].[Method]
'or
Report1.[Method]
Try changing the sequence of lines in your code. I had a similar situation and it turned out that I inserted the "delete sheet" code in between the commands that were copying data from sheet1 to sheet2. When I put the deletion lines (commands) after I finished copying sheets everything started to work correctly. Many commands activate one sheet while performing and this immediately disactivates another sheet. So if you used "ActiveSheet" sommand somewhere it may be incorrectly understood - the command may be executed not on the sheet you meant.
Just use the full address for the range you are trying to manipulate, for example instead of:
Sheets("mySheet").Activate
Range("A1:B10").Cut
Sheets("myOtherSheet").Activate
Range("A1:B10").Paste
use:
Sheets("mySheet").Range("A1:A10").Cut Destination:=Sheets("myOtherSheet").Range("A1:B10")
I just had the same problem, also with Excel 2013. So even if the thread is over 9 month inactive, I want to share my solution in case somebody gets here through a Google search.
The solution was really simple. Call the userform with:
UserForm1.show vbModeless

Click to toggle sheet tab

I am trying to use VBA to create the same action you would do if you had an Excel spreadsheet open with 3 sheet and you want to toggle between sheets exactly like when you click on the sheet tab at the bottom of the workbook (i.e. Toggle between Sheet1, Sheet2, Sheet3 by clicking on the sheet tabs)
I've tried some thing:
book.Worksheets(1).Activate = True
book.Worksheets(1).Activate
sheet.Visible = True
But nothing seems to work. I'm not sure Activate is what I want. I think that just makes the sheet active, but not visible. Visible seems to do nothing. How can this be accomplished?
book.Worksheets(1).Activate
Is designed to perform the action you want "clicking on a tab"
You can also try
book.Worksheets(1).Select
Which can be used to select multiple tabs (see Have a look at What is the difference between Sheets.Select and Sheets.Activate?)
I am guessing something is not right either with your workbook reference or something else in your code. If you make a new excel file and just try Sheets("Sheet2").Activate it will select that tab.
thanks for the feedback. I have been attempting to use com objects in python. they usually interop with each other quite well, but this is one thing that doesn't seem to work. I've tried all of your suggestions with no results.
I managed to find a workaround by just moving the sheets around using:
sheet.Move(Before=book.Worksheets(1))
For some reason, when the sheet is moved, it also activates it which is my desired end result. It works for the scenario I am scripting, but may not for other people trying to do something similar.

Running Sheet-Specific Macro from Other Sheet

I have a Excel book that has multiple sheets, subs, and macros. In order to make it a bit more user friendly, I'd like to create a dashboard sheet where the user has to just click buttons to run everything. This is possible easily with the subs, but how can I call a macro from one sheet to another? I can't seem to find any Excel documentation on this.
Put the subs in a Module as opposed to in each sheet object.
To Create a Module right click on the book name in the VBA editor and then choose Insert -> Module. Any sub will then be accessible to any sheet (or object) within that book.
If you have a Sub in a worksheet code module, you can call it from a regular code module (or any other module) like:
Sheet1.MySub
...where Sheet1 is the codename of the sheet containing the code you want to run.

How to shell to an exe and pass cell values from Excel worksheet

Is this possible in Excel?
I have a workbook with multiple worksheets. I wrote some vba code in a code module to shell to an exe and pass it cell values as arguments.
What I want to be able to do is select a cell or row in any of my worksheets and then call my Shell Sub while passing the values from a couple cells to the Sub. A hot-key combination would be best.
The part I am having trouble with is calling a sub in a code module from a hot hey.
Can you help with this? Any sample code?
Much appreciated!
Found my answer! It was in a post from Leith Ross found here...
http://www.excelforum.com/excel-programming/590157-keyboard-shortcut-to-run-sub-module-in-vb.html
If your macro is a Sub then its easy to assign a shortcut key to your macro. While in Excel, type ALT+F8 to bring up the Macros List. Select the name of your macro by clicking it. At the bottom right corner of the dialog, click "Options..". You can then assign a shortcut key and add a description of the macro for other to see when it is selected in the Macro List.
That's exactly what I needed.