计数和复制动态范围Vba

问题描述 投票:0回答:1

我试图根据单元格内的值过滤A列中的一个范围,例如只有590到690,其中包含一个值的范围,首先是为了计算动态范围的行数,其次是为了将动态范围中的值复制到另一列中.我有困难,我认为第一个msgbox范围,我不知道为什么我已经尝试了多种变化的范围.

column values

子巴卡()

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
excel vba
1个回答
0
投票

我设法解决了这个问题,看来是单元格变量引起的问题,因为我认为,我把它们从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
© www.soinside.com 2019 - 2024. All rights reserved.