View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
davegb davegb is offline
external usenet poster
 
Posts: 573
Default Next not incrementing

I have a spreadsheet that looks like this:

Sheet A
Column A Column B
begin end
2/16/2005 3/15/2005

ID Price


Sheet B
ColumnA ColumnB ColumnC
ID Date Price
C2134 1/15/2005 29.99
F2156 1/30/2005 14.99
R5432 2/14/2005 12.98
M4621 1/22/2005 13.49
U4832 3/14/2005 6.98
R5563 2/17/2005 11.99

I'm working on a macro that searches through the dates in sheet B. When
it finds a value between the dates in Sheet A, cells A1 & B1, it
extracts the ID and cost from Sheet B, col A and Col C and pastes those
values into the next available row in Sheet A under ID and price (Col A
& B), starting in row 5 (first blank row below ID and Price).
I've tried several things to get it to increment from the first cell in
Col B in Sheet B, but it never moves to B3, just stays on B2 and loops
endlessly.
In Sheet B, cells B2 to B7 are the named range "Date". I previously
tried to using just the range "B:B" (instead of a named range), but the
program wouldn't do the "next" then either.
Here is the code:

Sub ExtractByDate()

Dim dtBeginDate As Date, dtEndDate As Date
Dim strID As String
Dim sCost As Single
Dim rCurCell As Range
Dim strCurID As String
Dim curCost As Currency
Dim iCurRow As Integer
Dim bFlag As Boolean

dtBeginDate = Worksheets("A").Range("A2").Value
dtEndDate = Worksheets("A").Range("B2").Value
iCurRow = 5
Worksheets("B").Range("b2").Select
bFlag = False

Do Until ActiveCell.Value = ""
For Each rCurCell In Range("Date")
If ActiveCell.Value dtBeginDate _
And ActiveCell.Value < dtEndDate Then
strCurID = ActiveCell.Offset(-1, 0).Value
curCost = ActiveCell.Offset(1, 0).Value
Worksheets("A").Range(iCurRow, 1).Value = strCurID
Worksheets("A").Range(iCurRow, 2).Value = curCost
bFlag = True

Else

End If
If bFlag = True Then iCurRow = iCurRow + 1
Next
Loop
If bFlag = False Then MsgBox "No dates within Range", vbOKOnly

End Sub

I know the code is crude, I'm just learning VBA. I'm guessing it looks
like code from many years ago, which is when I did most of my
programming.
The main problem is that the "For each, Next" loop isn't "nexting". It
just stays on cell B2 and loops back around. How do I get it to move to
the next cell in the range?
I'd like to get this code working, so I can understand how more about
the basics of VBA. Then, I'd like suggestions on how to properly write
this program using VBA's full functionality. One of the things that I
find daunting about VBA is that it seems that everything has several
ways of doing the same thing. Too many choices is always a problem for
me. I'm easily confused!
I'd also like to know if there's a way to interupt and endless loop in
VBA, other than going to the Task Manager and terminating the program.
And yes, I know if you write good code, you shouldn't get endless
loops. But that's a ways away for me.
Can anyone help me with this?