Vb.net Updating excel formula with references to other workbooks - vb.net

I am trying to update some formulas from one workbook, to another workbook. Everything is working great until I run into a formula that has a reference to another workbook. For example a formula like this =IF(ISERROR(W!Var1),0,W!Var2) It will prompt me to open this workbook, I am assuming so that it can evaluate the formula. So my question is this. Is there a way for me to handle these situations on the fly, so if there is a workbook reference needed it will prompt me and then save it to memory? Because if I have more than one cell that contains these formulas it will prompt me to open the referenced workbook for every cell that contains the link. Alternatively, is there a way that I can just push my formula into the cell without having excel evaluate it?
So in my code I have this line which works for any value that doesn't contain a workbook reference. TheRange.RefersToRange.FormulaR1C1 = RangeFormula
Any help is greatly appreciated.

I understand that you refer to Worksheets (each of the "tabs" in a given Excel file), the Workbook is the whole file. The popping-up message appears when the referred Worksheet cannot be found. Example: range.Value = "=sheet5!A3" (in a Workbook having just "sheet1", "sheet2" and "sheet3"). If you want to avoid this message (although bear in mind that the Worksheet is not there and thus the calculations will be wrong anyway), you can write:
excelApp.DisplayAlerts = False
Where excelApp is the Excel.Application you are currently using.

Related

Excel : Printing all workbook but with refreshing (recalculating) each tab before printing it

