gpt4 book ai didi

vba - 组特定范围

转载 作者:行者123 更新时间:2023-12-04 20:55:36 26 4
gpt4 key购买 nike

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

假设事件单元格介于 row 2 and row 13 之间。 , 宏应该能查出row 1 and row 14是该范围的边界,因为 A 列中存在值,因此创建和选择从 row 2 till row 13 开始的范围.
我试图遍历向下的单元格以在 column A 中找到具有事件值的第一行,大致了解它应该如何工作,但努力创建工作代码:)

    Dim StartCell, EindCell As Range
Dim teller As Integer
Dim teller2 As Integer

teller = 0
Do While Selection.Offset(teller, 0).Value = ""
teller = teller - 1
Loop
Selection.Offset(teller, 0).Select
Set StartCell = ActiveCell

teller2 = 0
Do While Selection.Offset(teller, 0).Value = ""
teller = teller + 1
Loop
Selection.Offset(teller, 0).Select

Set EindCell = ActiveCell
Range(StartCell, EindCell).Select

enter image description here

最佳答案

工作代码:

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"))

资源:

Calculating sum and grouping of dynamic range

关于vba - 组特定范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48209656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com