How can I delete VBA objects in a Recovered Workbook? - excel

I was working with VBA when Excel crashed.
I recovered the workbook but instead of having named sheets, it created a Workbook? object for each sheet.
I cannot delete these objects with the GUI, and which means I also cannot change the sheet names back.
I get an error:
Invalid Property Value
I assume this is because of the object. (Note: I can change the sheet name to something else.)

Related

How to use same macro to read all worksheets

I have a excel workbook which already has a macro say "covert". It has two worksheets. First includes configurations (variables) required to run macro and second includes rows (data) on which macro is applied. On first sheet there's one button on-click of this rows are converted into JSON. I checked configuration of this button, 'convert' macro is assigned to it.
Now, I want to make a copy of second sheet which will have similar data and I want to use same macro to read this newly created sheet with slight change in macro.
As soon as I copy sheet with data, I can see macro is also duplicated.
To make macro to decide which sheet should be read, I've added a row in first sheet and then I am adding below code to fetch config.
Dim configSheet As Worksheet
Set configSheet = ThisWorkbook.Worksheets("Configuration")
With configSheet
VAR_SHEET = .Range("B8").value
Then selecting particular sheet using below code.
With ThisWorkbook.Worksheets(VAR_SHEET)
Now, the problem is even after making changes in macro, it is always reading first sheet instead of considering variable.
When you copy a sheet with code like that, any buttons on the sheet do not "auto-adjust" to call code in the copy: they still call the same subs as they did originally (assuming non-activeX button).
Any buttons on the sheet which call code in the sheet module will need to be re-linked to the code in the copy.

Unique Name or GUID Property of the Excel workbook sheet

Is it possible to set a Unique GUID Property of each the excel workbook sheet. So that even when user renames the sheet I could be able to retrive the correct sheet programmatically via sheet Unique GUID.
We are creating a plugin which should be able to point to correct excel sheet even when user renames it to a different value.
Could not find any handle or property in excel sheet object.
Microsoft.Office.Interop.Excel.Worksheet sheet1 in ActiveWorkbook.Worksheets
This is a long shot but I'd set the (Name) field for the worksheet in Excel directly (from the VBA Editor) ...
... and see if that is then accessible from the CodeName field in the interop properties for the workbook in C# implementation.
If you're not familiar with how to get to that editor, in Excel, press Alt + F11
Obviously after you've made that change, save the workbook and then try again from your C# project.
That field is for that exact purpose but it's a design time field only, you can't set it at run time. It's used so if the name of the sheet changes or it's index changes, you can still reference it via the technical name (CodeName).
I'd be interested to see how that goes and if it then works for you.

Referencing unopened excel workbooks in a formula returns a #REF! error

I created a directory in Sharepoint with many workbooks where my team inserts the information I need to create a dashboard.
To do this, I am trying to create (in another file) a table that uses an index/match formula to retrieve the data I need.
It works just fine, except if I open the workbook without the referenced workbook being opened the formula throws an error and all rows returns #REF!.
How Do I fix it?
Instead of referencing the workbook as
='[Filename.xlsx]Sheet1'!$H$1
Refer it with the entire path
='C:\Users\[Filename.xlsx]Sheet1'!$H$1

Excel Dynamic Formula/Reference Workbook

I'm trying to create a dynamic reference within a formula; meaning the referenced workbook and subsequent sheet name can be changed and the appropriate data loaded.
I'm aware that a static formula reference could be done as follows:
=SUM('C:\Reports\[SOTDJ17.xlsx]Summary'!C10:C25)
but I want to be able to change the workbook name, and it will reference information from that particular workbook and change the data. The workbook name
is stored in B5 and the sheet is stored in B8. I thought I might be able to use
something simple like:
=SUM('C:\Reports\[B5]B8'!C10:C25)
but it doesn't seem to work. Is there a syntax error or something else I am missing?
(I should note that I am trying to reference a closed workbook)
You'll want to use Indirect():
=SUM(INDIRECT("["&B5&"]"&B8&"!$C$10:$C$25"))
Note that the workbook you're referring to must be open.

Excel #ref error: Occurs when data is refreshed

I have made a connection in excel to another excel workbook, using the native functionality found under the data tab in excel. I imported the data into my "Main" excel workbook from the "Other" workbook by Selecting Data->existing connections and then opening the relevant sheet. This allowed me to link the data from my other workbook to my Main workbook.
My issue is when the data in the Other workbook is changed i received a #ref error in my Main workbook. The formulas i use on the imported data are pasted in a separate sheet (i tried pasting it next to the imported data as well). Note: i paste the formulas from row 1 - 500 for the respective column, so that when the data is imported the formula automatically populates the cell with the correct value. When i refresh the data (i.e when data has been changed in the Other workbook) It seems as the formulas i have copied down do not reference the correct cell anymore.
I have tried using an indirect but to no success.
This is what the formula i use looks like
=IF(INDIRECT("A"&ROW(#REF!))<>"",VLOOKUP(INDIRECT("A"&ROW(#REF!)),'Safex Codes'!$A$1:$H$1000,8,0),"")
However when the data is refreshed "SOME" of the rows in the column the formula is in return this formula
=IF(INDIRECT("A"&ROW(#REF!))<>"",VLOOKUP(INDIRECT("A"&ROW(#REF!)),'Safex Codes'!$A$1:$H$1000,8,0),"")
This occurs everytime i refresh the data. What is strange is that at the end of my data, hence the last time the formula is being called, the referencing to the correct row is completely off.
For example in row 28 i would expect the above formula to reference a28, however i receive
=IF(INDIRECT("A"&ROW(A24))<>"",VLOOKUP(INDIRECT("A"&ROW(A24))
I'll bet that you're having the same issue I was. When the number of rows in the data range changes, the unused cells previously populated with data are deleted. The formulas that referenced those deleted cells no longer have references to them because they've been deleted (hence the #REF errors).
You can change Excel's behavior with respect to handling new or unused rows using the Properties menu on the Data tab.
In that menu, take a look at the section labeled "If the number of rows in the data range changes upon refresh." I'm guessing you have the first option checked. Try the third option.

Resources