ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel VBA - Data Type Conversion problem (https://www.excelbanter.com/excel-programming/297794-excel-vba-data-type-conversion-problem.html)

PaulC

Excel VBA - Data Type Conversion problem
 
All I want to do is display today's date in 3 separate boxes for Day
Month and Year.

I have tried the following code to split the date into 3 strings:

Dim TodaysDate As Date
Dim strDate As String
Dim myDay As String
Dim myMonth As String
Dim myYear As String

TodaysDate = Format(Date, "dd/mm/yy")
strDate = CStr(TodaysDate)
myDay = Left(Cells(strDate, 1), 2)
myMonth = Mid(Cells(srtDate, 1), 4, 2)
myYear = Mid(Cells(srtDate, 1), 7, 2)


However, it says there is a type mismatch on the line:
myDay = Left(Cells(strDate, 1), 2).

There must be a simple solution but I can't find it.

Pau

--
Message posted from http://www.ExcelForum.com


Don Guillett[_4_]

Excel VBA - Data Type Conversion problem
 
couldn't you have just used the first one to do all three by changing the
format

myDay = Format(Date, "dd")
myMonth = Format(Date, "mm")
myYear =Format(Date, "yy")




--
Don Guillett
SalesAid Software

"PaulC " wrote in message
...
All I want to do is display today's date in 3 separate boxes for Day,
Month and Year.

I have tried the following code to split the date into 3 strings:

Dim TodaysDate As Date
Dim strDate As String
Dim myDay As String
Dim myMonth As String
Dim myYear As String

TodaysDate = Format(Date, "dd/mm/yy")
strDate = CStr(TodaysDate)
myDay = Left(Cells(strDate, 1), 2)
myMonth = Mid(Cells(srtDate, 1), 4, 2)
myYear = Mid(Cells(srtDate, 1), 7, 2)


However, it says there is a type mismatch on the line:
myDay = Left(Cells(strDate, 1), 2).

There must be a simple solution but I can't find it.

Paul


---
Message posted from
http://www.ExcelForum.com/




JE McGimpsey

Excel VBA - Data Type Conversion problem
 
There's no need to use Format - the value passed to XL from VBA will be
parsed just as if it's a keyboard entry. One way:

Dim myDay As String
Dim myMonth As String
Dim myYear As String
myDay = CStr(Day(Date))
myMonth = CStr(Month(Date))
myYear = CStr(Year(Date)


You got the type mismatch error because Cells() requires a number as its
first argument, not a string (see XL/VBA Help!).


In article ,
PaulC wrote:

All I want to do is display today's date in 3 separate boxes for Day,
Month and Year.

I have tried the following code to split the date into 3 strings:

Dim TodaysDate As Date
Dim strDate As String
Dim myDay As String
Dim myMonth As String
Dim myYear As String

TodaysDate = Format(Date, "dd/mm/yy")
strDate = CStr(TodaysDate)
myDay = Left(Cells(strDate, 1), 2)
myMonth = Mid(Cells(srtDate, 1), 4, 2)
myYear = Mid(Cells(srtDate, 1), 7, 2)


However, it says there is a type mismatch on the line:
myDay = Left(Cells(strDate, 1), 2).

There must be a simple solution but I can't find it.

Paul


PaulC

Excel VBA - Data Type Conversion problem
 
Many thanks - that works perfectly

Paul Christi

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 01:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com