Convert text cells to DATE, but leave empty and DATE cells intact [closed] - spreadsheet

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
sorry for my ignorance I have no idea about spreadsheets.
What I have is a column that contains dates but they are there in different format
some are text like '19.12.2006 and others are cells that are formated as a date like 2009-10-10
I want them all to be date cells, so I found a formula =DATEVALUE
What I done is I put =IF(A2="";"";DATEVALUE(A2))
For the dates in text format it done well, for empty cells it's ok as well, but when the cell is a date cell I have Err: 502
Can anyone please help me?

As you've discovered, DATEVALUE expects a string that looks like a date, not an actual date. However, there is nothing stopping you from taking your dates and converting them to strings with TEXT. The right format mask can distinguish between dates and string dates for you.
        
The formula in N1 is,
=IF(ISBLANK(M1); ""; DATEVALUE(TEXT(M1; "mmm dd, yyyy;#")))
This formats anything that is an actual date into a string date and leaves any string dates alone. There seemed to be a dd/mm/yy vs mm/dd/yy issue between your sample data and my regoinal system so I used that complex date format but just about any date format will do. The # is stuck on the end to catch strings. The result is processed by DATEVALUE. I used ISBLANK for the second test but there is nothing wrong with IF(M1=""; ....

I'm not a user of OpenOffice Calc, but in Excel and Google Sheets there is a formula called =IFERROR() which can be used in this scenario - your formula in N7 would be:
=IFERROR(IF(M7="","",DATEVALUE(M7)),M7)
Otherwise, you could use IF() with ISERROR() as follows:
=IF(ISERROR(DATEVALUE(M7)),M7,IF(M7="","",DATEVALUE(M7)))
Hope that helps!
Mike

Related

