如何在VBA中动态更改图形的选择范围

时间:2019-08-02 07:42:42

标签: excel vba

这可能是一个愚蠢的问题,但是我直到最近才开始使用VBA。我想画一个图。我的问题是选择范围。目前,我有29行,但是这种情况会不断变化。我将始终有两列,但数据集的行将不是恒定的。如何更改范围选择以根据单元格值进行更新?我可能可以使用count来计算行数,但是我不确定如何根据单元格值指定范围

ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("I1:J29")

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法:

Option Explicit

Sub test()
    Dim LastRowI As Long, LastRowJ As Long, LastRowMax As Long

    'Change sheet name if needed
    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last of row of column I.
        LastRowI = .Cells(.Rows.Count, "I").End(xlUp).Row
        'Find the last of row of column J.
        LastRowJ = .Cells(.Rows.Count, "J").End(xlUp).Row
        'Find the maximun of LastRowI & LastRowJ in case you missing values
        LastRowMax = Application.Max(LastRowI, LastRowJ)
        'I used .Select to mark the range in order to ensure that you use the correct range
        .Range("I1:J" & LastRowMax).Select            
    End With  
End Sub

答案 1 :(得分:0)

是的,您可以计算行数并将该数字分配为变量。

   SQLPLUS    -s    $        -- false
   sqlplus    -s    $        -- false