Excel VBA - 根据列标题查找最后一个非空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41134548/
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-12 11:42:18  来源:igfitidea点击:

Excel VBA - Finding Last Non-Empty Row Based on Column Header

excelvbaexcel-vba

提问by Jeeva

I am currently creating a reporting sheet for equipment for our company. This sheet will grab data from multiple Excel sheets and populate it based on our custom report template. So far I have managed to find on how to merge multiple sheets and find the column header. But now I'm stucked on how to find the last non empty value based on column header.

我目前正在为我们公司创建设备报告表。该工作表将从多个 Excel 工作表中获取数据,并根据我们的自定义报告模板进行填充。到目前为止,我已经设法找到如何合并多个工作表并找到列标题。但是现在我被困在如何根据列标题找到最后一个非空值。

enter image description here

在此处输入图片说明

Attached in the picture above is the sample data. As you can see, the last non empty cell in each row is the average of the row. What I am trying to do is to find the header column(eg SVC525) and take the last non empty value of the column, which is average.

上图中所附的是示例数据。如您所见,每行中的最后一个非空单元格是该行的平均值。我想要做的是找到标题列(例如 SVC525)并取列的最后一个非空值,即平均值。

回答by xtoybox

for the example given, column B, this should do the trick

对于给出的示例,B 列,这应该可以解决问题

Dim lastrow as Integer, val
lastrow = Range("B" & Rows.Count).End(xlUp).Row
val = range("B" & lastrow + 2).Value

to iterate through your rows(header) that is not empty is another story that you can easily search for.

遍历非空的行(标题)是另一个您可以轻松搜索的故事。

回答by user3598756

you may be after something like this

你可能在追求这样的事情

Dim svVal As String

svVal = "SVC525" '<--| set your header value to be searched for in row 1
With Worksheets("averages") '<--| change "Averages" tou your actual sheet name
    MsgBox .Cells(.Rows.count, .Range("A1", .Cells(1, .Columns.count).End(xlToLeft)).Find(what:=svVal, LookIn:=xlValues, lookat:=xlWhole).column).End(xlUp)
End With