jbenfleming
New Member
- Joined
- Mar 30, 2017
- Messages
- 34
This is my first post on mrexcel, I have read the FAQ and posting guidelines, but I apologize if make some beginner mistakes.
I am writing a macro to add the value of a cell from one workbook, to the total of a cell on another workbook, based on whether two date values are equal. Here is my code:
It works fine for the first two days. Then it skips the next row before returning a value. Then it skips 2 rows before returning a value. Then it skips 3 rows, and this pattern continues on until it reaches the last day, which should be in a33 in this case. But because of the skipped rows pattern that last day ends up in cell a468. I've used this type of ForLoop before and never had this issue. Can anyone see where I am going wrong?
FYI, The "yes" and "no" in the second IF statement are placeholders for a formula I will be writing in later.
I am writing a macro to add the value of a cell from one workbook, to the total of a cell on another workbook, based on whether two date values are equal. Here is my code:
Code:
Sub ainvestment_record()
Dim rownum As Double, totdays As Double, day As Double, settledate As Double, totrows As Double
'determine number of days
settledate = Range("e2").Value
Workbooks.Open ("filepathhere")
Sheets("MAR17").Range("a3").Select
If Range("a3").Value = "" Then
MsgBox ("No Data Exists"): Exit Sub
ElseIf Range("a3").Value <> "" And Range("a4").Value = "" Then
totdays = 1
ElseIf Range("a3").Value <> "" And Range("a4").Value <> "" Then
Selection.End(xlDown).Select: totrows = ActiveCell.Row
totdays = totrows - 3
End If
Sheets("MAR17").Range("a3").Select
rownum = 3
For day = 1 To totdays
ActiveCell.Offset((rownum - 3), 0).Select
If ActiveCell.Value = settledate Then
ActiveCell.Offset(0, 3).Value = "yes"
Else
ActiveCell.Offset(0, 3).Value = "no"
End If
rownum = rownum + 1
Next day
End Sub
It works fine for the first two days. Then it skips the next row before returning a value. Then it skips 2 rows before returning a value. Then it skips 3 rows, and this pattern continues on until it reaches the last day, which should be in a33 in this case. But because of the skipped rows pattern that last day ends up in cell a468. I've used this type of ForLoop before and never had this issue. Can anyone see where I am going wrong?
FYI, The "yes" and "no" in the second IF statement are placeholders for a formula I will be writing in later.