简体   繁体   English

计数和复制动态范围 Vba

[英]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.我正在尝试根据单元格内的值过滤列 A 中的范围,例如仅 590 到 690,其中首先包含一系列值,以便计算动态范围的行数,然后复制动态中的值范围到另一列。我认为第一个 msgbox 范围有困难,我不知道为什么我尝试了该范围的多个变体。

列值

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.我设法解决了这个问题,似乎是单元格变量导致了我认为的问题,我将它们从 a 和 b 更改为 atr 和 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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