Problem with date values

T

t.aadland

I'm trying to write a macro in Excel 2003. In this macro, I want to
use the date values of three different dates, but I'm not able to make
it work. The code below shows three different methods I've tried, but
they all just return 0. I have also tried using Double or Date instead
of Integer, but it turns out the same. When debugging this code, it
looks like the second range selection doesn't work. Any suggestions?

With ActiveSheet
.Range(.Cells(5, 9), Cells(7, 9)).Select
'...
Dim dateBefore As Integer
Dim dateSepTest As Integer
Dim dateAfter As Integer
dateBefore = .Range(.Cells(5, 1)).Value
dateSepTest = .Range(.Cells(6, 1)).Formula
.Range(.Cells(7, 1)).Select
dateAfter = Selection.Formula
MsgBox("dateBefore = " & dateBefore)
MsgBox("dateSepTest = " & dateSepTest)
MsgBox("dateAfter = " & dateAfter)
End With
 
P

Patrick Molloy

not sure what the question is but look at this

Option Explicit
Sub demo()
Dim d1 As Date
Dim d2 As Date
d1 = Range("C3").Value
d2 = Range("C4").Value
MsgBox "First date: " & d1
MsgBox "Next date: " & d2
End Sub

in your code you have
dateBefore = .Range(.Cells(5, 1)).Value
this is INDIRECTION
so .Cells(5, 1) (cell "A5") must be a valid cell reference like "B4" or a
named range, and "B4" would be the date
if thats NOT what you meant, then it ought perhaps be this:
dateBefore = .Cells(5, 1).Value


I'm trying to write a macro in Excel 2003. In this macro, I want to
use the date values of three different dates, but I'm not able to make
it work. The code below shows three different methods I've tried, but
they all just return 0. I have also tried using Double or Date instead
of Integer, but it turns out the same. When debugging this code, it
looks like the second range selection doesn't work. Any suggestions?

With ActiveSheet
xxx> .Range(.Cells(5, 9), Cells(7, 9)).Select ''xxx
xxx> '... 'xxx
Dim dateBefore As Integer
Dim dateSepTest As Integer
Dim dateAfter As Integer
dateBefore = .Range(.Cells(5, 1)).Value
dateBefore = .Range(.Cells(5, 1)).Value
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top