Format codes for first page header/footer are different - excel

I want to set a different header and footer with VBA for the first page of an Excel 2013 sheet that should be printed. The header and footer should contain information such as the page number, the name of the active sheet and the file name. The official Microsoft documentation about format codes (https://learn.microsoft.com/en-us/office/vba/excel/concepts/workbooks-and-worksheets/formatting-and-vba-codes-for-headers-and-footers) provides various options for this, and all of those format codes work correctly on every page except for the first one - they appear to be assigned differently (e.g. "&N" should display the number of pages, but displays the file name instead) . How can I use the correct format codes for the first page header/footer?
I tried to assign those format codes to constants, but that doesn't work because the codes are only parsed when they are included in a header or footer string. I also recorded a macro while changing the headers manually (which works correctly), but that macro doesn't produce the correct output either.
Sub formatCodesTest()
With ActiveSheet.PageSetup
.DifferentFirstPageHeaderFooter = True
.FirstPage.LeftHeader.Text = "&N"
End With
ActiveSheet.PrintPreview
End Sub
I expected the output to be the number of pages (as it is described in the Microsoft docs), but the actual output is the file name.

I have now managed to work around this problem. Instead of relying on the Microsoft documentation, enter the header/footer manually in the GUI and then use the Immediate Window (Ctrl+G) for debugging and query the contents of the relevant header (e.g. ? ActiveSheet.PageSetup.FirstPage.RightHeader.Text). This will then give you the correct format codes.
If anybody else has an idea why this happens (and why others cannot reproduce it), I'll gladly accept this as an answer.

I have the same problem in Excel 2019: I wanted to show "Page 1 of 5" in the right footer.
As it concerns only the special settings for the first page, I solved it this way:
ActiveSheet.PageSetup.FirstPage.RightFooter.Text = "Page 1 of " + Trim(Str(ActiveSheet.PageSetup.Pages.Count))
Notice that this only works if filling cells, changing page layout, adding manual page breaks done by your VBA code is completed because before Excel can't now the correct amount of pages.

Related

If I have already set print areas desired, how do I print only the pages which include non-"" formula results?

As an inventory control specialist, I set up a macro and some formulas to paste and mine data copied from a IBM Power 9 inventory bin location database.
The end goal was to create a hard copy array of all bin locations with their bar codes and room for audit notes. The results worksheet is already set up with the desired formulas and formatting and divided into the desired print areas so that running the macro simply populates it with the names and barcodes of up to 2000 bin locations.
Everything works like a charm, but currently I need to use print preview and manually set the range of active sheets to print, as not all of the 48 pages spanning the active cells containing formatting and formulas are used for any given group of bins.
I have searched multiple sites for hours and have not found any code that is remotely helpful, and my self-taught VBA skill is limited to simple macros and slight modifications of other peoples' code. Is there a way to program a macro to print only pages (as defined by predetermined print areas) with values (non-""-result formulas) while ignoring pages filled only with ""-result formulas and formatting? I am running Excel for Office 365.
Thanks
My response was too long for the comment field and, more or less, answers your question so I moved it here.
The way I imagine your data, I'd loop through every page in the workbook. While on a page, I'd loop through every formula until I found a result that is not blank. On finding a non-blank result, I'd add the page to a list of pages to print and move to the next page. After checking the last page, I'd print each page on the list.
You may want to define the steps required to complete your project (similar to what I just did) because each step requires several motions and almost every one of them will be a new challenge for someone with your experience. Once defined, find the most fundamental movements that are critical to your program and start there. Keep it simple, you will not always know what the challenge will be or where it will come from. What looked like a simple step, like looping between pages, requires a basic understanding of the workbook object and that is a far more complex subject than a loop.
Keep in mind that we are here to solve problems with your current code. If you don't have a problem with existing code then there is not much that can be done beyond pointing you toward a path.
After reviewing the links provided by #ProfoundlyOblivious I quickly determined I lacked the skill to program a solution using VBA as suggested. However, trying to parse all that information got my brain on a roll and I did develop a very simple and sneaky workaround using formulas tied in with a simple one line macro.
Assuming all pages predefined by print areas are the same width, and assuming there is at least one cell that will always consistently be populated in on a used page, these instructions should work for you should you have a similar issue.
Enter the following simple binary IF formula in a blank row of the first page:
=IF(A1<>"",1,0)
where A1 is a cell in the first page containing a formula that would be guaranteed to consistently return a value if that page is being used. (in my case, the imported information is imported in columns beginning with the upper left corner of each page).
Select the row of the page (not the entire row of the worksheet, just the width of the page as defined by the print area) containing the formula, and drag fill across to all possible pages, then in the same row of the last page simply add a SUM(,,,,) formula referencing (adding) all of the binary IF(X,1,0) formulas. The resulting sum will be the number of populated pages.
Clunky, but functional.
Finally, the simple print macro:
Sub Print_Audit_Sheets()
ActiveWindow.SelectedSheets.PrintOut From:=1, To:=Range("JW42").Value, Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub
Where ("JW42") is the reference to the range with the SUM() of all the binary IF() formulas. It isn't fancy, but it's simple, and simple works. Be sure to change the text color on the IF() binary and Sum() cells so that you don't have random 1s and 0s obtruding in your printouts.
You will need to modify this code if your first page is not consistently populated or your value-containing pages are not contiguous.

MATLAB's xlsread gives different data than shown by Excel - hidden sheets

I want to use xlsread in MATLAB R2017b to read from an externally supplied data file. Usually, this works fine for me. However, in this case I get data I can't find in the .xls file and I don't know what happened.
Here is screenshot of the .xls:
and here of the corresponding raw from xlsread:
Note that there is data in MATLAB (e.g. 'Report tem...') that cannot be found in Excel, that the columns are in a different order and that their headers also differ.
The data file is from Svenska Kraftnät, the Swedish Transmission System Operator and contains the generation and consumption of electrical energy for a certain year. You can find it here.
I use the following line to import the data in question (I am only interested in the numerical data and the timestamps, but used the raw to try to understand what is going on here):
[num,~,raw] = xlsread('n_fot2013-01-12.xls');
I am sorry if this a bad format for the question or if this is a dupe, but I didn't have a clue how to make this question more general. Please feel free to suggest improvements!
Your workbook has a hidden sheet in it, and it is that sheet that is being read.
To read the visible sheet, specify the sheet name:
[num,~,raw] = xlsread('n_fot2013-01-12.xls','Förb + prod i Sverige');
To view the the hidden sheet, on the Home tab, in the Cells group, click Format > Visibility > Hide & Unhide > Unhide Sheet. Then select the hidden sheet.
There isn't a way to tell xlsread to only read visible sheets, and by default it reads the first sheet (hidden or not).

Downloading File with importrange function failing - think it's a bug

I've been saving Google Sheets to Excel without any problems for a while. These sheets have always successfully saved and opened in Excel with the importrange function. However, recently it hasn't been successfully saving correctly.
It used to just have the static value (e.g, 40). There used to be an IFERROR in the first cell in the header row but now it exists in every single cell.
E.g, each cell would have something like this:
=IFERROR(__xludf.DUMMYFUNCTION(importrange(blahblah)),"40").
DUMMYFUNCTION throws an error and "40" is returned as a result. but "40" is a string, not an integer which messes up all my formulas.
I also know this isn't an Excel issue because OpenOffice is doing the same thing with the file.
I'm pretty sure this would be a bug because why would it be working for months and then suddenly stop working?
What should I do?
I'm thinking it's a bug too.
Workarounds
On Excel
Copy and paste as values only the ranges with IFERROR(__xludf.DUMMYFUNCTION(..., then use Excel's UI tools to convert numbers shown as text to numbers.
Selectively remove quotes on the IFERROR second argument of the cells causing problems
Remove =IFERROR(__xludf.DUMMYFUNCTION(),"value") except value (we could use Excel's built-in FIND & REPLACE for this)
On Google Sheets
Use Copy > Paste as values only on the range areas having formulas with non-compatible functions like IMPORTRANGE, QUERY, FILTER, etc.
If you only need the values, download it as CSV instead of XLSX
IMPORTANT
In order to help to prioritize this issue, send feedback to Google. To do this open a Google Sheets spreadsheet, click on Help > Report a problem, then fill the feedback form and submit it.
Related stuff
I posted 5 small articles about this in Spanish. You could find them listed on https://www.rubenrivera.mx/p/descargar-hcg-excel.html.
We accidentally created a workaround for this bug with a different sheet that was just set up like this.
This works when you IMPORTRANGE into another Google Sheet. We are doing it into a Google Sheet with a single worksheet - haven't tried it with multiple.
It's going to sound a little nuts but it works for us.
In the first cell of your import range put a hyperlink in the original document you are importing from. This is in the first cell of the import range. We linked it to a worksheet in the original document. It has worked and failed with an external link. With an external link it worked when I linked it to an internal link, then changed it. But when I deleted the cell and just straight linked it to an external URL it didn't work.
Then #timbo was right - put data validation in. This can be in part of the document that isn't being imported into the second sheet. I put it in the first line of the import range but outside what I was importing. It might have to be the first line. I just put a date in one cell, then in the next cell data > data validation > then choose that one date as the data range.
For aesthetics I have hidden the first row in one Google Sheet I am importing into. In another I made the first cell link the title of the sheet and put the data validation outside the import range. Both of these work.
Let me know if this works for you.
Until this bug is fixed, a workaround is to put a data validation (Data > Data Validation) on the imported data (Any kind of data validation will do).

Excel 2010: Concatenated URL broken when saved to PDF

I have a small application built in Excel 2010 with cell functions plus some vba subs and a vba function that is called in worksheet cells.
One column has a formula in each cell that builds a hyperlink using a base URL, variable part, fixed part, variable part.
Works great, until...
I save the print area to PDF using the inbuilt functionality in Excel 2010.
The generated PDF shows hyperlinks that look OK but the link actually only takes you to the first part of the concatenated URL (the base URL).
Eg this link:
https://www.support.unisys.com/common/epa/macro.aspx?path0=a-s&path1=ple&path2=web-select&SELECT=PLE-PLE-TO-IC+eq+MCP-054.1A.117&TITLE=PLE(s)+released+since+IC:+MCP-054.1A.116
Would be generated by the following formula:
=HYPERLINK(CONCATENATE("https://www.support.unisys.com/common/epa/macro.aspx?path0=a-s&path1=ple&path2=web-select&SELECT=PLE-PLE-TO-IC+eq+",D1,"&TITLE=PLE(s)+released+since+IC:+",C1))
The saved PDF displays the hyperlink label as:
https://www.support.unisys.com/common/epa/macro.aspx?path0=a-s&path1=ple&path2=web-select&SELECT=PLE-PLE-TO-IC+eq+MCP-054.1A.117&TITLE=PLE(s)+released+since+IC:+MCP-054.1A.116
But the actual link takes you to:
https://www.support.unisys.com/common/epa/macro.aspx?path0=a-s&path1=ple&path2=web-select&SELECT=PLE-PLE-TO-IC+eq+
Any ideas how to get it to create the correct hyperlink in the PDF? Or is this a Microsoft bug(or Adobe)?
Regards,
Graham
So, turns out that I needed to select Ignore Print Areas on the PDF options dialog, to avoid lines being inserted during the conversion, because of the really long URL.
I also had to amend commas in the URL to be URL encoded (%2C) to truncation on the comma in the PDF.
See link below where I got the Ignore Print Areas nugget:
http://answers.microsoft.com/en-us/office/forum/office_2010-excel/excel-2010-concatenated-url-broken-when-saved-to/940ff1f5-fbdb-4875-a4f7-536b615e6df2

How to fix unreadable content error with Open XML SDK 2.0 excel using SpreadsheetWorker?

[SOLVED (see answer below)] I am using SpreadsheetWorker API to make working with Open XML SDK 2.0 easier. I have an excel spreadsheet that I use as a template when I generate new excel spreadsheets through SpreadsheetWorker. I am getting a "found unreadable content" error when I open my generated files, and excel will correct the problems with the file if you allow it. This is not suitable for production.
While adding hyperlinks to excel cell, try to avoid dynamic URL to screenTip attribute. Excel cell takes only 256 charecter long for tooltip. The repaired excel file chops off the characters after this limit.
For Spreadsheet gear,
worksheet.Hyperlinks.Add(worksheet.Cells[startRow, startColumn], href, null, "Click here to see details","textToDisplay");
Solved my own question: In my excel spreadsheet that I used as a template, I had some blank cells with 'Hyperlink' styles applied to them. Setting the style of these cells to 'Normal' fixed the problem, and I no longer get the "unreadable content" error when I open generated spreadsheets for the first time.

Resources