Here's debug friendly version of the code. I have put in some messageboxes to help you narrow down where the disconnect is. Use the information that the messageboxes provide along with the new comments in the code to find the problem:
Private Sub InOutResults(ByVal strAction As String)
'This is the common subroutine
Dim ws As Worksheet
Dim rngFound As Range
Dim rngDates As Range
Dim rngSum As Range
Dim strFind As String
Dim lOffset As Long
Dim dStart As Double
Dim dEnd As Double
If Me.ComboBox1.ListIndex = -1 Then
Me.ComboBox1.SetFocus
MsgBox "Must select a material"
Exit Sub
End If
strFind = Me.ComboBox1.Text
dStart = CDbl(CDate(Me.Calendar1.Value))
dEnd = CDbl(CDate(Me.Calendar2.Value))
If dStart = 0 Or dEnd = 0 Then
MsgBox "Must select both a Start Date and End Date"
Exit Sub
End If
Set ws = Sheets("Stocks")
Set rngFound = ws.Columns("A").Find(strFind, , xlValues, xlPart)
If rngFound Is Nothing Then
MsgBox "There is no data for material [" & strFind & "]", , "Invalid Material"
Else
'check if end date is being recognized properly
MsgBox Format(dEnd, ws.Range("C5").NumberFormat)
Set rngDates = ws.Rows(5)
Select Case LCase(strAction)
Case "in": lOffset = 1
Case "out": lOffset = 2
End Select
Set rngSum = ws.Rows(rngFound.Row + lOffset)
Me.TextBox1.Text = Evaluate("SumIf(" & rngDates.Address(External:=True) & ","">=" & dStart & """," & rngSum.Address(External:=True) & ")-SumIf(" & rngDates.Address(External:=True) & ","">" & dEnd & """," & rngSum.Address(External:=True) & ")")
Set rngFound = rngDates.Find(Format(dEnd, ws.Range("C5").NumberFormat), , xlValues, xlWhole)
'This if/then statement has been expanded for further debugging
If Not rngFound Is Nothing Then
'Check if column and row are where they should be
MsgBox "Found the end date in column: " & Trim(Mid(Replace(rngFound.Address, "$", String(" ", 99)), 99, 99)) & Chr(10) & _
"Looking for Actual Stock values in row: " & rngSum.Row + 3 - lOffset
Me.TextBox3.Text = ws.Cells(rngSum.Row + 3 - lOffset, rngFound.Column).Text
Else
'Check if the date simply isn't being found at all
MsgBox "end date not found"
Me.TextBox3.Text = 0
End If
End If
Set ws = Nothing
Set rngFound = Nothing
Set rngDates = Nothing
Set rngSum = Nothing
End Sub
Bookmarks