Excel 2013 VBA: Setting activeworkbook when clicking between workbooks - vba

I have a workbook with a userform that contains a listbox that is used to populate data on a sheet.
If I have multiple workbooks open and I click from one workbook directly to the listbox on the userform in the other workbook, the ListBox_Change event fires before Activeworkbook changes to reflect the workbook that contains the userform. So when the code reaches Set EqDataSht = ActiveWorkbook.Worksheets("Equipment-Data") I get a subscript out of range error because the workbook I'm coming from doesn't contain a sheet named "Equipment-Data".
What is the best way to set the ActiveWorkbook to the parent of the userform? Thoughts I've had are setting a public variable wb = ActiveWorkbook on workbook_open or just trapping Err.Number=9 and telling the user to click on the sheet before clicking the userform. I'm sure there is something simple I am completely overlooking (VBA amateur).
Thoughts?

Instead of activeworkbook use thisworkbook which returns the workbook in which the code resides.
To make it active thisworkbook.activate should work

Related

Excel VBA Allow user to select an already open workbook and attach variables to selected workbook

With Excel 2013 VBA, I am familiar with setting the value of a Workbook variable with:
Set oWBSource = Workbooks.Open(strFileToOpen) 'Works well.
In my situation though, sometimes the Workbook is already open on the desktop. I am trying to find a more elegant way to "select" that spreadsheet and then attach my variable to that workbook to operate on. The code is running on a separate "master" spreadsheet.
Currently, I am asking the user if the file is already open. If so, then:
Dim rng As Range
Set rng = Application.InputBox("Select any cell on workbook to operate on.", "Click Workbook Cell", Type:=8) 'Stops code and allows user to select a cell on an open spreadsheet
Dim sTest As String 'variable used to test.
sTest = ActiveCell.Value 'Testing to see what value code is seeing.
sTest = ActiveWorkbook.Name 'Testing to see what value code is seeing
Set oWBSource = Workbooks(ActiveWorkbook.Name) 'Works if selected Workbook is maximized and minimized.
If I run the above code and when it runs the inputbox, if I click one cell on the spreadsheet I want the code to operate on and allow the code to continue, the variables still reference the Workbook where the code resides rather than the "selected" workbook. If, while selecting one cell, I also maximize and minimize the spreadsheet (basically Activate it) and then click a cell and allow the code to continue, the active spreadsheet is set and it seems to work. I realize the cell reference is doing nothing. But the pause allows me to "Activate" the desired spreadsheet.
I am looking for a more elegant way to handle attaching my code to an already open spreadsheet. My current solution is clunky. Isn't there a way to have the user select the open spreadsheet and then have my code "act" on that spreadsheet?
I know this is a weird question. Be kind rating it please??!!
If I understand correctly what you're trying to do, just use the rng to resolve the selected Workbook:
Set oWBSource = rng.Parent.Parent
rng.Parent is the Worksheet a Range is a part of, and the .Parent of a Worksheet is its Workbook.

Reference Excel Workbook made by Worksheet.copy Method

I am trying to write a macro to copy worksheets into a new Workbooks using the .Copy(MSDN) method and then save and email these newly created files out.
To do this I will need a reference to the newly created worksheet in my macro. I haven't found a way to do it directly with the copy and am hesitant to always look for Book1.xlsx.
Is there a way to grab the most recently opened workbook or easily compare before and after collections of workbooks?
You can tell the worksheet Copy method to place the sheet Before/After a sheet in another workbook. So create a new workbook and then copy your sheet to before the first sheet in the new workbook.
Dim newBook As Workbook
Set newBook = Workbooks.Add
Workbooks("source_book.xlsx").Worksheets("sheet_name").Copy Before:=newBook.Worksheets(1)
You've then got a valid workbook reference to the book that holds the copy of the sheet.
Oh alright then.
Dim origBook As Workbook, newBook As Workbook
Set origBook = Workbooks.ActiveWorkBook
yourcode..yourcode..yourcode.Copy
Set newBook = Workbooks.ActiveWorkBook
Something like that.

