VBA: Sheet of a named workbook not working - excel

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.

Related

Running excel macro on multiple files

I have an excel spreadsheet with a very complex macro. The spreadsheet takes a file (imported through a button on the sheet), and runs statistics on it. is there any way to automate the macro to run on multiple files in a folder?
Thanks in advance
The macro identifies the worksheets from which it draws data and these sheets are in a workbook, which is also identified. Hence, by changing the name of the source(s) in the code, perhaps even dynamically, you can make the same macro perform its magic on other workbooks.
With that said, since VBA absolutely needs a workbooks to identify a worksheet and must have a worksheet in order to identify a cell, VBA will provide a default for either if the code doesn't mention another. Therefore the innocent Cells(1, 1) or Range("A1:B10") you may see in your code in fact stand for ActiveWorkbook.ActiveSheet.Cells(1, 1) or ActiveWorkbook.ActiveSheet.Range("A1:B10"). Therefore, if you want to change the default workbook for another the process is to first introduce a variable to specify the workbook and worksheet and then change the object assigned to that variable.
In a less generic way, let me presume that you don't have syntax like Range("A1:B10") in your code at all but Worksheets("Sheet1").Range("A1:B10"), identifying the worksheet but not the workbook. Let me further presume that the sheet names are the same in all the workbooks on which you want to run the code. In that case you would make the change as shown below.
Dim Wb As Workbook
Set Wb = ActiveWorkbook 'or perhaps ThisWorkbook (=the one having the code)
' and then change all applicable instances to
Wb.Worksheets("Sheet1").Range("A1:B10")
Now you can change the code to specify another workbook simply with:-
Set Wb = Workbooks("My workbook.xlsm")

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

Excel - VBA - Add-In - Worksheet

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

Two way to address an excel worksheet but index numbers are different

I'm using Excel 2016 VBA to get to a worksheet entitled Sheet40 (TestRandomTeams).
Both the following 2 lines of code point to the same worksheet, but I don't understand WHY?
Application.Goto ActiveWorkbook.Worksheets(45).Cells(6, 5)
Worksheets(40).Activate
There is only one workbook open.

Create a workbook using a macro with no formulas, but still linked back to macro workbook?

So I built this macro workbook that creates a formatted monthly report for me.
How it works is the macro builds the report based on some criteria I define, then it copies the data portion from the worksheet "Slide" and pastes it as values to the next worksheet, "Deliverable", using this code:
Application.Goto (ActiveWorkbook.Sheets("Slide").Range("A1"))
Range("C2", Cells(TableRows, 30)).Select
Selection.Copy
ActiveSheet.Next.Select
ActiveSheet.Paste
then it formats and sorts everything on "Deliverable", and copies it to a new workbook using this:
Sheets("Deliverable").Select
Sheets("Deliverable").Copy
The only issue is when you open up the new workbook that only contains the copy of "Deliverable", it's still linked back to the original macro workbook and you get the security warning. I could care less about this as I know it came from me, but the report is shared outside my organization and I would prefer to avoid questions and/or hassles for my clients.
The new workbook is not macro enabled, and like I said above, contains no formulas, let alone those that reference the original workbook.
What can I do to address this?
OK, the links were being caused by named ranges in the workbook that came over with the copied worksheet. No formulas, just named ranges.
I added this after the worksheet copy and it took care of it.
Dim nm As Name
On Error Resume Next
For Each nm In ActiveWorkbook.Names
nm.Delete
Next
On Error Goto 0

Resources