简体   繁体   English

在Excel 2007中获取列中包含数据的最后一个单元格

[英]Get last cell with data in column in excel 2007

I'm trying to write a macro to create graphs in excel 2007. I don't know the number of cells that will be in the range for one of the series of data (it could be anywhere from 50 - 1000). 我正在尝试编写一个宏来在excel 2007中创建图形。我不知道将在一系列数据范围内的单元格数量(它可能在50到1000之间)。 I've googled this and I've found answers but they are all over the map and the few I've tried haven't helped me at all. 我用谷歌搜索了这个,但我已经找到了答案,但是他们已经遍布地图了,我尝试过的几个根本没有帮助过我。

I'm a newb at vba macros but am an experienced programmer. 我是vba宏的新手,但是经验丰富的程序员。

I've found examples such as: 我发现了以下示例:

Sub FindLast2()
    x = ActiveSheet.UsedRange.Rows.Count
    ActiveCell.SpecialCells(xlLastCell).Select
End Sub

I'm not sure if this works & if it does work how would I incorporate that into my macro 我不确定这是否有效以及它是否有效如何将其合并到我的宏中

Here's my macro as it stands now: 这是我现在的宏观:

Sub temp_graph_5()
'
' temp_graph_5 Macro
'

'
    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(2).Select
    Sheets(2).Name = "Temperature"
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Sheets(1). _
        Range("B2:B324")
    ActiveChart.SeriesCollection(1).Name = "=""Temperature"""

End Sub

The 'B324' is the section that I need to be variable. 'B324'是我需要变量的部分。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

This code may help achieve what you need: 此代码可以帮助您实现所需的功能:

 Sub temp_graph_5()
    Dim myRng As Range
    Dim lastCell As Long

    //Get range to be plotted in chart
    lastCell = Worksheets(1).Range("B2").End(xlDown).Row
    Set myRng = Worksheets(1).Range("B2:B" & lastCell) 

    //Add worksheet and name as "Temperature"
    Dim newSheet As Worksheet

    Set newSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    newSheet.Name = "Temperature"
    newSheet.Select

    //Add a new chart in Temperature and plot values from sheet 1
    Charts.Add

    With ActiveChart
        .ChartType = xlLine
        .SetSourceData Source:=myRng, PlotBy:=xlColumns
        .Location Where:=xlLocationAsObject, Name:="Temperature"
    End With

End Sub
sub test()
last_row_all = Range("A65536").End(xlUp).Row
msgbox last_row
end sub

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

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