Change date format with vba - excel

I would like to change the date format of a cell with VBA, but I have no idea for that. Let take below as our example, Column A is the input and Column B is the output as it must be in Thai Calendar...
My goal is extracting the value in Column A of Sheet1 to many Sheets with the converted Date...

Here is how you can copy data from cell 'A1' to 'B1' and convert date type to Thai.
Dim mydate As Date 'create date variable
mydate = Range("A1").Value 'copy date value from Cell A1
Range("B1").Value = mydate 'paste date value into cell B1
Range("B1").NumberFormat = "[$-th-TH,107]d mmmm yyyy;#" 'convert copied format to Thai
Hope this answers your question.

sorry for that I have low rep to comment.
date change is not a code issue.
change from western date to buddadate is adding 543 yearsto western date
as excel using the number for the date.
1 equals to 1900-01-01
2 equals to 1900-01-02
43752 equals to 2019-10-14
you need to add 543 years to western date
put this on b1 cell.
=A1*1+543*365+rounddown(543/4,0)
remind that leap year comesevery 4 years so that you could adjust +rounddowns() part to correct date

Related

Can't change year as text format to date format in Excel

I have 1 column that only have year value but formatted as general. I really need that value but formated as date in year only. I have tried to change the format to date, use custom to yyyy and use =year(a1) function, but it's useless. any solution?
Dates in Excel are integer values. First date Excel can recognize is 01/01/1900 which integer value is 1, number 2 would be 02/01/1900 and so on.
Number 2011 is 3rd July 1905 and because you are applying format yyyy Excel returns 1905.
You need first to do a proper date, not converting directly number 2011. So you need to type a date like 01/01/2011 (to be honest, any date from the 365 days of year 2011 would work but it is most usual to write on the first day of the year in these cases because you want just the year).
So you can do like this:
Formula in cell B1 is =DATE(A1,1,1) This will return always the first day of the year.
Now format B1 to yyyy and then it would be your expected output.
And just notice a fact: the date 01/01/2011 got an integer value of 40544 :)

Counting rows with start date before AND end date after in Excel

I have an excel sheet with an unique id column, start date column and end date column:
ID | start date | end date
I want to count the rows (or id's) with a start date BEFORE 01-01-2020 AND and end date which is AFTER 01-01-2020 OR empty.
All dates are in dd-mm-yyyy format.
I solved it in the following way:
Added two columns named 'Start Year' and 'End Year' with the following formula:
=IF(B1;YEAR(B1);"")
With the B column the start date column and for end year it references the end date
column of course. This returns the year and if the cell is empty it returns empty.
Then I used pivot tables. Not a direct solution but it works and it is not too complicated.

Compare dates yyyy with dd/mm/yyyy

In excel is it possible to compare a year yyyy (say 2016) with date dd/mm/yyyy (say 01/01/2015) and find out which is greater. My assumption would be that the date in yyyy format will always be the first day of the year.
So for example
2015 v 01/01/2014 would return true
2015 v 01/01/2015 would return false
2015 v 01/01/2016 would return false
I can code this in VBA but the user needs this in a cell in an excel spreadsheet.
One option would be to write the VBA code in a function in a code module, that returns the greater value.
Public Function CompareDates(date1 AS Date, date2 As Date) As Date
'Compare date1 and date2
'CompareDates = whichever date is greater
End Function
You can then simply add the formula =CompareDates(date1, date2) in the cell
Assuming A1 = 2015 and B1 = 01/01/2015 you can try:
A1=YEAR(B1)
Do you mean something like:
=DATE(A1,1,1)>B1
Assuming your Years are in Column A and Dates are in Column B.

Excel Countif using month and letter as Criteria's

I have a list of sales data that includes the date of the sale and also a sales type code. I'm trying to use COUNTIFS to count how many sales of a specific type there were during each month .
So for example:
Date. Sales Type Code
10/01/2014. S
12/01/2014. S
15/01/2014. O
18/01/2014. S
02/02/2014. O
08/02/2014. S
Would give me the following results:
Code S
January - 3
February - 1
Code O
January - 1
February - 1
I have the formula
COUNTIFS('SALES LEDGER'!A:A,F2,'SALES LEDGER'!C:C,"<"&EOMONTH('MONTH Sales by Sales Type'!$C$1,0)+1)
Where A:A is the list of Sales Type Codes, F2 is the code I want to count, C:C is the list of dates and C1 is the first of the month I want to count (ie. 01/01/2014).
This works fine for January, giving me the expected result. But when I change C1 to 01/02/2014, it counts January and February together, rather than just February.
If anyone has any suggestions it would be appreciated!
Thanks
Your COUNTIFS formula specifies an end date but no start date, if you want to count for a specific month and year (based on C1) you need another criterion to specify the start date using C1, i.e.
=COUNTIFS('SALES LEDGER'!A:A,F2,'SALES LEDGER'!C:C,"<"&EOMONTH('MONTH Sales by Sales Type'!$C$1,0)+1,'SALES LEDGER'!C:C,">="&'MONTH Sales by Sales Type'!$C$1)
You could use this one (if your range with dates doesn't contain empty cells):
=SUMPRODUCT((MONTH(A2:A100)=1)*(B2:B100="S"))
or (if your range could contain empty cells)
=SUMPRODUCT((TEXT(A2:A100,"mmm")="Jan")*(B2:B100="S"))
If you need to add also year condition, just add *(YEAR(A2:A100)=2014) in any formula:
=SUMPRODUCT((TEXT(A2:A100,"mmm")="Jan")*(YEAR(A2:A100)=2014)*(B2:B100="S"))
Assuming:
it is amenable to use helper columns
Dates in col A
Sale Type Codes in col B
Results placed from A9:C11
Screenshot:
Add a helper col C to extract Month from date. Add this formula in C2 and drag down:
=TEXT(A2,"Mmm")
Add this formula in B10 and drag down and across:
=COUNTIFS($B$1:$B$7,B$9,$C$1:$C$7,$A10)
with the code below you can retrieve the month each date is in:
dim objDate as Date
dim i as integer
dim intMonth as integer
for i = 2 to 'number of row
objDate = CDate(cells(i, 1))
intMonth = Month(objDate)
next i
After figuring out the month with a simple loop you can count the number of sales in each month.

Create date by two cells - one with date and other with hour in Excel

I would like to create a date like this : 21/10/2013 18:45 in Excel.
I have one cell with the date : 21/10/2013
one more cell with : 18:45
How can i create a date like this 21/10/2013 18:45?
If your date is in A1 and your time is in A2, then use this formula
=A1+A2
Then format the cell as d/m/yyyy hh:mm
Dates in Excel are integers that represent the number of days since 12/31/1899. Times in Excel are decimals between 0 and 1 and represents the fraction of a day that has passed. So noon would be 0.5 because it's 1/2 a day. That's why you can just add them together.
In A1 enter:
=DATE(2013,10,21) and format this cell as "dd/mm/yyyy"
In B1 enter:
=TIME(18,45,0) and format this cell as "[hh]:mm"

Resources