Excel - Copy the displayed value not the actual value - vba

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.

Related

Parsing formula to cell using VBA

Trying to place formula:
IF(TEXT(A4,”ddd”)=“Fri”,IF(TEXT(B4-INT(B4),”hh:mm:ss”)>=TEXT(TIME17,0,0 "),”hh:mm:ss”),”flag”," "),IF(TEXT(A4,”ddd”)=“Sat”,”flag”,IF(TEXT(A4,”ddd”)=“Sun”,”flag”,IF(TEXT(A4,”ddd”)=“Mon”,IF(TEXT(B4-INT(B4),”hh:mm:ss”)<=TEXT(TIME("8,30,0"),”hh:mm:ss”),”Flag”,"”)))))
into cell but using CHR(34) instead of " makes it too long in character length. Then tried using defined name and in "refers to:" used the evaluate formula i. e. =evaluate("T1"); T1 had the text version of the formula as above.
Create Date Create Time
03/05/17 07:28 AM
09/05/17 07:32 AM
13/05/17 07:20 AM
16/05/17 04:57 PM
17/05/17 10:17 AM
The spreadsheet contains date in column A and date-time formatted as time in column B. I am working out if the particular line is between the hours of Fri 5pm and Mon 8:30am.
If you just want a shorter formula, you could use
=IF(OR(WEEKDAY(A4,2)>5,AND(WEEKDAY(A4,2)=5,B4>17/24),AND(WEEKDAY(A4,2)=1,B4<8.5/24)),"Flag","")
In VBA, that could be coded as (for instance):
Range("C4").Formula = "=IF(OR(WEEKDAY(A4,2)>5,AND(WEEKDAY(A4,2)=5,B4>17/24),AND(WEEKDAY(A4,2)=1,B4<8.5/24)),""Flag"","""")
The above formulas assume that column B just contains the time, but if it contains the date and time, then you should use:
=IF(OR(WEEKDAY(B4,2)>5,AND(WEEKDAY(B4,2)=5,MOD(B4,1)>17/24),AND(WEEKDAY(B4,2)=1,MOD(B4,1)<8.5/24)),"Flag","")
In VBA, that could be coded as (for instance):
Range("C4").Formula = "=IF(OR(WEEKDAY(B4,2)>5,AND(WEEKDAY(B4,2)=5,MOD(B4,1)>17/24),AND(WEEKDAY(B4,2)=1,MOD(B4,1)<8.5/24)),""Flag"","""")
Correcting your original formula to what I think you intended it to be, would give you VBA code of (for instance):
Range("C4").Formula = "=IF(TEXT(A4,""ddd"")=""Fri"",IF(TEXT(B4-INT(B4),""hh:mm:ss"")>=TEXT(TIME(17,0,0),""hh:mm:ss""),""flag"","" ""),IF(TEXT(A4,""ddd"")=""Sat"",""flag"",IF(TEXT(A4,""ddd"")=""Sun"",""flag"",IF(TEXT(A4,""ddd"")=""Mon"",IF(TEXT(B4-INT(B4),""hh:mm:ss"")<=TEXT(TIME(8,30,0),""hh:mm:ss""),""Flag"","""")))))"
There are several things in that that could easily be simplified, e.g.:
TEXT(TIME(17,0,0),""hh:mm:ss"") could be more simply written as "17:00:00" (it's a fixed time that is going to have a fixed result), and
TEXT(B4-INT(B4),""hh:mm:ss"") could be simplified to TEXT(B4, ""hh:mm:ss"") (the time string is going to be the same whether you calculate it on the specific day, or whether you calculate it on 0 January 1900).

Today function equivalent in VBA in combination with countifs

I am having some problem with using a countifs formula in Excel / VBA. I have got the formula working perfect in Excel but ideally I want to use this in VBA with my form. Here is the formula in Excel which works a treat:
=COUNTIFS(Sheet1!A:A,"Place",Sheet1!K:K,"<"&TODAY())
will count the names places that are now in the past
=COUNTIFS(Sheet1!A:A,"place",Sheet1!K:K,">"&TODAY())
will count the names places that are current
I have five different Places in column A and hundreds of different dates in column K. The above formulas work well in Excel and return the correct values. I have spent hours trying to get this to work in VBA with my userform but keep getting various errors. The first part is not the problem but as soon as I get to the &today function it falls apart. From what I can see the &today function is not available in VBA and the &Date seems to be the recommendation. I have tried this but still get no where. I'm missing a trick (or several) here and I would really like to get this working in VBA rather than using the current formulas in Excel. The returned results are then displayed in textboxes on my form.
All ideas and feedback much welcome!
Second edit
================================
Thanks for the quick replies! Here is the actual code I am playing about with in VBA
'Count events by area'
Dim ListLondon As Long
ListLondon = .CountIf(Range("a1:a1998"), "London"), ("Sheet1!K1:K1998"), "<" & Date)
End With
Me.TextBox1 = ListLondon
I know the second part of the count if is all wrong regards the date - that's how I've left it for now. I am really hoping to use the current layout and a working Date / Today code at the end. Please show me what I've done wrong here!
====
oops - can see a mistake already - but the initial problem remains around the date issue. I should of used countifs as using multiple criteria.
You have to read the values of the cells to your VBA code. I recommend you to use Excel.Range object to do that. It can interpret the range like the edit line of the Excel, something like
Dim foo as Excel.Range
set foo = yourworksheet.Range("A1:B3")
Read the Date type data into VBA Date type variable by iterating through the cells.
Examine relation between the read data and the current date. Current date can be read by using the DateTime.Now function.
Increment a variable based on a decision

How to use UserForm Values for multiple things

I have UserForm1 which is a multipage userform and I am trying to access the information that was gathered through the form in a sub located in Module1. This sub will need to access several different values and do different things with those values so this is going to be a multipart question.
I have the below code in which I attempt to use one of the values as the upper limit of a For Next Loop. However the current problem is that when the code reaches this line it jumps to the Userform_Initialize routine.
For X = 1 To UserForm1.LocalOffer.Value
Second part of this question comes from inside the For Next loop from above. Where I have the below code. Which would ideally allow me to cycle through a series of similarly named Textboxes from the userform. Not even sure if that will work as the code keeps breaking before getting to that part.
Range("B" & X).Value = UserForm1.Controls("LocalTier" & Tier).Value
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
I am using Excel 2013 but the file will be ran on Excel 2007
Edit:
Turns out that problem 1 was fixed by not closing the userform with the X button but instead adding a line to hide the userform when you hit the last button. As it turns out my code for the second question worked just fine once i got past that. Only question left is the last one which I have no ideas on.
As from the comments, I see you don't need anymore to know about points 1 and 2, I will hence limit my answer to the point 3.
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
You can use either string manipulation, or date conversion.
Let's assume the Textbox is called myDateTextbox
String manipulation
Among the string manipulators that VBA provides, I would cite Left() and Right().
For example:
last_digit_of_the_year = Right(myDateTextbox.Text, 1)
will return you the last character of the string. On the other hand:
first_digit = Left(myDateTextBox.Text,1)
will return you the first digit of the string.
You can use the Len(myDateTextBox.Text) built-in to return the current length of the string.
Date conversion
You can simply convert your string into date using the CDate() function. Please note this function will return an error if you pass an invalid string. If your textbox contains 24/01/1990, you can first convert the string into a date:
myDate = CDate(myDateTextBox.Text)
Hence, you can retrieve day, month or year like this:
myYear = Year(myDate)
myMonth = Month(myDate)
myDay = Day(myDate)
Please note that CDate recognizes date formats according to the locale setting of your system.. Hence, if the format in the TextBox is not the same than the one of your system, then consider manipulating the string before to adapt it to the proper format. For example, if your system has settings DD/MM/YYYY and your textbox shows a MM/DD/YYYY type, you can "adjust it" as follows:
wrongFormat = myDateTextBox.Text
splittedDate = Split(wrongFormat,"/")
goodFormat = splittedDate(1) & "/" & splittedDate(0) & "/" splittedDate(2)
If wrongFormat was 1/18/2014 but your system would like to read it as 18/1/2014, it's now fine because goodFormat will be equal to 18/1/2014 after the split and re-build.

Date Formatting in Excel VBA

I have written a long procedure in VBA to manipulate a data set. Part of this involves using and formatting dates, and I can't get it to work properly.
The initial data as downloaded has dates in the format "yyyy-mm-ddThh:mm:ssZ" - e.g. 2014-12-11T04:59:00Z.
I then convert these into a date in UK format of "dd/mm/yyyy". Looping over all relevant cells, I use the following method:
initial_date = Range("A1").Value
Dim publish_date As Date
publish_date = DateValue(Mid(initial_date,9,2) & "/" & Mid(initial_date,6,2) & "/" & Mid(initial_date,1,4))
Range("A1").Value = publish_date
This seems to work fine. The cell automatically changes format to "Date" and when I calculate the difference between these dates and dates in another column, it works fine.
I then pick up these dates again and add to an array:
feed(1, 8) = Range("A1")
and then transfer this value into another array:
new_array(7, 1) = feed(1, 8)
Finally, I insert the value from the new array into a different cell. Having used UK date formatting thus far, I now want this value to display in the form "mm/dd/yyyy".
Range("E1") = new_array(7, 1)
Range("E1").NumberFormat = "mm/dd/yyyy"
This is where it goes wrong.
Running the code as above, the dates are all displayed as "dd/mm/yyyy" format. Excel changes the format of the cells to "custom". Finding the difference between these and other dates works, so the data seems to be stored correctly but not displaying as I want.
If I change the code to
Range("E1") = new_array(7, 1)
Range("E1").NumberFormat = "dd/mm/yyyy"
the dates still do not display correctly. Dates which can be written either way around (i.e. day is equal to or less than 12) display correctly, but are in fact the wrong date. i.e. 2014-12-11T04:59:00Z displays as 12/11/2014 which is what I want, but Excel thinks the date is the 12th November instead of 11th December. Dates such as 29/09/2014 which cannot be written both ways round display in UK format, but are not recognised properly by Excel which thinks the long date should be "29/09/2014" instead of "29 September 2014".
When I remove the formatting line completely, the results are the same.
I'm sorry for the rather long-winded explanataion, but there's clearly something I'm not understanding about how Excel and VBA handle, store and format dates. If anyone could enlighten me what's going wrong, I'd really appreciate it!
(Note, in all the code snippets above, where I quote e.g. Range("A1") this is shorthand. There is in fact a lot more code involved in looping and selecting values, but I know this works, so I am not concerned. The extracts above just demonstrate what happens for the first value in each loop.)
try
Range("E1").NumberFormat = "mm/dd/yyyy"
Range("E1") = Format(new_array(7, 1), "mm/dd/yyyy")

Copy Value Of Cell, Not Formula

I am trying to grab only the current number of minutes in an hour.
I use the NOW() function and I have the format of the cell it's in set to only mm. So in the cell display, if it's 9:08, I get 08.
Whenever I try a excel formula like =VALUE(D5) or =D5, I get (I'm assuming this is what it is) an unformatted date string.
I am now looking to macros using VBA to solve my problem.
I've tried:
Worksheets("Sheet1").Cells(23, 1) = Worksheets("Sheet1").Range("E20").Value
However I get the same thing as with my excel formulas. Is there any VBA way of getting only the value that's displayed in the cell and copied to a new area. It doesn't really need to be copied over, I just need to use the value further in my macro.
I'm open to other ways of getting the current number of minutes in the current hour as well.
Do try not to confuse formatting and data.
why not use =MINUTE(D5) in a cell which will set that cell to the minute value irrespective of formatting? Use that cell as your source in your VBA.
=HOUR(D5) would operate similarly but return the hour part.
If the displays 9:08
then select that cell and:
Sub dural()
Dim s As String
s = Split(ActiveCell.Text, ":")(1)
MsgBox s
End Sub
Note this is a String
Might be calculated like so:
=60*(NOW()*24-INT(NOW()*24))