check if dates are the same before adding to row

rodmc

Registered User.
Local time
Today, 16:28
Joined
Apr 15, 2010
Messages
514
IM doing a simple data entry form for an excel sheet. I have 3 text boxes. txtID, txtRefDate and txtSeenDate and Im using the following code to add data in a new row
Code:
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("RefData")


'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    
'check for a CHI number
If Trim(Me.txtCHI.Value) = "" Then
  Me.txtCHI.SetFocus
  MsgBox "Please enter a CHI number"
  Exit Sub
End If

'copy the data to the database
'use protect and unprotect lines,
'     with your password
'     if worksheet is protected
With ws
'  .Unprotect Password:="password"
  ws.Cells(iRow, 1).Value = Me.txtCHI.Value
  ws.Cells(iRow, 2).Value = Me.txtRefDte.Value
  ws.Cells(iRow, 3).Value = Me.txtSeen.Value
 '  ws.Protect Password:="password"
End With

'clear the data
Me.txtCHI.Value = ""
Me.txtRefDte.Value = ""
Me.txtSeen.Value = ""
Me.txtCHI.SetFocus

End Sub

What I want to do is add another data item which checks dates in both txtRefDte and txtSeen to see if they are the same, if true it returns a 1 and if false returns a 0 into a new column (iRow4)

What in access VBA is pretty simple, it would seem is not so simple in excel VBA

any help would be greatly appreciated
 
You do not say what you have tried

Try
If ws.cells(irow,2) = ws.cells(irow,3) then
Ws.cells(irow,4)=1
Else
Ws.cells(irow,4)=0
End if


BTW you do not need to say ws.cells inside a With ws just .cells

Brian
 
Or

Ws.cells(irow,4) = ABS(ws.cells(irow,2)=ws.cells(irow,3))


Brian
 
I was actually trying that same thing but with the object names rather then the cell refs in the excel form

If me.txtRefDte.Value = me.txtSeen.Value etc etc

doh!
 

Users who are viewing this thread

Back
Top Bottom