Need to extract substring from string surrounded by special character in VBA [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to VBA,I have a string I_HEAD_FOR
I wanted to extract the substring which is started and ended by special character '_'(Underscore) in VBA.
I need code snippet which can do above task,Could you please help me with it
Here in this case The code should extract substring HEAD
This the idea is from:
I_HEAD_FOR
to get as a result: HEAD
How about:
Public Function UnderScore(s As String) As String
UnderScore = Split(s, "_")(1)
End Function
If you want to use Excel, without VBA, this is the way you should think to make the formula:
On which position is the first _ sign?
Knowing the first position of the first sign, can I find the second _ sign?
Now, knowing the first and the second position, is there a way to extract only the string, which is between the two positions?
How can I unite the whole party into one formula, without getting crazy?
The Formulas SEARCH and MID together are pretty powerful. The SEARCH formula has optional value, saying where should you start search (see the second formula). Thus, we add +1 to the first found value. In the second formula, the -1 is added, because we do not want the position of the _, but the position before.
The formulas in text:
=SEARCH("_",A1,1)
=SEARCH("_",A1,SEARCH("_",A1,1)+1)-1
=MID(A1,B1+1,B2-B1)
=MID(A1,SEARCH("_",A1,1)+1,SEARCH("_",A1,SEARCH("_",A1,1)+1)-1-SEARCH("_",A1,1))

vba excel - Issues with multiple time formats [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Files here
Hello,
I am trying to write code to format a report. The main issue with the report is that some cells store multiple pieces of data in a cell when each row should only have one piece of data in each cell. My solution thus far has been to use the text to columns function for the affected cells, paste all other data transposed, and finally copy and re-transpose the new data over the original report area. This has worked so far, but I am running into an issue with cells that store time. Cells with single times (such as 13:00) are in the custom format hh:mm, which vba converts to a a decimal number. Cells with multiple times (such as 11:009:008:0010:30) are viewed as a string.
The code I am currently using to split the times works for multiple time cells because it searches the string for ":", but vba does not detect the ":" in the custom format cell 13:00 since internally it sees that cells value as something like 0.56412.
I am kind of at a loss as to what to do here. I can't change the format that the report arrive to me in. My thought was that maybe I could find a way to turn the custom format "13:00" into a string "13:00" instead of 0.56412
I have attached the code file and the truncated dummy report I am testing to code on. I would like to thank everyone who responds for their help in advance!
to change a single time to a string, you can use format and identify the value to excel as a string using '
example:
'immediate window
[a1]="'"&format([a1],"hh:nn")
'or, using a range
range("A1")="'"&format(range("A1"),"hh:nn")
you can use isnumeric to check if it's a single time. True means it's a time, False means it's a string of times

Automatic excel cell fill up from another sheet [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here's the situation:
I have two excel sheets.
The first sheet contains a table of product codes and product descriptions (Two columns A and B).
I have a second sheet where someone is supposed to enter the product code and automatically have the next cell fill up with the product description and the cell next to it with the time.
I was wondering if that's do-able without VBA? If so, can someone give me start.
Best,
You could have the first cell that you want to auto fill have an IF statement, where if the cell has no value, nothing happens, and anything other than that gets a calculation.
Using A2:B100 as a Range if you have a header Row. Adjust to your own needs, of course.
IF FUNCTION
'IF (condition, result if true, result if false)'
VLOOKUP FUNCTION
'VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)'
=If($A1 = "", "", VLOOKUP(A1,Sheet1!$A$2:$B$100,2))
The next cell over would have something similar:
=IF($A1 = "", "", NOW())
This will get you the time. You will also have to have the cell format set to Time.
There is a problem with that. The screenshot below illustrates it.
It will just keep refreshing with the current time over and over. I would use a bit of VBA to solve that by setting the value property instead of a formula.
Sheets("Sheet2").Cells(row,col).Value = Now()
You could copy and paste the value into another cell. Just the value. Not the formula.
Or you could check out this article: about generating Time Stamps.
edit: included VBA solution

Excel - Copy the displayed value not the actual value

I am a pilot, and use a logbook program called Logten Pro. I have the ability to take excel spreadsheets saved from my work flight management software, and import them into Logten Pro using the CSV format.
My problem however, is that the work flight management software, exports the date and time of take-off of a flight into one cell in the following excel format: DD/MM/YYYY H:MM:SS PM.
This is handled fine by Excel, and is formatted by default to DD/MM/YY even though the actual value is more specific, comprising of the full length date and time group.
This is a problem because Logten Pro will only auto-import the date if it is in DD/MM/YY format, and there is no way to pull out just the displayed DD/MM/YY date rather than the full date time group actual value, unless you manually go through and delete the extra text from the function box.
My question is: Is there a VBA macro that can automatically copy the actual displayed text, and paste it into another cell, changing the actual value as it does, to just the DD/MM/YY value? Additionally, can this be made to work down a whole column rather than individual cells at a time?
Note I have no VBA experience so the perfect answer would just be a complete VBA string I could copy and paste.
Thank You.
As pointed out in the comments, you'd better not use VBA but formulas instead.
This formula:
TEXT(A1,"dd-mm-yyy")
will return the formated date in a text way. You can drag and drop the formula in the whole range of your cells and Copy/Paste Special > Values so that you will only have the needed values to get imported in Logten Pro.
There are three options using formulas.
Rounddown
Excel stores the date time as a number and uses formatting to display it as a date.
The format is date.time, where the integer is the date and the fraction is the time.
As an example
01/01/2012 10:30:00 PM is stored as 40909.9375
All the values after the decimal place relate to the hours and minutes
So a formula could be used to round the number down to a whole number.
=ROUNDDOWN(A1,0)
Then format the value as a short date.
It will then display as 01/01/2012
INT
As above, but using a different formula to get rid of the fraction (time)
=INT(A1)
Text
Alternately the date only could be extracted as text using this formula
=TEXT(A1,"dd/mm/yyyy")
It will then display as 01/01/2012
I'm a bit late to the party on this one, but recently came across this as was searching for answers to a similar problem.
Here is the answer I finally came up with.
Option Explicit
Sub ValuesToDisplayValues()
Dim ThisRange As Range, ThisCell As Range
Set ThisRange = Selection
For Each ThisCell In ThisRange
ThisCell.Value = WorksheetFunction.Text(ThisCell.Value, ThisCell.NumberFormat)
Next ThisCell
End Sub
This answers the question as asked, apart from the new values are pasted over the existing ones, not into a new cell, as there is no simple way to know where you would want the new values to be pasted. It will work on the whole range of selected cells, so you can do a whole column if needed.

Determine the number of rows in a range [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know the range name of the start of a list - 1 column wide and x rows deep.
How do I calculate x?
There is more data in the column than just this list. However, this list is contiguous - there is nothing in any of the cells above or below or either side beside it.
Sheet1.Range("myrange").Rows.Count
Function ListRowCount(ByVal FirstCellName as String) as Long
With thisworkbook.Names(FirstCellName).RefersToRange
If isempty(.Offset(1,0).value) Then
ListRowCount = 1
Else
ListRowCount = .End(xlDown).row - .row + 1
End If
End With
End Function
But if you are damn sure there's nothing around the list, then just thisworkbook.Names(FirstCellName).RefersToRange.CurrentRegion.rows.count
Why not use an Excel formula to determine the rows? For instance, if you are looking for how many cells contain data in Column A use this:
=COUNTIFS(A:A,"<>")
You can replace <> with any value to get how many rows have that value in it.
=COUNTIFS(A:A,"2008")
This can be used for finding filled cells in a row too.
You can also use:
Range( RangeName ).end(xlDown).row
to find the last row with data in it starting at your named range.
I am sure that you probably wanted the answer that #GSerg gave. There is also a worksheet function called rows that will give you the number of rows.
So, if you have a named data range called Data that has 7 rows, then =ROWS(Data) will show 7 in that cell.
That single last line worked perfectly #GSerg.
The other function was what I had been working on but I don't like having to resort to UDF's unless absolutely necessary.
I had been trying a combination of excel and vba and had got this to work - but its clunky compared with your answer.
strArea = Sheets("Oper St Report CC").Range("cc_rev").CurrentRegion.Address
cc_rev_rows = "=ROWS(" & strArea & ")"
Range("cc_rev_count").Formula = cc_rev_rows