Excel - VBA - Add-In - Worksheet - excel

I created an excel macro "add-in" for the first time. So now I can use the same macro across multiple workbooks using the quick link at the top of the workbook.
My issue is that the first command of my Macro is to add in a sheet "Sheet1". My workbook has 2 sheets in it currently. "Attrition 2017" and "Attrition 2018".
When I added in "Sheet1" the first time nothing happened, and the rest of my workbook errored out because of it. The second time I went through it said "Sheet1" already exists. This is the only workbook I have open. I tried it with numerous sheet names. it keeps adding sheets to an unknown spot then stating that they already exist. the rest of my code works with the add-in.
My code for adding in the worksheet works when not using the add-in function. here it is.
Dim ws As Worksheet
With ThisWorkbook
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = "Sheet1"
End With

ThisWorkbook is the workbook where the code is running - in this case your add-in.
You probably need ActiveWorkbook here

Related

VBA: Sheet of a named workbook not working

I'm trying (and failing) to write a little macro that selects a range from one workbook to transfer to another.
I named a Workbook 1 "InputWB" and the other Workbook "OutputWB".
Why is the following code not working? Any ideas are more than welcome.
Dim InputWB, OutputWBAs Workbook
InputWB.Worksheets("Database").Range(Cells(1, 2), Cells(4, 34)).Copy
And then I will past it in another outputsheet, but for some reason the Workbook.Worksheet does not seem to work.

Open Second Workbook Triggers VBA Code in First Workbook

I open an Excel workbook which has some VBA code in it including Event code on one of the worksheets. I open a second workbook which has no VBA code in it at all but as soon as the second workbook opens, it triggers the VBA code to run in the first workbook. The code fails because the first workbook is not the Active Workbook. Anybody got any ideas why opening a second workbook should trigger event code in the first workbook?
I can replicate that -seems like opening a workbook triggers the calculate event in other open workbooks. Having said that, your event-handling code should not rely on any particular workbook being active when it runs, so it would help to post that if you want suggestions for fixes.
For example the worksheet object which corresponds to the sheet module can be referenced via Me, and the workbook containing that sheet is Me.Parent
Private Sub Worksheet_Calculate()
Debug.print "calculating " & Me.Name & " in " Me.Parent.Name
End Sub

Copy data to master workbook from another open workbook without specifying FileName

is there a way where I can copy and paste data from any open workbook without using Workbooks.Open("FileName") ? I want to be able to copy the same set of data from a workbook, close it, and when I open another workbook, the macro would know that I am copying from the second workbook.
For example:
Code 1: Copying from first data source
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users\HONL120\Desktop\Sept HC Reports\HR Headcount Report 2018 Australia SEPTEMBER.XLSX")
Code 2: Copying from second data source
Set wb1 = ThisWorkbook
Set wb2 = Workbooks.Open("C:\Users\HONL120\Desktop\Sept HC Reports\18 09 Malaysia HR Headcount Report 2018 (Sept).xlsx")
From the example above, I would like to avoid this and let the macro know that when I open the second data source, I am pulling data from it, without having to type out the filename over again.
Is there a way to achieve this? I hope my explanation is clear and thanks in advance!
I suppose you could loop through the open workbooks, and use an If Statement to ignore the ones you don't want to include:
Dim wb As Workbook
For Each wb In Application.Workbooks
If wb.Name <> "PERSONAL.XLSB" And wb.Name <> ThisWorkbook.Name Then
Rem: Do what you need to do with the other workbook
End If
Next wb
Sorry i didn't quite get what your goal is, but I suppose you can set a macro in your excel template file (please google on where it is locate, as it depends on your version), with the code activeworkbook.range(xxx).copy.
In this case whenever you open a new workbook you want to copy, run this macro in that excel (you can even set the macro to the quick access bar for easier access)
Hope this help.

ISO How to program ActiveX button in Workbook 1, to run macros in Workbook 2?

Situation:
I have two workbooks:
Workbook #1: A Downloaded data set from online data repository
Workbook #2: A Master collection of Macros
I have built a collection of Macros to format a data set after it's been downloaded from an online repository. This data set can differ greatly based on features that users have chosen before downloading the data. The macros I created cover all possible scenarios. Thus, when opening the View Macros dialog box there are an overwhelming amount to choose from. Even with efficient naming conventions it's too much to sift through for my audience, who has an average to low experience level with Excel.
In order to simplify their experience, I wanted to level the playing field by simply providing a "Go" button after they choose from an ActiveX dropdown list.
I successfully created the dropdown, populated the list, and upon activation of the "Go" button, the selection triggers a specific Macro to run.
MY PROBLEM:
I need the Subcode on the "Go" button in Workbook #2 to force the macros to run in Workbook #1.
Thank you ahead of time for all your help!
You can take cues from the macro recorder.
Here is some basic code that is stored in one workbook and manipulates cells in another workbook.
Sub Macro2()
Dim mlib As Workbook
Dim wb As Workbook
Dim ms As Worksheet
Dim ws As Worksheet
Set mlib = ActiveWorkbook ' the file with the macros
Set ms = mlib.Worksheets("Sheet1")
' write into the macro workbook
ms.Range("B3").FormulaR1C1 = "asdf"
'activate another workbook that is already open and is called somefile.xlsx
Windows("somefile.xlsx").Activate
' set variables to reference that workbook
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
' write something into somefile.xlsx
ws.Range("B1").FormulaR1C1 = "copy me"
'copy something within somefile.xlsx
ws.Range("B1").Copy ws.Range("B2")
' copy something from the macro workbook to the somefile workbook
ms.Range("A1").Copy ws.Range("A5")
' activate the macro workbook
mlib.Activate
End Sub
I asked for some sample code so I could explain in your context.
There are about a dozen different ways how you can identify the "other" file instead of hard-coding it into the macro. If you could be bothered to provide a bit more information, that little detail could also be taken care of.

Cannot see excel sheet in VBE

I am working with an Excel file that was created by somebody else.
One sheet containing Macros appears to be password protected, but what I don't understand is that I cannot see it in VBE under the sheet list. The sheet tab is visible in Excel, but I cannot see the content.
Is there any way to unhide it in VBE?
One sheet containing Macros
Does that refer to Excel 4.0 macros?
Worksheets containing Excel 4.0 macros don't appear to be visible within the list in VBE.
They do appear to be accessible from VBA to some extent: using Excel 2007 I inserted an Excel 4.0 macro sheet to a workbook then tried the following:
Public Sub TestAccessToXL4MacroSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet ' succeeds
Debug.Print ws.Name ' outputs "Macro1"
Set ws = Worksheets("Macro1") ' fails: "Subscript out of range"
End Sub
As far as I know, there is no way you can hide a sheet from VBE! However, you can rename it there (in effect changing the .CodeName of the worksheet). Thus, if you know the Excel worksheet name (the one you see in the Excel worksheet tab), but cannot find it in VBE, go to the Immediate window in VBE (Ctrl-G) and run
? Worksheets("YourName").CodeName - this should give you the name under which it can be found in the VBE Project tree.

Resources