I have a workbook setup for users that have a few cells index/matched to a serperate workbook. The issue I am running into is these user books are sometimes opened on Mac, and sometimes PC, causing issues with the Index/Match formula. My first attempted solution was this:
ThisWorkbook.UpdateLink Name:=ThisWorkbook.LinkSources
However when this was assigned to a macro button, it causes a dialogue box to open for the user to select the source.
I instead have tried to set up two seperate macros; one for Mac and one for PC (Not unusual on this sheet as i've had to duplicate a fair bit for Mac/PC compatibility), and these macros paste the required formula directly to the cell. However, this still causes the choose source dialogue to open.
Dim wb As Workbook, ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Sheets("Tracker")
ws.Range("J3").Formula = "=INDEX('[Workflow 19-20 LIVE.xlsm]PLAN'!$F:$F,MATCH(I3,'[Workflow 19-20 LIVE.xlsm]PLAN'!$A:$A,0))"
Is there any way I can avoid the dialogue box situation? I really don't want to give users any options here.
Related
I am new to VBA!
I have a workbook A that I use as a template for spinoff workbooks B, C, D, etc.
I made an error in formulas range A36:E37. I need to correct it in all the subsequently created workbooks, which can have any random name
I want to open the corrected master workbook A, and copy range from A to whateverworkbookname
Every time I use thisworkbook refrerence, it pastes the data to my personal macro workbook, same thing with activeworkbook.
I'm sure there's a simple solution, (like assigning a variable to the freshly opened workbook that needs fixing?) but I don't know how to do that.
Help is much appreciated!
Also of note, I am planning on manually opening the whaverworkbookname, then VBA unprotecting the sheet, copy paste function, protecting the sheet, saving, and closing the whateverworkbookname book when the macro completes, to be repeated with the rest of the incorrect workbooks.
If there is a smarter way to do this (which is probably way over my head) like applying a macro to all workbooks in a folder for instance, I would be interested in a point in the right direction to learn about it.
First a note:
ThisWorkbook always refers to the workbook the code is written in. This never changes.
ActiveWorkbook is the workbook that has focus (is on top). This changes easily with a mouse click on any workbook.
The issue is probably that you run the code from VBA editor. But if it is in your personal workbook and you run from the editor, then as soon as you are in the code your active workbook is the personal one because that is where your code is and if you click there to run the code it has focus.
You can check if the active workbook is the personal one
If ActiveWorkbook.Name = ThisWorkbook.Name Then
MsgBox "The active workbook is the personal one. Make sure to focus on the correct workbook."
Exit Sub
End If
'rest of your code
Create a button or link a ribbon button to launch your macro and use ActiveWorkbook in your code.
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")
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.
I am trying to make another worksheet active when command button is clicked, but I'm staying within the same active workbook:
Sub Submit_Warranty()
'Set warranty data worksheet as active page
Sheets("Sheet2").Activate
'Show the submit warranty user form
Warranty_Input.Show
End Sub
I keep getting "subscript out of range" error. Any ideas?
If the code you posted is everything, then that error pretty much has to be from an invalid reference. So my guess would be that the actual displayed name is something like "Warranty_Data", while "Sheet2" is likely the VBA object name (maybe you're using them in reverse).
There are a lot of ways to select a worksheet, with various advantages and disadvantages. When it comes to selecting by name, the major gotcha to watch out for is that sheets actually have two names assigned, and you're employing both methods of selection in the code you posted. The one name is what's displayed in the sheet's workbook tab, the other name is internal to VBA. Here's a screenshot to demonstrate how to use both types of names.
I have search throughout this site to find a answer to my problem and most of the related solutions are for a far more complicated problem. Here is what I need to have done. I created a simple form in Excel 2007. I am looking for the ability to add a button at the bottom of the form which allows the user to click on the button and copy that worksheet into a new worksheet within the same excel document. Basically just duplicating the active worksheet.
I tried to do it with macros but did not get the desired results, and most of our co-workers still use Excel 2003 so I am not sure if macros will work in the older version of excel. I do not know any VBA which is why I come here in search of help from you all.
So to recap.
One sheet Excel document with a simple form and a command button at the bottom of the active worksheet
The command button "Copy and Paste" that worksheet into a new worksheet within the same excel document
A solution that could work in both Excel 2003 and 2007 if possible. If not, for 2007.
Thanks so much ahead of time for anyone who is willing to help out a Novice Excel User.
Assuming that you know how to add a button here is a simple line of code to duplicate the active worksheet:
Sub Button1_Click()
ActiveSheet.Copy after:=ActiveSheet
End Sub
Maybe something like this (tested in Excel 2003 only):
Dim srcSheet, dstSheet
Set srcSheet = ActiveSheet
Sheets.Add
Set dstSheet = ActiveSheet
srcSheet.Activate
srcSheet.Cells.Select
Selection.Copy
dstSheet.Activate
dstSheet.Cells.Select
ActiveSheet.Paste
You should find this method will work in both Excel 2003 and Excel 2007. In your form, add the following method:
Sub CopySheet(WorkSheetName as String)
Dim WrkSht As Worksheet
Set WrkSht = Sheets(WorkSheetName)
WrkSht.Copy After:=Sheets(WorkSheetName)
Set WrkSht = Nothing
End Sub
From the button click event, call it using:
Sub Button1_Click()
Call CopySheet("WorkSheetToCopyName")
'You could also replace the string name with ActiveSheet if you so wish
End Sub
This will dump a copy of the worksheet in between the current sheet and the next one. I've tested it in Excel 2003 and Excel 2007 and it works in both. It doesn't give the second one a pretty name sadly - it just gets the same name as the source worksheet with (2) put after it.
All the formatting, protection and formulas are copied across too - it's a carbon copy of the first.
I know the question is quite old, but just wanted to note that you (and the user) can do the exact same thing with zero code: right-click on the sheet name at the bottom and select Move or Copy..., then check the Create a copy box and click Ok. Yes, it takes 4 clicks, but it is super easy and avoids code.