Date in Spreadsheet shows as number when cell format is short date - excel

One spreadsheet in the workbook has a column of dates that appear as left justified numbers, even though the format on the column is Short Date. Cannot set the column to number format, change alignment or force any other format. When a cell is selected it shows as a date in the cell and in the formula bar, but when focus is changed, it reverts to number. How to fix this?

The spread sheet was copied from another workbook, and was apparently protected in that workbook. Thus not able to format or change anything. Ended up selecting all cells in the worksheet and pasting into a new worksheet. Able to apply changes then.

Related

Excel VBA - Text to Date after running macro in a single Row

I have several Macros running at once in a file. The output pastes unique dates from B1:Z1 on the next sheet. The dates right now are pasting as ex. 43619 instead of 6/3/2019. I have been troubleshooting this but everything I have found is a whole column instead of rows. Thanks!
To Excel a Date is a number(the number of days since 1/1/1900). The number is formatted to look like a date.
If you are just pasting the values the format is not following and as such the value is reverted to the number.
To apply the formatting just use:
Worksheet("Sheet1").Range("B1:Z1").NumberFormat = "mm/dd/yyyy"
Changing the sheet name to your desired sheet and the number format to the format desired.

Accessing ActiveX textbox value from cell formula

I cannot post my code because it is company property, but the issue is that I would like to enter a cell formula so that it automatically updates when particular other cells change.
I have two columns, one for prices in USD and another for another currency. The other currency column is a simple "=SUM..." but the currency conversion to USD is always changing, and the prices are large enough that these changes matter. As such, I have the user enter the conversion value in the activex textbox, and I want to set the formula of the USD cells to be the price in USD, so obviously I need to get this conversion value, but I cannot just type in the textbox name, that does not work.
I am using VBA, but I would like to enter this directly into the cell as "=FORMULA", where formula contains the activeX textbox value, so that it automatically updates.
As far as I have found in VBA, if I want to assign a formula to a cell, I have to do so within a sub or function, so I have to call the sub every time I want to update the cells, but I want this to be as automatic as Excel is for other cell formulas.
I have been playing with Worksheet_Change and considered worksheet_open but have had other issues. For example, there are many different cells that could change that I have to monitor for change, and I cannot simply monitor the price column in the other currency for change because when cells update from a formula, Worksheet change does not show those cells as having changed.
Maybe this is as simple as setting the cell formulas on worksheet_open, but I also have not had luck with getting cell.formula to even work. I could discard the textbox for a regular cell which would totally solve this problem, but I also have a textbox_lostfocus sub that I need to run, and I don't know how to do that for a cell.
Sorry for the long post, in summary, how can I assess a textbox value for use in a cell formula ideally without using vba?
This is what linked cells are for. You link your ActiveX textbox to a cell. The cell value then gets updated once you update the value of your textbox. You can reference this cell in your normal excel formulas. See here for a tutorial: https://analysistabs.com/vba-code/activex-controls/textbox/

cell formatting not applied after formula

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.

Excel workbook Formatting cells

My colleague has sent me an excel file with lots of data, pivot tables, and charts. But there is something weird with the file's default cell format. When I enter a date into a cell "1/1/2014" it turns into "EUR 41,640.00" Where "EUR" is the Euro symbol.
I right clicked on a cell and went to number format cells and under number, it says "Accounting" and under symbol it says "Euro". So that explains it. Now, my question is, can I change the format for the entire workbook and all its sheets and charts and tables, in one shot, to something that a normal/default excel sheet has where it can recognize dates for dates, currency for currency, and numbers for numbers and will that mess us all the existing pivot tables and data? I would think that it won't mess up the numbers but simply the formatting but I want to be absolutely sure before I change the default format for the entire workbook.
I went to File-> Options->General/Formulas etc but I can't find cell formats there.
Excel just thinks this is a number, namely 41640.
For each column of Euros-that-should-be-dates, right-click on the column header (like the letter A above the first column) and choose Format Cells...
Then pick your preferred date format.
This happens sometimes when the file has different currency or date default settings.

Excel - Conditional Formatting - Updates Based on Current Date

How would I go about applying conditional formatting to a range of cells (Excel 2007) based on the current date?
I have a six-week plan (each column represents a day in the week (Monday to Friday, excluduing weekends)) and I would like to shade each column in green as the day passes without any user interaction, so that when I look at it, it will accurately have 'marked off' each day.
This is what I hope to achieve: I view the plan on Monday 1st January. I then don't access it until Thursday 4th January; once the plan is opened, I will see that the columns M-W have been shaded in green, whereas the rest of the plan for the remainder of the six weeks remains unshaded and this should continue for the remainder of the duration of the six-week plan.
Is this possible?
I would imagine that VBA would be used to check the system time and then apply the formatting according to what day it is.
Many thanks.
UPDATE
This is how my planner looks:
Sure, it's possible. The following should put you on the right track.
First, you can record the last time the workbook was accessed by storing the current date/time in a cell at the time when the workbook closes. This can be done using the Workbook_BeforeClose event, which has to be put in the workbook module. Open the VBA editor (Alt-F11), then open the ThisWorkbook module and paste this code in it.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheet1.Range("B2").Value = Now()
End Sub
Every time the workbook is closed, the date/time at closing time will be noted in cell B2 (change this as you see fit).
In cell B3, use the =NOW() Excel formula to show the current date time. Cells B2 and B3 now define the interval within which your columns should be highlighted.
Then how to format the columns... Say your columns have date headers as in the image above. Select your "calendar" range (C5:I12 in the example above) and click Conditional Formatting > New Rule... > Use a formula. The formula should be =AND(C$5>$B$2,C$5<$B$3) meaning that only the columns whose date in the header is after the workbook was closed last time, but before now (adjust as you see fit), will be formatted. Click Format... to select e.g. a green fill, or whatever. Then OK.
Of course you could refine this in many ways (e.g. rounding to the previous or next day at midnight) but at least you should be on the right track now.
Use this conditional formatting formula:
=IF(DAYS360(DATE(B$2;B$3;B$4);TODAY())>1;1;0)
what is does is
if(the difference between this colums date and today is more then 1 day, be true, otherwise false)
use it in a sheet like

Resources