如何在Excel中获取最后一个单元格索引

时间:2017-04-06 11:16:25

标签: excel vba excel-vba

我正在为某些数据创建折线图

var socket = io.connect("your.domain:3000");

我的最后一个单元格是D352,此索引因不同的纸张而异。如何通过获取最后使用的单元格索引来更改代码以使其动态工作?

2 个答案:

答案 0 :(得分:2)

假设最后一个索引是由A列中最后一个非空单元格给出的

变化:

ActiveChart.SetSourceData Source:=Range("A2:D352")

为:

ActiveChart.SetSourceData Source:=Range("A2:D" & Cells(Rows.Count, 1).End(xlUp).Row)

答案 1 :(得分:0)

考虑一下。

Sub FindingLastRow()

'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")

'Ctrl + Shift + End
  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'Using UsedRange
  sht.UsedRange 'Refresh UsedRange
  LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row

'Using Table Range
  LastRow = sht.ListObjects("Table1").Range.Rows.Count

'Using Named Range
  LastRow = sht.Range("MyNamedRange").Rows.Count

'Ctrl + Shift + Down (Range should be first cell in data set)
  LastRow = sht.Range("A1").CurrentRegion.Rows.Count

End Sub

https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba