Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 组特定范围_Vba_Excel - Fatal编程技术网

Vba 组特定范围

Vba 组特定范围,vba,excel,Vba,Excel,我试图创建一个宏来选择和分组一定数量的单元格,下面是一个简单的示例图片。唯一应该确定范围的是列A 假设活动单元格位于第2行和第13行之间,宏应该能够发现第1行和第14行是该范围的边界,因为A列中存在值,因此创建和选择从第2行到第13行的范围。 我尝试循环遍历单元格,在列A中找到具有活动值的第一行,大致了解它应该如何工作,但仍在努力创建工作代码:) 工作代码: Sub Group() Dim Rng As Range Dim Rstart As Long, Rend As Long Dim R

我试图创建一个宏来选择和分组一定数量的单元格,下面是一个简单的示例图片。唯一应该确定范围的是
列A

假设活动单元格位于
第2行和第13行之间,宏应该能够发现
第1行和第14行是该范围的边界,因为A列中存在值,因此创建和选择从
第2行到第13行的范围。
我尝试循环遍历单元格,在
列A
中找到具有活动值的第一行,大致了解它应该如何工作,但仍在努力创建工作代码:)

工作代码:

Sub Group()


Dim Rng As Range
Dim Rstart As Long, Rend As Long
Dim R As Long

R = ActiveCell.Row
If Len(Cells(R, "A").Value) Then R = R + 1
Rstart = R
Do Until Len(Cells(Rstart - 1, "A").Value)
    Rstart = Rstart - 1
Loop
Rend = R
R = Cells(Rows.Count, "D").End(xlUp).Row
Do Until Len(Cells(Rend + 1, "A").Value)
    Rend = Rend + 1
    If Rend = R Then Exit Do
Loop

Set Rng = Range(Cells(Rstart, "H"), Cells(Rend, "H"))
资料来源:


更新了上面包含的代码。我发布了一个新问题,其中包含了一些额外的实现和附加内容,因此这个带有活动标题的问题不再真正“相关”。
Sub Group()


Dim Rng As Range
Dim Rstart As Long, Rend As Long
Dim R As Long

R = ActiveCell.Row
If Len(Cells(R, "A").Value) Then R = R + 1
Rstart = R
Do Until Len(Cells(Rstart - 1, "A").Value)
    Rstart = Rstart - 1
Loop
Rend = R
R = Cells(Rows.Count, "D").End(xlUp).Row
Do Until Len(Cells(Rend + 1, "A").Value)
    Rend = Rend + 1
    If Rend = R Then Exit Do
Loop

Set Rng = Range(Cells(Rstart, "H"), Cells(Rend, "H"))