I'm exploring the use of Userform on my Excel VBA project and I want to create a form which will convert date from a DTPicker into text on a text box.
The reason for doing this is because I need the date to be in a specific format of yyyy/mm/dd to search an SQL query and the DTPicker value always default to dd-m-yyyy. Giving me no results in the query.
This is what I have so far:
Private Sub convertDate()
TextBox1 = DateFrmMac.Value
Me.TextBox1.Text = Format(Me.TextBox1.Text, "yyyy/mm/dd")
TextBox2 = DateToMac.Value
Me.TextBox2.Text = Format(Me.TextBox2.Text, "yyyy/mm/dd")
End Sub
During testing if the date is for example 29/1/2020, the conversion can work as intended. But if the day is below 12, like 8/11/2019, the day and month will mix up its values and give me the wrong date.
What can I do to prevent this from happening?
Private Sub convertDate()
Dim d As Date, strT1 As String, strT2 As String
strT1 = CStr(DateFrmMac.Value): Debug.Print strT1 'for 2019/12/5 use
d = DateSerial(Split(strT1, "/")(0), Split(strT1, "/")(1), Split(strT1, "/")
(2))
'for strT1 = "5/12/2019" use:
d = DateSerial(Split(strT1, "/")(2), Split(strT1, "/")(1), Split(strT1, "/")
(0))
Me.TextBox1.text = Format(d, "yyyy/mm/dd")
strT2 = CStr(DateToMac.Value)
d = DateSerial(Split(strT2, "/")(0), Split(strT2, "/")(1), Split(strT2, "/")(2))
Me.TextBox2.text = Format(d, "yyyy/mm/dd")
End Sub
Related
I am trying to convert the date format of my cells as the csv format they are delivered in shows a date but excel doesn't recognize it as a date (it shows "Standard" as format and the dates are aligned on the left, hence not dates in excel).
Dim lr11 As Integer
Dim dates11 As Date
lr11 = WS1.Cells(WS1.Rows.Count, "C").End(xlUp).row
For dates11 = 2 To lr11
WS1.Cells(dates11, 3).Value = CDate(Cells(dates11, 3).Value)
Next dates11
The above code sometimes works in a Test Sub() but when used in my main Sub, I always get "12:00:00 AM" in all cells instead of dates.
What am I doing wrong?
Thanks!
If you have strings that look like dates in the format DD.MM.YYYY you can split them and create a numeric date using DateSerial like below:
Option Explicit
Public Function ConvertStringDDMMYYYYtoDate(ByVal InputString As String) As Date
Dim RetVal As Date
Dim Parts() As String
Parts = Split(InputString, ".")
If UBound(Parts) = 2 Then
RetVal = DateSerial(Parts(2), Parts(1), Parts(0))
If Not Format$(RetVal, "DD.MM.YYYY") = InputString Then
MsgBox "Input String is not a real date", vbCritical
RetVal = 0
End If
End If
ConvertStringDDMMYYYYtoDate = RetVal
End Function
Then use it like
For dates11 = 2 To lr11
WS1.Cells(dates11, 3).Value = ConvertStringDDMMYYYYtoDate(WS1.Cells(dates11, 3).Value)
WS1.Cells(dates11, 3).NumberFormat = "DD.MM.YYYY" ' format it however you like it to look like
Next dates11
I want to convert a date in a cell to the date function so it is a formula. How do I get the date (using VBA), any date, say, 13 Jun 2020 to =DATE(2020, 6, 13) using variables for the year, month, and day. My code I have tried but won't work. The activecell shows 13-Jun-2020 as a date but appears in the function box as 13/06/2020
Sub ConvertDateToDateFunction()
Dim mvDay, mvMth, mvYr As Integer
mvDay = Left(ActiveCell, 2)
mvMth = Mid(ActiveCell, 4, 2)
mvYr = Right(ActiveCell, 4)
ActiveCell.Value = "=DATE(mvYr, mvMth, mvDay)"
End Sub
You have two problems. Here is the solution to the smaller one. The code below would do what you intend. It would convert a text string in the ActiveCell to a function of similar value and insert it in the cell below the ActiveCell.
Sub ConvertDateToDateFunction()
' if you don't say what it's supposed to be it'll be a Variant
Dim mvDay As String, mvMth As String, mvYr As String
mvDay = Left(ActiveCell.Value, 2)
mvMth = Mid(ActiveCell.Value, 4, 2)
mvYr = Right(ActiveCell.Value, 4)
ActiveCell.Offset(1).Formula = "=DATE(" & mvYr & "," & mvMth & "," & mvDay & ")"
End Sub
It's not entirely easy to insert a date as a text string in Excel because Excel will try to recognize a date for a date. Observe that any part of a string is a string, not an integer.
Now about your much bigger problem which is that you don't understand how Excel handles dates. It is such a big problem because you are trying to create a date in Excel in various ways and you run into all sorts of trouble. Read up on the subject here.
To give you a taste of what you will learn: what you see displayed in a cell isn't what the cell contains. There might be a formula in it and you see a number. And there might be a date and you see a string. What you see is determined by the cell's format. I think Chip Pearson's article will cover that topic. If you need to know more, look for "Cell formatting" on the web.
Your macro won't work because the date is a "real date" and not a string.
Try the following to convert the contents of cells containing a real date to a formula which will return the same date:
Option Explicit
Sub dtToFormula()
Dim R As Range, C As Range
Dim vDateParts(2)
Set R = [a1:a10]
'Set R = ActiveCell 'or Selection whatever range you want to convert
For Each C In R
If IsDate(C) And Not C.HasFormula Then
vDateParts(0) = Year(C.Value2)
vDateParts(1) = Month(C.Value2)
vDateParts(2) = Day(C.Value2)
C.Formula = "=DATE(" & Join(vDateParts, ",") & ")"
End If
Next C
End Sub
I slightly amended some code I found online.
Purpose:
Click on a 'Print with Dates' button, then enter start date, and have Excel automatically generate/print a months worth of timesheets (much better than the previous spreadsheet having 6 weeks of pages to print, and you had to edit every date manually).
Issues:
It prints weekends, which wastes paper. Is there a way it can refer to a list of dates (weekends, public holidays), and not generate those for printing?
You'll see the date format is m/d/yyyy in the code, which strangely prints as dd/mm/yyyy (which is what I wanted). When the code was dd/mm/yyyy it was printing correctly (20/03/2019), but if it goes to the following month it was switching to American format m/d/yyyy (04/20/2019). I know it doesn't seem to make sense, but having it as m/d/yyyy actually prints as dd/mm/yyyy across any start/end dates. I'd like to know why, and also have dd/mm/yyyy in the code correctly printing across any date range.
CODE:
Sub PrintSheet()
Dim s As String
Dim d As Date
s = InputBox(Prompt:="Please enter the start date")
If Not IsDate(s) Then
MsgBox "Incorrect date", vbExclamation
Exit Sub
End If
For d = CDate(s) To DateAdd("m", 1, CDate(s)) - 1
Range("F2").Value = Format(d, "dddd")
Range("I2").Value = "" & Format(d, "m/d/yyyy")
ActiveSheet.PrintOut
Next d
End Sub
Cheers in advance :)
You can use the weekday function. It returns a number from 1 to 7. You can filter for 1-5 only for weekdays.
Sub PrintSheet()
Dim s As String
Dim d As Date
s = InputBox(Prompt:="Please enter the start date")
If Not IsDate(s) Then
MsgBox "Incorrect date", vbExclamation
Exit Sub
End If
For d = CDate(s) To DateAdd("m", 1, CDate(s)) - 1
If Weekday(d, vbMonday) < 6 Then
Range("F2").Value = Format(d, "dddd")
Range("I2").Value = "" & Format(d, "m/d/yyyy")
'MsgBox ("printing ") 'for testing
ActiveSheet.PrintOut
End If
Next d
End Sub
Platform: OS window 7
Excel: 2007
Issue : I had a .csv file with some data and date capture from other place. The data(date) is located in cell A2 having the category under Date. (e.g 18/3/2014)
Firstly there is this Locale (location), stated English (Singapore). I wanted to change it to English (United States)
After that then change the date format to "M/D/YYYY"
What i had tried:
ExcelApp.ActiveWorkbook.Sheet1.Range("A2").NumberFormat = "M/D/YYYY"
it prompted me run time error "438"
object doesn't support this property or method.
how can make it that it return me the cell value as "3/18/2014"?
If you look in A2 and see:
18/3/2014
then its either a String or a formatted date. it does not matter which ;run this to fix it:
Sub FixDate()
Dim s As String
With Range("A2")
s = .Text
ary = Split(s, "/")
.Value = DateSerial(ary(2), ary(1), ary(0))
.NumberFormat = "m/d/yyy"
End With
End Sub
EDIT#1
If you want to Dim ary, then:
Option Explicit
Sub FixDate()
Dim s As String, ary
With Range("A2")
s = .Text
ary = Split(s, "/")
.Value = DateSerial(ary(2), ary(1), ary(0))
.NumberFormat = "m/d/yyy"
End With
End Sub
Well, a CSV file is just a Comma Separated Value File, so it doesn't support any of the cell formatting that an XLS or XLSX file would. So what you can do is:
If it only contains a date.
Dim dt As Date : dt = cdate(Range("A2").Value)
Range("A2").Value = Format(dt, "MM/d/yyyy")
If it contains a date and time and you only want the date.
Dim dt As Date : dt = cdate(Range("A2").Value)
Range("A2").Value = FormatDateTime(dt, 2)
if it contains a date and time and you want both.
Dim dt As Date : dt = cdate(Range("A2").Value)
Range("A2").Value = FormatDateTime(dt)
(Excel 2010 VBA)
I have a cell (A1) containing a date in the format of mmm-yy ("Custom" category).
Foe example, if I enter 1/6/13 the cell shows June-13. That's fine.
In my VB macro I need to check this date whether the month is the current month and whether the year is the current year. I don't care about the day.
Does this help you:
Public Sub test()
d = Sheet1.Range("A1")
n = Now()
If Year(d) = Year(n) And Month(d) = Month(n) Then
MsgBox "It works"
End If
End Sub
Thanks to Dave and MiVoth I did :
Dim xdate As Date
xdate = Worksheets("sheet1").Range("A1")
If Month(Date) = Month(xdate) And Year(Date) = Year(xdate) Then
MsgBox "OK"
Else
MsgBox "not OK"
End If
That did the job!
Thank a lot to everyone,
Gadi
How about this:
Function MonthYear() As Boolean
MonthYear = False
If Not IsDate(Cells(1, 1)) Then Exit Function
If Month(Date) = Month(Cells(1, 1)) And Year(Date) = Year(Cells(1, 1)) Then
MonthYear = True
End If
End Function
The function returns true if month and year are the same as current date. If not it returns false.