PDA

View Full Version : Solved: VBA ranges and "for" loop



udigold1
12-23-2008, 08:15 AM
Hi, I have this problem, I want to copy from one range to another range only cells that meet some conditions. So far so good, trouble is that I use a "for" loop and I get gap in rows in the target range because I use the same counter for the two ranges.
here's the code I use :

Range(ret.Offset(1, 0), ret.End(xlDown)).Select
For i = 1 To Selection.Rows.Count
If ret.Offset(i, 0).Value > 1 And AccName.Offset(i, 0).Font.Bold = False Then
dhr.Offset(i, 0) = dat.Offset(i, 0)
ahr.Offset(i, 0) = act.Offset(i, 0)
nhr.Offset(i, 0) = niar.Offset(i, 0)
khr.Offset(i, 0) = AccName.Offset(i, 0)
End If
Next i
I don't want to have gaps in the target range, but I can't figure out what to do.

nst1107
12-23-2008, 08:29 AM
Try adding another counter. Something like this:Dim j As Long
Range(ret.Offset(1, 0), ret.End(xlDown)).Select
j = 1
For i = 1 To Selection.Rows.Count
If ret.Offset(i, 0).Value > 1 And AccName.Offset(i, 0).Font.Bold = False Then
dhr.Offset(j, 0) = dat.Offset(i, 0)
ahr.Offset(j, 0) = act.Offset(i, 0)
nhr.Offset(j, 0) = niar.Offset(i, 0)
khr.Offset(j, 0) = AccName.Offset(i, 0)
j = j + 1
End If
Next i

udigold1
12-24-2008, 02:30 AM
Thanks,

It works...