Unable to get list or delete namedRange which has #REF using google API python - google-sheets-api

Unable to list or delete the namedRange using python google sheet API if the namedRange is invalid otherwise the original sheet on top of the namedRange is deleted.
Steps to simulate:
Create a new google sheet using python API, dump data into a new tab and add a namedRange say new_range (provide a name for the namedRange).
Delete the new tab which was created, now check the namedRange new_range it should be #REF
Now list the namedRanges for the above sheet, nothing will be returned but in actual google sheet there will be a namedRange new_range with range as #REF
You would not be able to add or update or delete a nameRange with the same name new_range unless it is manually deleted in google sheet.
Please suggest a solution for this.

Related

Programmatically Update Linked Named Range of excel object in MS Word (2007)

First question, excuse me if this has already been solved, but I've searched thoroughly and cannot find an answer:
I have linked several named ranges into a word document. This word doc (and the related excel workbook with named ranges) is a template: it's for a coworker who will make many copies of these templates (of both the word doc and the excel workbook).
I would like to include a command button in the word doc that, when clicked, will update the sources for the linked named ranges. Specifically, I want it to set the workbook with the same name as the worddoc, as the source.
The issue is that it does not like the named range I have entered. I get the:
Run-time error '6083': Objects in this document contain links to files that cannot be found. The linked information will not be updated.`
However, I have quadrupled-checked my excel doc, the named range exists. AND, when I hit Alt+F9 in word, I clearly see the link contains the named range!
{LINK Excel.Sheet.8 C:\Users\Marc\Documents\WIP_SSS.xlsm CED \a \p}
Here is my code:
Public Sub ChangeSource()
Dim filename As Variant
Dim fieldcount As Integer
Dim x As Integer
filename = Left(Application.ActiveDocument.Name, Len(Application.ActiveDocument.Name) - 4) & "xlsm"
fieldcount = ActiveDocument.Fields.Count
For x = 1 To fieldcount
'Debug.Print ActiveDocument.Fields(x).Type
If ActiveDocument.Fields(x).Type = 56 Then
ActiveDocument.Fields(x).LinkFormat.SourceFullName = ActiveDocument.Path & "\" & _
filename & "!CED"
End If
Next x
End Sub
If I don't enter the named range at all, the macro works, but it embeds the entire excel worksheet (which I do not want it to do). Any ideas on how/ why it is not liking the named range?
Thanks,
Marc
UPDATE:
With help from Bibadia, I found a solution; in addition, I want to document some strange behavior exhibited by Word VBA:
First off, the solution code:
Public Sub ChangeSource()
Dim filename As Variant
Dim fieldcount As Integer
Dim x As Integer
filename = ThisDocument.Path & "\" & Left(Application.ActiveDocument.Name, Len(Application.ActiveDocument.Name) - 4) & "xlsm"
fieldcount = ActiveDocument.Fields.Count
For x = 1 To fieldcount
On Error Resume Next
If ActiveDocument.Fields(x).Type = 56 Then
ActiveDocument.Fields(x).Delete
End If
Next x
ActiveDocument.Bookmarks("R1").Range.InlineShapes.AddOLEObject filename:=filename & "!Range1", LinkToFile:=True
End Sub
I first deleted all type 56 fields (linked object, or more technically, "wdfieldlinked"). Then, I added OLEObjects at pre-set bookmark locations.
Interestingly, just as Bibadia noted, the key was to input the LinkToFile:=True code. It seems Word will not accept the object if it is embedded: if I remove that line, I get the error Word Cannot obtain the data for the C:\...\document!NamedRange link.
Finally, I found one other odd behavior: When trying to simply replace the link, using this code,
ActiveDocument.Fields(1).LinkFormat.SourceFullName = filepath+name & _
"!CED" 'that is the named range
it would work once, when I changed both the word document's and the excel workbook's filenames (see original message for context). So, when the new filepath+name DID NOT match the existing filepath+name, Word VBA accepted the change. However, once initially updated, if I tried to run the macro again, I would get:
run-time error '6083': Objects in this document contain links to files that cannot be found. The linked information will not be updated.
I would get this error even if I changed the named range to another named range in the same worksheet (and obviously same workbook). So it appears that Word VBA does not like "updating" filepath+name when the filepath+name does not change.
Just so anyone who didn't know (like me) now knows. Sorry for the long update, I just wanted to be thorough.
I am not completely sure of this, but it is a little too long for a comment.
As far as I know, you can only set LinkFormat.FullSourceName to the name of a file, not a fullname + subset name, which is what you are trying to do when appending the "!CED". Although you can read the subset name (CED) from OleFormat.Label, you can't modify it as it's a read-only property.
So if you actually need to modify the subset name (CED), AFAICS the only way to do it is to delete and reinsert the LINK field. If you reinsert using Fields.Add, you just specify the text of the field, so you can get the file name and Subset name right. What is slightly confusing is that if you insert a LINK using InlineShapes.AddOleObject, you can specify fullname+subset name in exactly the way that you are trying to do in your code.
However, I do not think you are trying to modify the Subset name. So let's assume that you already have a LINK field along the lines of
{ LINK Excel.SheetMacroEnabled.12 "the full pathname of a .xlsm file" CED \a f 0 \p }
Word will only be able to update that link if the path+filename is valid (i.e. there's a .xlsm at that location, the workbook has a Range Name called CED, and the Range Name is in the first Sheet. Otherwise, you have to specify a Sheet name as well, e.g.
{ LINK Excel.SheetMacroEnabled.12 "the full pathname of a .xlsm file" Sheet2!CED \a f 0 \p }
It's just a guess, but if your code is trying to connect to a Workbook where the range defined by CED is not in the first sheet, you would see the error you describe.
Further, the scope of the CED Range Name has to be either "workbook" or the name of the first sheet. Otherwise, if the scope is the first sheet but the range is actually in another sheet, or vice versa, I do not think Word can make the connection whatever subset name you provide (my guess is that Word never really caught up with Excel after Excel introduced multi-sheet workbooks).
If CED can reference sheets other than the first one, I think you will probably have to use the Excel object model to discover which sheet its Range is in, construct the appropriate Subset name, and delete/re-insert the LINK field.

Check if unique identifier is present in "new data", if not, add entries to "historical data" sheet?

I've got data with a unique ID number in one column. This is pasted into a "Raw Data" sheet by the user.
Then, I've got macros that manipulate the "Raw Data", including archiving some of it on a "archival" worksheet which includes the ID numbers.
I want to run a check to see if there are any "new" ID numbers in the "Raw Data", and if so add a new row with some of the data including the ID number to the "archival" sheet.
I've googled and checked here. It looks like I want to use a Collection? Never encountered this so far, not sure where to start.
Sorry that this isn't the most well structured question, and that it doesn't include any code. Not sure how to get started.
It should be noted that this reconciliation only needs to go one way -- I don't need to delete "old" unique IDs from the "archival" sheet.
Thanks!
If you specifically want to use collections, over Range.Find, you can do so like this.
Although handling errors this way in VBA is best to avoid most times, for iterating over collections, it can result in faster and often less verbose code.
I have used named ranges for a Rawdata and Archive worksheet tabs, so you will need to adapt this to your own situation. What this does is use collections to store unique items already in Archive, and compare these against items in Rawdata - when a new (unique) item is found in Rawdata that is not already in Archive, it is added to the sheet (and collection).
Sub IDcollection()
Dim IDcoll As New Collection
Dim cells, Rng, Rng_a As Range
Set Rng = Worksheets("Rawdata").Range("IDRANGE")
Set Rng_a = Worksheets("Archive").Range("IDRANGE_A")
'IDRANGE looks like string IDs 'abc', 'ab1',etc
'get unique IDs already in ARCH sheet via named range
On Error Resume Next 'includes only unique items
For Each cells In Rng_a.cells
IDcoll.Add cells.Text, cells.Text
'use the IDs as the KEY in the VBA Collection
Next cells
'check for unique items not in Archive, but in Rawdata (i.e. new items)
On Error Resume Next
For Each cells In Rng
IDcoll.Item cells.Text
If Err.Number <> 0 Then
IDcoll.Add cells.Text, cells.Text
LastRow = Rng_a.Rows(Rows.Count).End(xlUp).Row + 1
Rng_a(LastRow).Value = cells.Text
Err.Clear
End If
Next
End Sub
Just stick this in a module in your VBA for your worksheet, set up some named ranges as above, populated with dummy data.

Adding Data Validation to an excel spreadsheet using vb.net

I am trying to add Data Validation to a spreadsheet using VB.NET with data that is on Sheet2. I am able to open the workbook with both sheets and the data I need on Sheet2, but the problem I am having is populating the Data Validation Dropdown on Sheet1. I am trying to create the Data Validation in Column A and the first 700 rows of Sheet1, and the data on Sheet2 will be in Column A with the first 800 rows.
I have tried the following and it creates the Data Validation, and it only has one value inside - "Sheet2" -since I am not actually putting a formula in here.
With xlsSheet 'worksheet
.Range(String.Concat("Sheet1!$A$2:$A$700")).Validation.Add(XlDVType.xlValidateList, & _
Formula1:="Sheet2")
End With
When I try the following, it creates the excel sheet with the columns and the data on sheet2, but it doesn't create the data validation.
With xlsSheet 'worksheet
.Range(String.Concat("Sheet1!$A$2:$A$700")).Validation.Add(XlDVType.xlValidateList, & _
Formula1:="=Sheet2!$A$2:$A$800")
End With
I was looking for examples online and found http://www.get-digital-help.com/2011/10/05/add-values-to-a-data-validation-list-vba/ but this too did not work for me.
How can I get the data from A2:A800 in sheet2 into data validation on sheet1 from A2:A700?
Any help would be appreciated.
If anyone would like to know a solution to this, here is what I did:
I first created a named set that I will use to reference in the datavalidation dropdown.
workbooks.Names.Add(Name:="Working", RefersToR1C1:="=Work2!R2C1:R700" & "C1")
'Named set needed for data validation.
With xlsSheet
.Range("Work1!$A$2:$A$700").Validation.Add(XlDVType.xlValidateList, Formula1:="=Working")
End With
In the code above you can see Work1 is the name of the sheet I want to insert the Data Validation Dropdown in. The named range is getting the data from Work2 - my second sheet.

VBA Reference of dropdown selection list

I have a code that copies all sheets from book 1 to book 2. It copies the sheets several times, so I change the names by adding 001_, 002_ and etc in front of the copied sheets names. All of the sheets have dropdown menues. The problem is that they are referenced (Formulas--> Name Manager) to the previous workbook and they are not working.
There are 5 sheets to be copied and 4 of them are taking the values for the dropdown menues from the sheet TypeLists. This sheet is copied only once, so the code doesn't copy it, if it is already present in Book 2.
Using Record Macro, in changing one of the references, I got this:
ActiveWorkbook.Worksheets("003_TDT").Names("List_DataType").RefersToR1C1 = _
"=TypeLists!R3C14:R5C14"
What I did is, I went to Formulas --> Name Manager and changed one of the references from:
='C:\Users\z183464\Desktop[seq_tdt_template.xlsx]TypeLists'!$N$3:$N$5
to:
=TypeLists!$N$3:$N$5
My question is: How can I write a Macro that changes all the references from Book1/TypeLists to Book2/TypeLists ?
Cheers!
You can use this code. It iterates through all the defined names and changes its reference to the same value with the undesired part extracted (replaced by "").
This is not a macro, but VBA. You need to insert it in the VBA editor (Alt+F11).
Sub replaceNameValues()
Dim oName As Excel.Name
For Each oName In ThisWorkbook.Names
oName.RefersTo = Replace(oName.RefersTo, "C:\Users\z183464\Desktop[seq_tdt_template.xlsx]", "")
Next
End Sub
Regards,

How to reassign the address of a named range in Open Office VBA?

In Excel VBA, I redefine the address of a named range with:
Ranges("MyRange").Cells(1).CurrentRegion.Name = "MyRange"
in the Worksheet_Deactivate event.
This way, after updating the spreadsheet, MyDataRange always references to the entire data.
Ok with Excel, but when I tried to migrate this to Open Office VBA (OpenOffice.org 3.1.1 /
OOO310m19 Build:9420) the ".Name" property of the Range isn't defined.
Then, I tried with:
ThisWorkbook.Names("MyDataRange").RefersTo = Range("MyDataRange").Cells(1).CurrentRegion
with no luck ("the property is Read Only" error)
Again, I tried with:
ThisWorkbook.Names.Add ("MyDataRange", Range("MyDataRange").Cells(1).CurrentRegion)
This works, but after that all the cells with validations that references to the named range loses the original reference and shows "#NAME#" in the popup.
Is there any way to reassign the address of a named range without losing the references to it?
TIA,
Pablo
I wonder if this technique would work?