简体   繁体   中英

Count and Copy Dynamic Range Vba

I am trying to filter a range in column A based on the values within the cells eg only 590 to 690 which contains a range of values firstly in order to count the number of rows for the dynamic range and secondly to copy the values in the dynamic range to another column.I am having difficulties I think with the first msgbox range and I do not know why I have tried multiples variations of the range.

列值

Sub barca()

Dim a As Variant
Dim b As Variant
Dim ws As Worksheets

Set ws = Worksheets("Rec")

LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row


For a = 4 To LastRow
For b = 4 To LastRow


If ws.Cells(a, 1).Value = "590" And ws.Cells(b, 1).Value = "690" Then


MsgBox ws.Range(Cells(a.Value, 1), (Cells(b.Value, 1))).Rows.Count


If ActiveSheet.Range("h4:h14").Rows.Count = ws.Range(Cells(a.Value, 1), (Cells(b.Value, 1))).Rows.Count Then

Range("a4:a15").Offset(0, 2).Copy
Range("g4:g15").Select
ActiveSheet.Paste

Else

MsgBox "no"


End If
End If



Next b
Next a


End Sub

I manged to solve the problem it seems that it was the cells variable that were causing the problem as I thought and i changed them from a and b to atr and btr.

Sub barca()

Dim atr As Variant
Dim btr As Variant
Dim ws As Worksheet
Dim s As Range
Dim t As Long


Set ws = Worksheets("Rec")

LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row


For atr = 4 To LastRow
For btr = 4 To LastRow


If ws.Cells(atr, 1).Value = "590" And ws.Cells(btr, 1).Value = "690" Then

Set s = ws.Range(Cells(atr, 1), (Cells(btr, 1)))

t = s.Rows.Count

If ActiveSheet.Range("h4:h14").Rows.Count = t Then

s.Offset(0, 2).Copy
Range("g4:g15").Select
ActiveSheet.Paste

Else

MsgBox "no"


End If
End If



Next btr
Next atr

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM