How can I convert 20110114 (YYYYMMDD) to week, eg WK02-11, in excel ?
Thanks.
First convert the number in a date. Supposing your number is in A1 cell
=DATE(LEFT(A1;4); MID(A1;5;2); RIGHT(A1;2)
Then use WEEKNUM
=WEEKNUM(DATE(LEFT(A1;4); MID(A1;5;2); RIGHT(A1;2), 2)
(gives 3)
Then, if you want, you could embellish the result:
="WEEK-" & WEEKNUM(DATE(LEFT(A1;2); MID(A1;5;2); RIGHT(A1;2), 2) & "-" & LEFT(A1;4)
Related
I have two cells that have a date field. I have code to delete records based on dates.
Scenario 1: (Working)
Column has the date as 30/04/2020 10:00:00 (dd/mm/yyyy format).
Scenario 2: (discrepancy)
Column has the date as 01/05/2020 10:00:00 (dd/mm/yyyy format).
My code is treating it as 01JAN2020.
Sample code:
a=worksheets("Sheet1").cells(row,col).value ' captures value in the required column
b=format (date-1,"DD-MM-YYYY") ' Captures yesterdays date for comparison purpose
'This is to convert the required column format
' to the format that we are comparing the value with
c=format(a,"DD-MM-YYYY")
if (b <> c) then
msgbox("This is an invalid record")
else
msgbox("Valid")
end if
The code works when the date is 30/04/2020 10:00:00 which is treated as 30-APR.
But 01/05/2020 10:00:00 is treated as 01-JAN and the comparison goes wrong.
If both dates are truly Valid excel dates, then converting to string format just adds unnecessary noise.
a = Worksheets("Sheet1").Cells(row, col).Value ' captures value in the required column
b = Date - 1
If (a <> b) Then
MsgBox ("This is an invalid record")
Else
MsgBox ("Valid")
End If
should work.
If the dates are NOT valid dates, then you need to correct that problem.
The issue you describe is usually created by incorrectly opening a text/csv file that has dates in a format different from your Windows Regional Settings. The "fix" is to import the data, so you can tell Excel the proper format at the time of importation.
ANSWER:
I have tried using the SPLIT and CDATE combination and that did solve my purpose.
a1=split(worksheets("Sheet1").cells(row,col).value,"/") ' captures value in the required column
a=cdate(a(1) & "/" & a(0) & "/" &a(2))
b=format (date-1,"DD-MM-YYYY") ' Captures yesterdays date for comparison purpose
c=format(a,"DD-MM-YYYY") 'This is to convert the required column format to the format that we are comparing the value with
if (b <> c) then
msgbox("This is an invalid record")
else
msgbox("Valid")
end if
I have a time in a cell, want to minus 2 hrs and add 2hrs to the time and display in excel
Example : 02/08/2020 11:00AM
So Minus 2hrs and plus 2 hrs next cell must display as 9:00AM-13:00PM
I tried the below formula but displays like 43811.0577893518 - 43811.2244560185
=CONCATENATE(A1-TIME(2,0,0)," ","-"," ",A1+TIME(2,0,0))
Subtract & add two hours (2/24) to your date
=TEXT(A1-(2/24),"hh:mm AM/PM") & " - " & TEXT(A1+(2/24), "hh:mm AM/PM")
Use TEXT to format the output:
=CONCATENATE(TEXT(A1-TIME(2,0,0),"HH:MM AM/PM")," ","-"," ",TEXT(A1+TIME(2,0,0),"HH:MM AM/PM"))
what excel formula should I use to change a date value that is numbered into a date format. For example
5312018 into 5/31/2018
or 10202018 into 10/20/2018
See also image example below.
You could try:
=IF(LEN(A2)=8,LEFT(A2,2),LEFT(A2,1)) & "/" & IF(LEN(A2)=8,MID(A2,3,2),MID(A2,2,2)) & "/" & RIGHT(A2,4)
Results:
I am attempting to format a single number stored as a text value.
For example, I would like to convert:
5145350002005000080
To:
5145-350002-00500-0080
The formula I am using is:
=text(A1,"0000-000000-00000-0000")
The output I am receiving is:
5145-350002-00500-0000
Why are the last 4 characters "0000" instead of "0080" as I would expect? Is there a character limit, or is my formula incorrect?
Quote from Large Number Arithmetic:
The limit in Excel is 15 significant digits in a number. Enter a 16
digit credit card number and 1234567890123456 will become
1234567890123450.
Actually, even 5145350002005001111 will result in 5145-350002-00500-0000.
Moreover, take a look at formula bar when your input cell is selected - for my Excel 2007 I see:
Hope that was helpful)
EDITED:
As a solution to solve the task - keep your numbers formatted as text and use the following formula:
=LEFT(A1,4)&"-"&MID(A1,5,6)&"-"&MID(A1,11,5)&"-"&RIGHT(A1,4)
Here is a custom function. Place it in a regular code module of the workbook and you can call it in the cell by =FormatLargeNumber("A1")
Public Function FormatLargeNumber(val As String)
'This function parses extremely large numbers per your example.
' Modify as needed.
FormatLargeNumber = Left(val, 4) & "-" _
& Mid(val, 5, 6) & "-" & _
Mid(val, 11, 5) & "-" & _
Right(val, 4)
End Function
I'm having big problems doing operation with the date in Excel VBA.
I have a form that has a textbox where the user will enter the date. The problem is that he may enter it in different formats (eg, 1.08.2011 for 1st of August, or 8/1/11 for the same day). Now what I want to do is to subtract some days from that date that he enters in the TextBox. I had to success so far and I don't know how to do it.
I tried something like this
Format((Format(Me.datalivrare.Value, "dd.mm.yyy") - 4), "dd.mm.yyyy")
Where datalivrare is that textbox where the user enters the date and 4 is the number of days I want to subtract from that date... and I want the format to always be dd.mm.yyyy no matter what they enter in that textbox.
I suggest looking at the DateAdd function for VBA
http://www.techonthenet.com/excel/formulas/dateadd.php
http://office.microsoft.com/en-us/access-help/dateadd-function-HA001228810.aspx
You could do the following:
Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
First cast to Date, then subtract days, then format appropriately:
Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
the best to add and substract from dates on vba is dateadd() (with negative number for substractions)
also, in your example code there's a missing y on the format string (it accepts 1, 2 or 4 y, only)
It is important to check if the user entered a value that VBA can interprit as a date so first you should:
If isDate(Me.datalivrare.Value) Then
str_Date = Format(DateAdd("d", -4, CDate(Me.datalivrare.Value)), "dd.mm.yyyy")
Else
MsgBox "Not a valid date value", vbCritical + vbOkOnly, "Invalid Entry"
End If
I think bluefeet's answer had the most information so far and I borrowed the use of DateAdd and CDate.
Just a simple answer, as many aren't using the OP's code.
Eg: Minus 4 days
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function
Sub DateTest()
Dim DateVar As Date
DateVar = DateAdd("d", -4, Date)
Debug.Print DateVar
End Sub