I have made some changes and now my code compiles, but no results,
please help.
Sub GetAmOrDep()
Dim PayRange As Range
Dim TestRange1, TestRange2, TestRange3 As Range
Dim TestDate As Date
Dim CurrentColumn As Column
Dim RowNumber As Integer
Range("M7").Select
Do While ActiveCell <> Range("M64")
Do While ActiveCell.Column <> Range("AD:AD")
RowNumber = ActiveCell.Row
TestRange1 = "F" & RowNumber
TestRange2 = "H" & RowNumber
TestRange3 = "I" & RowNumber
PayRange = "K" & RowNumber
TestDate = DateAdd("m", Range("TestRange1").Value,
Range("TestRange2").Value)
If TestDate < Range("TestRange3").Value Then
Range("PayRange").Select
Range("PayRange").Value.Copy
Selection.Copy
ActiveCell.Offset(0, 2).Select
If ActiveCell.Value = Empty Then
ActiveSheet.Paste
Else
Do Until IsEmpty(ActiveCell.Value)
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Loop
End If
ActiveCell.Offset(0, 1).Select
TestRange1 = TestRange1 - 1
Else:
ActiveCell.Offset(0, 2).Select
If IsEmpty(ActiveCell.Value) Then
ActiveCell.Value = 0
Else
Do Until IsEmpty(ActiveCell.Value)
ActiveCell.Offset(0, 1).Select
Loop
ActiveCell.Value = 0
TestRange1 = TestRange1 - 1
End If
End If
Loop
If CurrentColumn = "AD" Then
ActiveCell.Offset(1, 17).Select
ElseIf CurrentColumn = "AC" Then
ActiveCell.Offset(1, 16).Select
End If
Loop
End Sub
Thank you,
Loren
> Yep, you've got all your loops and dos but you're missing an End If
>
[quoted text clipped - 100 lines]
> > Loop
> > End Function
Trevor Shuttleworth - 30 Aug 2006 23:26 GMT
Loren
you really need to step through the code and see what it is trying to do.
Various things you should change firts:
'Dim CurrentColumn As Column doesn't seem to work for me - can't dim
something as column. That stops me straight away so I have remmed it out.
You could use:
If ActiveCell.Column = 30 Then
rather than:
If CurrentColumn = "AD" Then
Dim TestRange1, TestRange2, TestRange3 As Range only defines TestRange3 as a
Range; the other two are variants. When you try to use them you are
actually creating strings with addresses in them.
Hence you need to take out the quote marks in the following statements:
TestDate = DateAdd("m", Range(TestRange1).Value,
Range(TestRange2).Value)
If TestDate < Range(TestRange3).Value Then
Range(PayRange).Select
Range(PayRange).Value.Copy
This statement:
Do While ActiveCell.Column <> Range("AD:AD").Column
jumps over column AD because it starts at M7 and increments by 2 so you'll
never get 30 and stop this part of the loop.
This statement compares values/content:
Do While ActiveCell <> Range("M64")
Whilst this compares the addresses of the cells (which is what I think you
want ?)
Do While ActiveCell.Address <> Range("M64").Address
(Unless there is a value in M64 that you are looking for, in which case,
cancel the last remark)
You are moving across the columns but checking down the rows to terminate
your loop
ActiveCell.Offset(0, 2).Select is moving to the right, two
columns at a time
So, O7 and Q7 get set to zero ... in fact, every other x7 gets set to zero
till you fall off the side of the sheet. You'll get a Run time error '1004'
I have no idea what your data looks like so I can only guess at how the
comparisons are working out for you.
Short story, I can't get it to work but I've found a few bugs which won't
help.
As I said, if you step through the code you'll see what is happening
Amended code below:
Sub GetAmOrDep()
Dim PayRange
Dim TestRange1, TestRange2, TestRange3
Dim TestDate As Date
'Dim CurrentColumn As Column
Dim RowNumber As Integer
Range("M7").Select
Do While ActiveCell.Address <> Range("M64").Address
Do While ActiveCell.Column <> Range("AD:AD").Column
RowNumber = ActiveCell.Row
TestRange1 = "F" & RowNumber
TestRange2 = "H" & RowNumber
TestRange3 = "I" & RowNumber
PayRange = "K" & RowNumber
TestDate = DateAdd("m", Range(TestRange1).Value,
Range(TestRange2).Value)
If TestDate < Range(TestRange3).Value Then
Range(PayRange).Select
Range(PayRange).Value.Copy
Selection.Copy
ActiveCell.Offset(0, 2).Select
If ActiveCell.Value = Empty Then
ActiveSheet.Paste
Else
Do Until IsEmpty(ActiveCell.Value)
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Loop
End If
ActiveCell.Offset(0, 1).Select
TestRange1 = TestRange1 - 1
Else:
ActiveCell.Offset(0, 2).Select
If IsEmpty(ActiveCell.Value) Then
ActiveCell.Value = 0
Else
Do Until IsEmpty(ActiveCell.Value)
ActiveCell.Offset(0, 1).Select
Loop
ActiveCell.Value = 0
TestRange1 = TestRange1 - 1
End If
End If
Loop
If ActiveCell.Column = 30 Then
ActiveCell.Offset(1, 17).Select
ElseIf ActiveCell.Column = 29 Then
ActiveCell.Offset(1, 16).Select
End If
Loop
End Sub
>I have made some changes and now my code compiles, but no results,
> please help.
[quoted text clipped - 186 lines]
>> > Loop
>> > End Function
loren.pottinger - 01 Sep 2006 15:58 GMT
Thanks for taking the time to help Trevor. I'm going through the code
now.
> Loren
>
[quoted text clipped - 308 lines]
> >> > Loop
> >> > End Function