简体   繁体   English

如何在Excel中的表格上方获取单元格的值

[英]How to get value of a cell above a table in Excel

I have a function that loops through all my sheets. 我有一个遍历我所有工作表的函数。 Each loop has a loop inside that loops through all tables within the sheet. 每个循环内部都有一个循环,该循环遍历工作表中的所有表。

I cannot figure out how to get the value of a cell that is directly above the table column headers. 我无法弄清楚如何获取表列标题正上方的单元格的值。

见图片

For Each sh In ActiveWorkbook.Worksheets
    If ActiveSheet.Name <> sh.Name And Not sh.Name = "Template" Then
        For Each tbl In sh.ListObjects
            I need those values here
        Next tbl
    End If
Next sh

You answered your own question (almost): 您(几乎)回答了自己的问题:

Option Explicit

Public Sub tblTitle()

    Dim sh As Worksheet, tbl As ListObject

    For Each sh In ActiveWorkbook.Worksheets
        If ActiveSheet.Name <> sh.Name And Not sh.Name = "Template" Then
            For Each tbl In sh.ListObjects
                With tbl.Range(1, 1)
                    If .Row > 1 Then MsgBox .Offset(-1, 0)
                End With
            Next tbl
        End If
    Next sh
End Sub

For the tables in the image bellow it will show 2 messages: "A" and "B" 对于下面的表格,它将显示2条消息:“ A”和“ B” 在此处输入图片说明

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

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