EPPlus changes cell interior color - excel

I found something really strange while using EPPlus and I couldn't find the solution anywhere online (I also didn't find anyone with the same problem to be fair). Luckily, after some time, I found out how to fix it and because I couldn't find anything about it, I want to save someone else his/her time and document it here. Note, the past week was my first experience with EPPlus so I'm sorry if this question is very trivial and everyone knew this already.
The Issue
I'm trying to put data into an existing Excel template using EPPlus. One of the sheets of the Excel file looks like this:
Nothing strange about it you would say. However, when I open this Excel file in my code and use EPPlus to add values and save it, it comes out like this:
Of course the numbers in the table are intentional, but how does it suddenly have a purple background!? Also why doesn't it happen to particular cells. There is nothing in the code that does anything with the styling. I only add values to the table.
Another strange thing is that this happened on multiple pc's, but not on all.

The underlying problem
After looking around, I found out that the standard styling suddenly showed the same purple color.
Before
After
I still don't understand why it changes the color, but it does.
Then somewhere on github, someone mentioned that you need to change the code of EPPlus to get rid of this. It mentioned that because the Excel spreadsheet was created in another language than English, it couldn't find the "Normal" style, since it's named "Standaard" in Dutch in this case, and you have to change "Normal" into "Standaard" in the code.
But I just used Nuget to get a package and didn't want to create my own package, and more important: What if I have a spreadsheet next time that is created in the English version?
The Solution
For such a long story, the solution is just to easy: In you workbook, in VBA in the immediate window, type the following code
Thisworkbook.Styles.Add("Normal")
The workbook now has a custom format called "Normal". Since it's likely that your normal format would be just blank, this will show you the proper styling.

Related

Why would my Excel 2010 UDF quit working with a #NAME? error?

I have Excel 2010. A couple of months ago I wrote some UDF's and got them debugged and working. Today I changed a value in the spreadsheet, and suddenly all the UDF's displayed #NAME?
Ultimately I decided to start from scratch. I created a new workbook with just one UDF. Here it is, in it's entirety:
Function say7() As Integer
say7 = 7
End Function
I then went to Sheet1 and in box A1 entered "=say7()". I got "#NAME?".
If I type "sa", the dropdown shows "say7". I can press tab or enter and it fills in the function name. And then it says #NAME?.
I understand that Excel 2010 is no longer supported. Did Microsoft do something to break UDFs?
My XLSM was working a few weeks ago. Indeed, the file where I made the change was a copy of the original spreadsheet. I went back to the original, changed a value in one cell that should have forced a recalculation, and it said #NAME?. I didn't change the code, I didn't change any settings, nothing. The only thing I changed was one data value and somehow that makes the function name not be recognized anymore.
Update: Several posters have pointed out that "say7" is not a legal name for a function in Excel and that using this name would produce a #REF? error. True. But my problem was that I was getting a #NAME? error, not a #REF? error. In my attempt to create a simplified scenario -- to make sure that the problem wasn't some error within the function -- I created a new, deliberately very simple function that got the same #NAME? error. Yes, I gave this function an illegal name, which muddles my question. I apologize for the confusion. Explaining why say7 is an invalid name is interesting and useful information, but sadly does not solve my original problem.
When you are writing the function then there are conventional rules of naming it. One of them says
You should not use any names that are identical to the functions, statements, and methods in Visual Basic because you may shadow the same keywords in the language.
The name Say7 while it looks innocuous enough, clashes with a column numbered 12895 which is named as SAY. So in principle, below works for me but I do not have Excel 2010 to see and confirm.
Public Function SaySeven() As Integer
SaySeven = 7
End Function
Edit:
After reading OP's own answer post and other details over, I can think of one more situation where we may encounter #NAME error. This can be reproduced fairly easily.
Step 1: Create a UDF and save the workbook. When you do so, it should not be saved at Excel's trusted locations and the file shall not be identified as trusted. If it is trusted then Excel doesn't prompt for enabling active content. At the point of creation, Excel will produce results without any error. Now close the workbook.
Step 2: Reopen this workbook. You will be prompted with below message.
Ignore this message i.e. do not press "Enable Content" button. OP may have missed this message somewhere.
Step 3: If the UDF was implemented in the previous session and it has returned a valid result then it will not return any error upon recalculation. However, if you enter this UDF in a new cell then Excel will return #NAME error. In below snapshot, both green cell and yellow cell contain exactly the same formula i.e.
=dist3()
But the outcome differs.
So in conclusion: UDF name dist3 is valid and it should work as cited by OP in his later postings. However, in above specific case, one may encounter error of this kind. It will, however, apply to all UDFs and not just dist3.
I have tested this in Excel's Office 365 version but I don't think there will be any major differences in this behavior across versions.
I eventually partly figured it out. At least enough to get it working.
Excel normally blocks you from running UDF's unless you explicitly give it permission. When you try to use the first UDF it pops up a question at the top of the window asking for permission, and if you don't give it, then UDFs aren't recognized and you get #NAME? errors for them.
Apparently -- and this is the part I'm not sure about -- the message telling me that support for Excel 2010 had expired was displayed on top of this message so I couldn't see it. Or something like that. I copied the file to another computer that had a newer version of Excel and I got the message. I clicked OK and the UDFs worked. I then saved it and copied it back to the original computer and it worked fine.
As I had previously clicked Ok on this and this answer is apparently saved with the file, I'm not sure why it asked again. Maybe some changes to the file invalidate the previous answer? I'm not sure.
I created a new file and saw the message about "UDFs ok?" on that one. So maybe my theory that the message was covered by the expiration notice is not correct. I guess it's possible that the message was there all along and I was just blind and didn't see it. That seems unlikely as I was searching for some explanation, but, etc.
In my first version of this post I said that the function name "say7" worked. This was incorrect. When I got the spreadsheet to work I had gone back to my original spreadsheet, where the function that was referenced in the spreadsheet was called "dist3", which works. (There were other functions but they wee all called from within dist3.)
In any case, before I created "say7" I was trying to get it to work with a function called "dist3" and that was also failing with the #NAME?. And as I said in the question, it worked a month ago and I didn't change any function names, so I knew that an illegal function name was not the issue. My attempt to create a simple function to test the problem was a wrong turn because I used an illegal function name that created different problems.
Seeing the logic of #shrivallbha redij's reply I changed the function's name to SAYG (with the idea that G defines the 7th column) and still got a #NAME error. However this can be avoided by passing an argument in the function call, even if the argument isn't used by the function or if it's blank.
=SAYG - returns an error
=SAYG(A1) - returns 7
=SAYG() - returns 7

