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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4413502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:24:50  来源:igfitidea点击:

Get last cell with data in column in excel 2007

excelexcel-vbaexcel-2007vba

提问by Patrick

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

我正在尝试编写一个宏来在 excel 2007 中创建图形。我不知道一系列数据之一的范围内的单元格数量(它可能是 50 - 1000 之间的任何地方)。我用谷歌搜索了这个,我找到了答案,但它们遍布地图,我尝试过的少数几个根本没有帮助我。

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.

任何帮助是极大的赞赏。

回答by Alex P

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

回答by Ashwith Ullal

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