简体   繁体   English

Excel VBA-选择例程

[英]Excel VBA - Selection Routine

SO I have a table like this: 所以我有一个这样的表:

Assume Tidal Time is column A, Tidal height column B 假设潮汐时间为A栏,潮汐高度为B列

Tidal Time  Tidal Height
00:00:00    
01:00:00    
02:00:00    
03:00:00    
04:00:00    4.5
05:00:00    
06:00:00    
07:00:00    
08:00:00    
09:00:00    
10:00:00    2.1
11:00:00    
12:00:00    
13:00:00    
14:00:00    
15:00:00    
16:00:00    4.5
17:00:00    
18:00:00    
19:00:00    
20:00:00    
21:00:00    
22:00:00    1.9
23:00:00    

What I need is a selection routine in excel-VBA like this: 我需要的是excel-VBA中选择例程,如下所示:
Find the last non empty value (In this case 1.9) 查找最后一个非空值(在本例中为1.9)
Select last empty Value. 选择最后一个空值。
Select each cell up till the next non empty value. 选择每个单元格直到下一个非空值。 (In this case 4.5) (在这种情况下为4.5)
Use the following code to trend: 使用以下代码进行趋势分析:
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _ Trend:=True
Select that cell again (4.5) 再次选择该单元格(4.5)
Select each cell up till the next non empty value (In this case 2.1) 选择每个单元格直到下一个非空值(在本例中为2.1)
Trend. 趋势。
Select that cell it finished on (2.1) 选择完成的单元格(2.1)
Select each cell up till the next non empty value(In this case 4.5 at the top of the table) 选择每个单元格直到下一个非空值(在这种情况下,表格顶部为4.5)
Trend. 趋势。

That's pretty much all that needs to happen. 这几乎是所有需要发生的事情。 Can any shed some light on the process for this? 任何人都可以阐明这一过程吗? I keep getting confused. 我一直感到困惑。 It doesn't help that there are many different ways to do it either. 同样有很多不同的方法也无济于事。 Thanks in advance! 提前致谢!

You can use the Range.End() method to find the next (or last) non-empty cell. 您可以使用Range.End()方法来查找下一个(或最后一个)非空单元格。 In your case you would work from the bottom up. 在您的情况下,您将自下而上地工作。 So for example, to find the last non-empty cell (1.9): 因此,例如,要找到最后一个非空单元格(1.9):

Set LastCell = Activesheet.Cells(ActiveSheet.Rows.Count,2).End(xlUp)

Do While LastCell.Row > 2

    If LastCell.Offset(-1,0) = "" then
        Set NonEmptyCellAboveLastCell = LastCell.End(xlUp)
    Else
        Set NonEmptyCellAboveLastCell = LastCell.Offset(-1,0)
    End If

    If NonEmptyCellAboveLastCell.Row > 1 Then
        Set RangeToFill = ActiveSheet.Range(NonEmptyCellAboveLastCell, LastCell)
        RangeToFill.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, Trend:=True

        If NonEmptyCellAboveLastCell.Offset(-1,0) = "" then
            Set LastCell = NonEmptyCellAboveLastCell.End(xlUp)
        Else
            Set LastCell = NonEmptyCellAboveLastCell.Offset(-1,0)
        End If

    Else
        Set LastCell = ActiveSheet.Range("B1")
    End If
Loop

Note that the End method will jump to the next non-empty cell or to the last non-empty cell in a contiguous range of non-empty cells, so you need to check whether the adjacent cell is empty or not. 请注意,End方法将跳转到连续的非空单元格范围内的下一个非空单元格或最后一个非空单元格,因此您需要检查相邻单元格是否为空。

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

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