Excel Connections - Edit Links Not Working?

I do not know if this has been asked elsewhere but I looked and googled around for 30 mins and couldn't seem to find much. I have an old file with some drop down boxes for certain cells in an excel spreadsheet. I never really know how it works to be honest, and there are no macros within this sheet. But somehow, there are some links between some cells within the same file.
I recently changed file names and made copies to this master file, but some of the newer copies drop down boxes no longer work, and seems to be wanting to get to the original file. I tried editing this/these link(s) under the ribbon interface using 2016 - Data > Edit Links (under connections), and it brings up a pretty dialogue box, which has this change source button. But after I clicked it and selected the correct file (it's a circular reference, so it is the current file), it simply did nothing and did not update the file name or anything else shown within the dialogue box. And of course, my links are not fixed and the drop down boxes are not working.
I have uploaded the file here with all data stripped out, but the links are still intact there for your review. Any suggestions why I am seeing this? If I want to fix the links, what can I do to achieve that without fixing each of the cells individually? Thanks!
The file is here:
https://drive.google.com/open?id=1Sn27jnQmYvqWHho6Oa0yz7PXVGU4zwPJ
These reason you're seeing these links is that there are cells with Data Validation that point to an external file (cell B71, for example).
Remove all Data Validation that is referencing external files, save the file, and re-open. The links should be gone.

Indirect not working

I want to reference an external workbook that is open. If I type =path\[file] directly into the cell (even though I am still asked to 'Update Values',) I am able to get the data I require. However, I am using INDIRECT because the name of the file changes everyday (date). So, I am using CONCATENATE to create a filename daily and then using INDIRECT to the cell where the concatenated formulas is. If you paste values to see what is being generated formula, it is exactly the same formula as above, =path\[file]. However, I continue to only get #REF! error.
I read through this, https://support.office.com/en-us/article/control-when-external-references-links-are-updated-87861952-ca9d-4d2a-a986-4d672c908d0d, and I still can not figure it out. I had it working on my computer at home last night but today and work it is giving me trouble.
To clarify, the formula that I am trying to get to work looks like this =INDIRECT("'"&CONCATENATE($F$129,$F$130,F95)) which worked yesterday at home. Can someone lead me in the right direction?
EDIT:
I have tried two options, =INDIRECT(F121) and =INDIRECT("'"&F121). The contents of F121 have been =CONCATENATE("'",F191,F192,F157) and =CONCATENATE(F191,F192,F157), respectively. I have also tried =CONCATENATE("'",F191,F192,F157) in the destination cell. F191 = [Liquidity Risk Dashboard v2.03 -, F192 = =TEXT(E3,"yyyymmdd")&"]", and F157 = Calculations'!D122. If I write ='[BMI Liquidity Risk Dashboard v2.03 - 20170105]Calculations'!D122 am asked to update values but, 100% have both workbooks upon in same instance
If I understand what you really have in your cells, the problem is that you are not properly naming the source workbook.
Try changing
F192: =TEXT(E3,"yyyymmdd")&".xlsb]"
Replace .xlsb with whatever the file extension really is for your file. (eg: .xlsx, .xlsm, .xls)

Ghost/Phantom Workbooks/sheets appear in VBA Project list alongside legitimate sheets

(edited a day later - see end)
Not sure what's happening here but it's occurring in a few workbooks I'm working with. Over time additional "Phantom" worksheets (or workbooks) appear as shown in the picture. Sheet13, Sheet14, Sheet15 etc are Phantoms and I have no idea how to get rid of them.
What I've tried so far:
1) Searching Google/Stackoverflow no joy but could be a terminology problem
2) Created SUBS to loop through ALL worksheets and workbooks but the only things it finds are the single workbook and the legitimate worksheets.
3) Creating code within one of the phantoms, which is possible. I created a simple SUB on workbook open within Sheet13 to return the name of the sheet or workbook, but no matter what code is there I get
Run Time Error '429':
ActiveX component can't create object
4) Changing properties on the phantoms. The properties for each phantom exactly mirror each other and mirror ThisWorkbook. If I make a change to one, say KeepChangeHistory to False, this is mirrored in ALL phantoms and ThisWorkbook. If I make the change back to True in another phantom, again ALL phantoms and ThisWorkbook are updated.
5) Checked links (greyed out). Tried F5 -> special -> objects "No Objects found".
I've basically tried everything I can think of and searched the internet over the past week before posting this here, as a last resort.
Does anyone have ANY idea what could be causing this (I'm leaning towards some bad object handling somewhere), but also HOW to remove them completely so the only things listed are the actual, real, worksheets?
Thanks for reading.
edit
It affects more than just this workbook (it's happening in 3 different workbooks), it is also getting worse over time. I have saved archived versions of all workbooks. If I go back a month, there are 2 "phantoms", if I go back 2 weeks there are 8 "phantoms" and if I look at one from just last week there are 13 "phantoms". This is one of the reasons I'm assume some poor object handling, likely related to ThisWorkbook is going on. Anyway, I'm going to take Rory's advice and create a new Workbook with NO phantoms and try and figure it out from there. Just want to thank Rory and Mark for taking the time to respond with ideas, it is appreciated.
Well, I guess the simplest answers are the best. What I didn't mention in the question is that each workbook I've worked on that has this problem is uploaded to a 3rd party application, which is, in turn, sent to a second 3rd party application (bespoke apps written by others).
The second application creates 1 or 2 of these "phantoms". I choose to work slightly differently to other developers and I actually use the output from the second application to make changes/fixes, then format that workbook and reupload to application one.
Over time, more and more "phantoms" appear, 1 or 2 per run through this process.
In the end, absolutely nothing to do with Excel, poor code (at least mine!), badly handled objects or anything else.
Thanks again to Rory and Mark for attempting to help. Maybe next time I'll actually start with the mindset of "I'm sure this ISN'T my fault!" and go from there!
I had exactly the same problem. I was reaching the Sheet41111. I used the option Book Inspection in the Information Menu, then I selected remove hidden objects and it luckily worked.
See the image attached, my excel is in Spanish so maybe the option is not really called "Book inspection" in the English version.
Hope it is useful
Pd- Unhide the legitimate sheets because it will delete all hidden sheets. I suggest trying it in a copy of the book. enter image description here enter image description here

how to copy multiple selections from excel to word (manually and/or VBA)

Using Excel/Word 2007 I'm trying to copy multiple selections from Excel to Word. When I paste it into Word it's as if it ignored the break in the ranges and just pastes a range that includes everything I was trying to leave out.
For instance if I intend to copy/paste "A2:D4, A6:D7, A10:D11" what is actually pasted in word is "A2:D11", so it's ignoring the breaks in the selections. I can't seem to find any documentation on this issue anywhere. If someone knows anything I'd really appreciated it. I know I'm selecting the ranges properly because if I copy and paste the multiple ranges within Excel it works like it should.
The actual issue is that I have these actions automated in a macro and I'd really prefer not to loop through the individual ranges because they're stored in a dictionary and it would be just a real hassle. Despite that, it seems weird that I can't even accomplish the task manually.
I've done quite a bit of digging and can't seem to come up with anything. I thought about using the Office clipboard but you can't paste tables as an RTF. It doesn't seem like there are any options when pasting with the office clipboard.
AFAIK it doesn't seem possible and I've tried to exhaust all my normal avenues of knowledge before posting here. If anyone has ever just experienced this it would be comforting to know I'm not alone.
I don't know how to prevent that from happening, but you could potentially work around it by pasting your non-continuous selection into a continuous region in a new, temporary sheet/workbook, then copying the continuous region from the temp sheet/workbook into word.
a bit of a hassle, yes, but it should get the job done.

Resources