Macro visibility in other excel sheets

I created the macro in one of the excel workbook (abc.xlsm). And I want only that particular workbook to display the macros in view macros popup. But when view macros from other workbook (xyz.xlsx) I am able to view the macros which I created in the abc.xlsm.
I tried with private keyword before the macro. But it will hide the macro visibility in all the workbooks including the first workbook (abc.xlsm)
IS THERE ANYWAY TO RESTRICT THE MACRO VISIBILITY ONLY IN THE WORKBOOK WHICH IT WAS CREATED?
Excel macros popup lists all the macros available for execution in all workbooks that are open in the current Excel instance. This means that the only option to execute the macro only from a specific workbook is to check if ActiveWorkbook is the workbook you want the macro to be executed from. You can accomplish that with this line on top of your sub code:
If Not ActiveWorkbook.Name = "abc.xlsm" Then Exit Sub

Select worksheet in Excel add-in to run VBA macro

I have created an add-in using VBA which is a workbook with calculations. The add-in has a userform to extract relevant information from access database and populates the workbook. After the data is populated, calculations are performed in Sheet1. I need to paste the worksheet "Sheet1" from the add-in worksheet to a new workbook on running the add-in macro.
When I run the add-in however, the worksheet appears to be hidden so my data is not updating. I get this error:
" Run-time error '1004': Method 'Worksheets' of object '_Global' failed".
Can someone tell me how to work with an add-in which has a worksheet where the required calculations are performed?
The intriguing part is when I load the add-in after removing it from the list of add-ins in excel, it runs perfectly. But when I re-run the macro, the worksheet becomes hidden, so the same error appears. I am fairly new to VBA so any suggestions would be appreciated!
Edit
Code:
Private Sub OptionOK_Click() 'On selecting OK from userform
Dim ws1 As Worksheet
Sheets("Sheet1").Visible = True
Set ws1 = Worksheets("Sheet1")
'User Form Validation
If Trim(Me.cboData.value) = "" Then
Me.cboData.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copies data to given cell in excel
ws1.Range("A1").value = Me.cboData.value
'To copy selection from "Sheet1" into new workbook
Workbooks("myaddin.xlam").Sheets(1).Copy
End Sub
I get the error on ...> Sheets("Sheet1").Visible = True.
I just realized that I had to use "ThisWorkbook" in the add-in VBA code.
Set ws1 = ThisWorkbook.Sheets ("Sheet1")
VBA code within a workbook should use "ThisWorkbook" to reference to sheets or ranges inside the add-in.
If you know what sheet it is and you have access to the add-in code just make sure it's visible before the line that throws the error.
Sheets("Sheet3").Visible = True
I suspect you have another problem though because you can still reference a hidden sheet in code.
Are you sure this line is correct:
Workbooks("myaddin.xlam").Sheets(1).Copy
Before you were referencing the name of the sheet now your referencing the position of the sheet in the workbook.

Linking un-named workbook to variable in VBA

I am writing a macro that will copy and paste information form one workbook into another workbook in excel 2010. The workbook that the data is in is the same workbook as the macro. I have made VBA create a new workbook to paste the data in. How do I assign the new workbook that VBA has just created to a variable.
Thanks For Any Help
You haven't mentioned exactly how you create the workbook, but you can set a reference to the new Workbook object in the same statement that creates it.
Example:
Option Explicit
Sub AddWorkbook()
Dim oWb As Workbook
Set oWb = Workbooks.Add
'Do something with the new workbook
Debug.Print oWb.FullName
Set oWb = Nothing
End Sub
try seeing names of all workbooks by iterating over Workbooks. I think the name of newly created workbook will "Workbook1" until there is already no other unnamed workbook. So basically newly created workbook is still not unnamed.