Parsing formula to cell using VBA - 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).

Related

vba customize cells format

I am needing to customize cells with simple thousands format, like 1000, without any separator or decimal.
However, I wish to remove text fonts other than a number when they are input.
For example, I want to input 120118, however in my paper from which I am copying that figures, it is formatted as a date, thereby 12/01/18. I am needing Excel to simply keep it as 120118 after typing, removing the slash (/). I have seen similar settings in access queries.
Have you tried simply pasting only the cell value with:
Selection.PasteSpecial Paste:=xlPasteValues
Or just clear the cell format and format it again with your desired format.
Try:
Selecting the range
Home > Number > Number Format (or Ctrl+1 I think) > Custom
Enter ddmmyy
Okay
Can be done programmatically e.g.
Thisworkbook.worksheets("Sheet1").range("A1:A50").numberformat = "ddmmyy"
The above would only be a visual/cosmetic change and the internal value of each cell would still be a date (technically a number) for calculation purposes.
However, if I've misunderstood and you instead want to go from the date 21 Jan 2018 to the number 210118, I think you would need to get the range's value(s), format as DDMMYY string, then clng() - or maybe (DD*10000) + (MM*100) + (YY) might work, then format as "000000" to preserve leading zeros.

MATCH() function can't locate hours

I've created a Time Sheet for my dog walkers to fill in. They enter TIME STARTED and TIME ENDED for each of their dog walks and then a TOTAL TIME is generated using the =X-Y formula. This TOTAL TIME is formatted to be displayed in hh:mm but its true VALUE is a long integer.
Using the MATCH and INDEX functions, I've set up a formula to match the TOTAL TIME value generated to an index on the "Payment Schedule" sheet and locate the respective payment.
I keep getting errors that state the total time VALUES can't be matched but I know that formatting isn't the issue and the values are clearly on the "Payment Schedule" sheet. And when the MATCH function does return a row it returns the incorrect row, which locates the incorrect payment/rate.
Here is the Google Sheets I'm having trouble with.
Found the answer on another website:
it's floating point rounding error on the time values. I don't fully
understand what's happening, but I was able to reproduce it on my
system, and I found a work-around: Change your formula to
=INDEX(B:B, MATCH($I$4+TIME(0,0,1), A:A)) This adds one second (TIME(0,0,1); the arguments are TIME(hours,minutes,seconds)) to the I4
value; that seems to be enough to get it "over the hump", so that it
tests as being ≥ the value in A4 (or A7 or A10). BTW, I tried
TIME(0,0,0.9), but apparently TIME() won't honor fractional seconds,
and so it just treats that as TIME(0,0,0); i.e., just plain zero. If
you want to get a millisecond, you can use TIME(0,0,1)*0.001.
link

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")

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.