I am having some issues with printing an excel file i'm using.
The file is made of a mastersheet, and several sheets that contain individual reports (using data in the mastersheet).
The sheets are generated from a template by a macro, and are automatically named. The sheet name is referenced in the sheet, and I am using Index/match to pull the data I need from the mastersheet. So this means that the tabs need to be recalculated constantly when I switch between them (and there's a little piece of VBA code that does it perfectly).
My issue is that when I want to print the individual reporting sheets, they are all printed with the same data (because their contents are not refreshed individually before printing).
Is there any way of printing them without selecting them one by one and individually printing them each time ?
I thought about some macro/vba code that would copy paste the sheets as values in another workbook, and then printing this workbook, but I'm not sure if its the optimal or quickest way of doing things. I would appreciate it if someone could help me.
Thanks !
Here's a start for you
Sub S
Dim ws as worksheet
For each ws in worksheets
ws.calculate
'or call your recalc routine
ws.printout
next ws
end sub

VBA - When to specify ActiveWorkbook

When writing VBA code, if you know you're going to be operating in the same workbook and on a certain worksheet, should you specify ActiveWorkbook for the workbook objects in your code there?
Example:
ActiveWorkbook.Sheets("Sheet1").Range("$A$1")
vs
Range("$A$1")
Above code assumes you are working off of Sheet1 through local macros(under Sheet1 object in VBA).
Better than using ActiveWorkbook in most situations is using WorkBooks("Book1") This is more specific and robust and I believe faster to execute than activating a workbook and then pointing to it when referencing ranges.
The VBA will default to the last active workbook if you do not designate one when using lines of code like Range("A1") So if you are working in just one workbook it is not necessary since the last active workbook should always be the workbook you want the code to execute in. I typically only designate sheets and ranges when I'm working in a single workbook since it is a little redundant to tell excel to select the active workbook when it already does by default in the absence of a specific workbook designation.
Just make sure that you will be running the macro from the open workbook that you want the code to execute in.
Including a workbook designation will never hurt and it will make your code more robust. But if you will only ever have the code execute in a single workbook it is not necessary.

Removing links from copied worksheet

What I want to do
I want a code in my workbook (wbDestination) that opens another workbook (wbBosOriginal) and copies an entire sheet as values (wbBosOriginal has a lot of code in it, in modules and in the worksheet in question, and I do not want this code because it references stuff in wbB that doesn't exist in wbDestination). I have had great problems pasting as values, because it will not paste columns and rows that are currently hidden. So this is why I decided to import the whole sheet instead.
What I tried and what's wrong with it
Here is a block of code I used to copy the worksheet in the destination workbook, in the last index position. The problem with it is that some links still exist to the old workbook (Formulas, validation lists, conditionnal formatting). I have deleted all these links but STILL when I paste the sheet successfully, save and reopen, I have an error saying some content is unreadable. I believe there are still some elements linked to the old workbook.
Set wbBosOriginal = Workbooks.Open(strChosenPath, ReadOnly:=True)
With wbBosOriginal.Sheets("BOS")
.Visible = True
'Pastes the ws in last position in wbDestination
.Copy after:=wbDestination.Sheets(wbDestination.Worksheets.Count)
Set wsNewBos = Worksheets(Worksheets.Count)
'Deletes VBA code in the copied sheet
ThisWorkbook.VBProject.VBComponents.Item(wsNewBos.CodeName).CodeModule.DeleteLines 1, _
ThisWorkbook.VBProject.VBComponents.Item(wsNewBos.CodeName).CodeModule.CountOfLines
End With
The worksheet is successfully pasted with no code in it, with everything else it had previously. I then remove all formulas, conditionnal formatting, and validation lists. Even after removing those as well, I still get an error when opening the workbook.
My question
Apart from conditional formatting, validation lists, VBA code, and formulas linking a worksheet that was pasted to a new workbook, what other elements could cause the workbook from opening in repair mode every time due to existing links to the old workbook?
If my question is not clear, comment and I will clarify.
Dealing directly with VBE seems a bit heavy-handed to me. If your code is manipulating several workbooks, I would put the code in an add-in and not have it in any workbook. (Technically *.xlam addins are workbooks, but when I say "workbook" I mean normal *.xls, *.xlsx, *.xlsm, etc.)
That said, if you're just copying cell values (which may be formulas) between different workbooks, you shouldn't have any dependencies other than cell references, named ranges, and user-defined functions in the original workbook. I would make sure there are none of those. Please also share how you are ensuring your formulas do not have broken references.
If the issue you are having is caused by trying to avoid hidden columns and rows not allowing pastevalues, why not unhide the rows and columns and then copy only the values to the new book?
Just cycle through each of the sheets in the original book and use the method .UsedRange.Hidden = False. As far as I am aware, this should unhide every cell on the sheet and allow you to do the original pastevalues calls
This works fast and smooth (it's harder to delete ALL the data Imo):
Sub tests()
Dim AllRange As Range: Set AllRange = ActiveSheet.UsedRange
Dim ItemRange As Range
Dim myWbDestination As Workbook: Set myWbDestination = ThisWorkbook
Dim SheetDestination As String: SheetDestination = ("Sheet2")
For Each ItemRange In AllRange
With myWbDestination.Sheets(SheetDestination)
.Range(ItemRange.Address) = ItemRange.Value
End With
Next ItemRange
End Sub
Repair mode can be triggered by many factors, you would need to post the code you are getting to look for an explanation, it would be like asking why vba may broke

Reference an excel sheet from another workbook without copying the sheet

Im wondering if it's possible to reference an excel sheet from another work book without making a copy of that sheet?
The situation : I have some very large worksheets filled with various data, but i don't want to keep a copy of them open in my workbooks because while each workbook uses the same data source, they're slightly different.
I have a vba routine that takes this data and creates input files for other codes, vba expects this data to be available on the defined sheet names.
Is it possible to make either excel or vba to know that when i request worksheet("Example_1") it instead knows that i mean example_1 from a different workbook?
Thanks
Yes, it is possible.
You need to add those lines to your code:
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Set wkb = Excel.Workbooks("name_of_workbook.xlsx")
Set wks = wkb.Worksheets("Example_1")
Now, every time you want to refer to a range from this other workbook, you need to add wks. before, i.e.:
'Printing value in cell.
wks.Range("A1") = "x"
'Selecting range.
call wks.Range(wks.Cells(1,1), wks.Cells(2,2)).Select
=SUM('C:\test\[test.xlsx]sheet_name'!A1:A25)
is an example of a formula which references sheet sheet_name in workbook C:\test\text.xlsx.
Note that when the other workbook is opened, the formula automatically changes to
=SUM([test.xlsx]sheet_name!A1:A25)
and then when it is closed, the formula will change back.

Excel Copy Worksheet from external WorkBook

What I am doing is copying a sheet from a different workbook to my current workbook. I'm basically doing the following:
Delete the current Worksheet in the current Workbook
Open the external Workbook and Copy the Worksheet required
This all works as expected but all references in the other sheets are lost and replaced with #REF.
Is there a workaround (other than find and replace hack) that can be done to avoid this.
Regards,
Lloyd
You could try using Clear and Copy-Paste instead:
- Clear the contents of the current worksheet
- copy the external worksheet
- paste into the current sheet
Why don't you just copy the values contained in the sheet instead of the whole sheet object?
v = Workbooks("Book1").Worksheets("Sheet2").Range("A1:IV65536")
Workbooks("Book3").Worksheets("Sheet1").Range("A1:IV65536") = v
where v is a Variant. Or,
Workbooks("Book3").Worksheets("Sheet1").Range("A1:IV65536") = _
Workbooks("Book1").Worksheets("Sheet2").Range("A1:IV65536")
This takes a couple of seconds, but will be faster if you reduce the range to what you really need ("A1:IV65536" is presumably exaggerated...)
Of course this won't do if you also have formulas in the sheet you're copying and you need those formulas in the destination sheet. It isn't clear from your question what exactly you're trying to accomplish.