Excel VBA: Use a sheet from the same workbook? - vba

The scenario: I have an Excel workbook with a macro that uses a template (from "C:\Users\User1\Desktop\1.xltx") to create customized sheets.
The problem: I need to make Excel to use the template from the same workbook - I have added the template as a separate sheet ("temp1"). I can't seem to figure it out, even though it's probably very simple.
The current code:
Set wks = Sheets.Add(After:=Worksheets(Worksheets.Count), Type:="C:\Users\User1\Desktop\1.xltx")

You probably don't want individual copies of the template for each user. If something on the template changes, you would have to update every copy.
Have you thought of talking to your SysAdmin and storing the template (1.xltx) in a common location with Read Only permission for the users?

Related

Is there a way to copy an entire Spreadsheet with only values and no formulas/links (Google Sheets)?

The issue I am facing is trying to automate my weekly occurrence of coping an entire Spreadsheet to make a copy of it. Each week I need to hit "File --> Make a copy --> share with same people" and after doing so on the copy I need to hit "Allow Access" multiple times for each table that requires access, if I do not do this, there is no data displayed.
So I am wondering if there is a way to create a copy of a Spreadsheet where the copy contains entirely plain text and no formulas or links that way all the data can be read as soon as a copy is made.
This could be a separate question, but if anyone also knows how to automate hitting the "allow access" button for multiple tables in the copy that would also be helpful.
*To give an idea of the layout, essentially I have a main Spreadsheet (the one I make a copy of) that references data from other Spreadsheets (that are linked to google forms), and then I make a copy of the main Spreadsheet, and in making this copy is where I am required to hit the access button for each table.
Answer:
You can do this with Apps Script.
Code Example:
function duplicateSpreadsheet() {
const idOfSheetToCopy = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var file = DriveApp.getFileById(ifOfSheetToCopy).next()
const newFile = file.makeCopy()
SpreadsheetApp.openById(newFile.getId()).getSheets().forEach(function(sheet) {
sheet.getDataRange().setValues(sheet.getDataRange().getDisplayValues())
})
}
Code Rundown:
Define Template Sheet ID
Make a copy of the sheet using DriveApp
Get the ID of the newly created sheet and open it with SpreadsheetApp
Loop through all sheets in the new copy and replace all cell values for the cell's display value
Instead of pasting with "Ctrl+V" use "Ctrl+Shift+V" for pasting without formatting. This will ignore things like links, font, and font size.
Not sure about the allow access button.

VBA - Pushing data from one workbook to another, irrespective of file names

Been in a pickle for a while (week or so) here and was hoping someone in this magical community could help me out. There is likely a very easy solution for an experienced individual, which I am not.
First, my goal is as follows: Push data from Workbook A to Workbook B via macro.
Conditions:
Workbook A must be able to be renamed without compromising the macro (it is a tool used in day-to-day functions and saved as a new name each use). Workbook A holds the macros.
Workbook B receives the data. Its name will also change with time, but in this case it needs to be based off written text in a cell from Workbook A (name change about yearly due to versioning) Let's just call it Cell A1 for argument's sake.
There may be additional workbooks open at the same time, related or unrelated.
To keep it simple, I will just post one line in my current macro, but I will be applying this to dozens. This works when I do not rename the files. I likely need help defining variables (strings?) and direction in what functions to use.
Windows("Workbook_B 4.7.5.xls").Activate
'*Workbook B name will ideally be derived from a cell value in Workbook A*
Range("V12") = "='[Workbook_A V1.2.5 .xlsm]SHEET_A '!R8C7"
In this chunk, the goal is to activate Workbook B and copy the value (or formula if it is easier), from cell G8 on Workbook A Sheet A to Workbook B(sheet is already active and sheet names will never change in either workbook).
Cheers!
"Workbook A holds the macros" - in this case you would use
ThisWorkbook
to refer to Workbook A
...and
Workbooks(ThisWorkbook.Sheets("Sheet1").Range("A1").Value)
to refer to Workbook B

Renaming active workbook in VBA

Is there a way to rename an active workbook in VBA? The workbook name (MTD_Truck_October2017.xls) will change every month I run a big macro on, and it references the name at one point. I'd like to rename it at the beginning to say MTD_Truck_Current.xls, then I can reference that name which will then be consistent for every month.
Also, the macro will need to run on several users computers. I originally just saved to my desktop as a certain name, but now that others will be using this, the path to their desktop is different, so I either rename the workbook at the beginning, or I have to change the path in the macro for every user.
Any ideas? Thanks!
The workbook that holds a macro can be referred to as ThisWorkbook. It has a path and Name property. Rather than hold the name of the workbook as anything, use
ThisWorkbook.Name

Excel Reference External Sheet

An excel question for you gurus. I've tried searching high and low and haven't come up with an effective solution.
I'm trying to create a formula that will lookup a value in an external sheet. I'm using the SUMPRODUCT formula and it works perfectly. Formula is below:
=SUMPRODUCT(--('File\Path\[file.xlsx]SheetName!$D$1:$D$1000=$B3), --('File\Path\[file.xlsx]SheetName'!$O$1:$O$1000=$A3), 'File\Path\[file.xlsx]SheetName'!$Q$1:$Q$1000)
The issue I'm running into, however, is that the source file is updated every day. Although the workbook name stays the same, the sheet name changes. A random string gets assigned to the source sheet name each time it is updated. As such SheetName becomes SheetName ase341.
Is there a way to have the formula read the external sheet number instead of the name? I want the formula to update regardless of the sheet name. If there's no way to read the sheet position is there a way to change the sheet name via a formula in an external workbook?
Usage Example
I have a workbook (analysis) and it pulls data from another workbook (source). Source is updated every day with new data. The data in Source is updated by downloading a report from the internet and saving over the old source file. As such, the file name stays the same but whatever is inside the file is always different (including the sheet name). There is always only ever one sheet in the Source with the same number of columns, always in the same position.
There is a really neat way to refer to a block of cells in an external workbook in which the sheetname or even the block address may vary. Say we have:
=SUM('C:\Users\James\Desktop\[Book1.xlsx]Sheet1'!$B$2:$B$9)
however the sheetname may vary. First assign a Defined Name to the block in Book1 (say XXX)
Then we can use:
=SUM('C:\Users\James\Desktop\Book1.xlsx'!XXX)
It does not matter if the sheetname changes, the Defined Name will change with it!
Your issue would be most efficiently solved with VBA, but if you're just getting started this might not be the best route.
You can get the sheetname or filename with just a formula, though:
http://www.ozgrid.com/VBA/return-sheet-name.htm

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal