Marco Soto
Marco Soto

Reputation: 61

How to save date data read from excel worksheet using vba code?

I have data saved in an excel worksheet in the form of time data (HH:mm:ss). However when I read the data from the chart and attempt to save the information the value is not saved correctly and I am given a string that is a decimal.

sub generic()
dim s as string
s = Range("A1").Value 'A1 is where the value is stored. In this case it is 7:30:30.
MsgBox s
end sub

The output s is a decimal (0.623.. I believe). I simply want the output to be the information in cell A1 as a string (7:30:30). I also cannot affect worksheet properties everything must be done with vba code.

Upvotes: 0

Views: 62

Answers (2)

Shai Rado
Shai Rado

Reputation: 33672

If you want to keep Dim s as String, you can use the code below:

s = Format(Range("A1").Value, "HH:MM:SS")
MsgBox s

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37347

You need to change the line

Dim s As String

to

Dim s As Date.

It should work now.

Upvotes: 1

Related Questions