I'm using NPOI.
I read in an Excel workbook with two sheets that acts as a "template" for the result workbook I'll be generating. Sheet 0 is empty aside from some header rows and sheet 1 has a number of rows of formulas. The formulas generally refer to the sheet 0 and pull data from it.
I can't write directly into the template workbook because I'd be having to move rows out of the way and it would get ugly fast, so in code I create a new result workbook with two sheets. Based on a preset configuration file, I populate the sheet 0 of this result workbook with data, row by row, cloning the style and formulas of certain rows from the template workbook. Sheet 0 ends up filled with data, and sheet 1 is basically a row of formulas copied and adjusted relatively, typically pulling data from sheet 0 with a small calculation here or there.
All of my data in sheet 0 is text and cannot be interpreted as numeric, dates, etc. So I set my data cell types as String.
In code I then loop through all cells in sheet 1 and EvaluateInCell.
I do this to obviously evaluate the formulas, but also to remove the formulas and leave the copied/calculated results. This is just a requirement of the work I'm doing. We deliver the end results and no formulas.
I save the resulting workbook.
In general things look good, except where I have cell formatting, the formatting seems to not be applied.
The formatting IS there when I get the cell formatting properties in Excel.
For example I might have text data that is a date-time in my data that shows up as:
7/7/2016 9:54:55 AM
this IS what the original data text is, but on my formula sheet I have a custom cell format yyyymmdd.
And yet, the cell still shows: 7/7/2016 9:54:55 AM
In Excel I then do something like manually edit the value, for example delete the last 'M' and retype 'M' and hit enter, that cell changes to the desired format and shows:
20160707
So again, the formatting IS there, it's just not applied.
I don't want to have to manually edit cells, or do anything once the workbook is opened in Excel by my customers. I want the resulting workbook to open up with the values formatted.
I've tried a few things that seemed like a shot in the dark, like:
this.ResultWorkbook.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll();
((XSSFWorkbook)this.ResultWorkbook).SetForceFormulaRecalculation(true);
But this didn't help.
Any thoughts?
can you try either of this apprioach while setting the formula for cell
Approach 1
ICellStyle dateCellStyle = workbook.CreateCellStyle();
dateCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyymmdd"); // Or prefix single quote for data when writing it to excel file like 'yyymmdd ex: '20160707
Approach 2
XSSFCellStyle dateCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
XSSFDataFormat dateDataFormat = (XSSFDataFormat)workbook.CreateDataFormat();
dateCellStyle.SetDataFormat(dateDataFormat.GetFormat("yyyymmdd"));
I ended up looking at the template Excel file's cell I'm writing into. It looks like if it's not DateUtil.IsCellDateFormatted and the cellStyle.DataFormat is not 0x31 (text based on BuiltInFormats), then it must be numeric.
Based on that I try to convert my text to DateTime or double as applicable, and call SetCellValue with those converted variables. Otherwise I write the text.
This seems to be working for cases I've encountered.
Related
I’m very new to VBA, not really sure where to start. I have two separate workbooks saved in the same folder. What I am looking for is when column A is populated in workbook 1;
I want a macro that searches for that number in workbook 2 column C. If a match is found then I want the corresponding value from column F in workbook 2 to be copied onto column I in workbook 1:
If a match is not found just leave the cell blank.
If more than one match is found, use the greatest number.
Sometimes workbook 1 has multiple pages as well.
Workbook 2 won't be opened by the user, they will just populate the data in workbook 1 and the macro runs automatically if data in column A changes.
Any help is appreciated.
I'm not sure you need VBA here, but I'm brand new to VBA. This is what I would use, and I'm sure someone can convert this to VBA. For reference, I'm calling your workbooks Book1 & Book2. In Book2 (the one you want the result in) use this formula:
=IF(MAX(IF([Book1]Sheet1!$C$2:$C$20000=A26,[Book1]Sheet1!$F$2:$F$20000))=0,"",(MAX(IF([Book1]Sheet1!$ C$2:$C$20000=A26,[Book1]Sheet1!$F$2:$F$20000))))
To make this an array formula, go back to the cell with your formula, hit F2 & then CTRL+SHIFT+ENTER, and it will put brackets around your formula to make it look like this:
{=IF(MAX(IF([Book1]Sheet1!$C$2:$C$20000=A26,[Book1]Sheet1!$F$2:$F$20000))=0,"",(MAX(IF([Book1]Sheet1!$ C$2:$C$20000=A26,[Book1]Sheet1!$F$2:$F$20000))))}
You can then drag that formula down or fill down that formula.
Note: $C$2:$C20000 & $F2:$F20000 is assuming that your part numbers & prices or whatever they are start on row 2, and there are less than 20000 rows, if it is more, just replace the 20000 to however high you need. The A26, appears to my old eyes to be the first cell in which you are typing in the part numbers which correspond to Sheet1 column C.
I have two excel sheets. The cells in the one sheet contains only simple numbers and strings. The other one has a lot of formatting and style. I want to move the data in sheet 1 to sheet 2 and have sheet 2 retain its format.
Essentially, if I write a number 1 in cell A1 in sheet 1, and color cell A1 in sheet 2 blue, I want to import the 1 from sheet 1 to sheet 2, and keep the colouring. I wish to do this for the whole sheet, matching cells and moving only the data.
Is this possible? I've tried using various things like saving the table format and the format painter but they do not produce the results I seek.
I'm not too sure which way you're trying to move data so i'll try and explain both:
If you want to copy the values from sheet 1 into sheet 2 exactly then try:
Paste Special - Values. You can do this easily with a keyboard shortcut of Ctrl+Alt+V and then pressing V and enter. More info Here
If you want to keep the data in sheet 1 but move formatting from sheet 2 across, then use:
Otherwise reading a bit more into Format Painter may be useful.
Microsoft has some instructions how to use this in detail Here. Otherwise there are some third party plugins such as Kutools that have more advanced features
Hope I've helped!
I'm trying to make a Excel VBA that gets 100 lines per time in a table and send them to a new excel file separately, but I don't know how. Is there a way?
There are 2000 lines in the table, so I want 20 separate files.
Excel has some special cell types, one of which returns the cell showing the maximum extent of data in the worksheet. Research xlCellTypeLastCell in the help file. The returned value does not mean there is data in that particular cell, but that there is no data in rows or columns past that row and column ref.
Eg You can select the last cell in a worksheet with the line below.
ActiveCell.SpecialCells(xlLastCell).Select
Then use the row number of that cell to set the bounds for your loop.
I need to display Excel sheet content to another sheet in same Excel file. Here I have two sheet MATHS and RESULT. I tried to set a cell value of RESULT by using =MATHS!D5 or ='MATHS'!D5. (I tried these two formulas.)
I got formula =MATHS!D5 or ='MATHS'!D5 instead of the result in the cell.
The calculation option is Automatic and cells are not Text, it shows General.
When open, Excel shows:
Double click on the cells the result appears:
You have to use
.setCellFormula("'MATHS'!D5");
to display the values of different sheet.
if it displays ='MATHS'!D5 you might have used
.setCellValue("='MATHS'!D5");
Hope it rings a bell.
I'm struggling to get my Excel spreadsheet (2010) to do what I want, but I'm not sure its possible!
What I have is diagrams with numbers (with the numbers in circles) on sheet two, on sheet one I have VLOOKUPs waiting to input the appropriate data. What I want, is for the user to click on the circle containing whichever number, then for that number to be put in the first column of the sheet, thus causing the VLOOKUP to fill in the rest of the data. Any ideas? I'm trying to play about with macros but I've had no luck so far.
Any help would be greatly appreciated
Jazz
EDIT: I've managed to get a macro to copy and paste the data to the correct column, what I now need is for the macro to put the pasted data into the next blank cell in the column
What if you try a command like the following:
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
After you have unlocked the worksheet and copied the data, have the VB code go to the active sheet, and paste your data in row that follows the last used row in the worksheet. If you can weave something like this in, you will be able to avoid indexing the rows with a number. You will also be able to deal with the case that there are empty rows sprinkled within column A in this case.