命名表中单元格的值

时间:2017-07-11 12:03:41

标签: excel excel-formula ms-office

如何使用可读公式(即通过名称而不是索引引用行/单元格的公式)获取指定表中特定单元格的值?

enter image description here

在上图中,我想显示表(Income, Feb)的单元格Financials的值。我可以使用什么语法?

2 个答案:

答案 0 :(得分:1)

正如我所提到的,你可以用用户定义的函数隐藏复杂性,例如

public class EmployerActivity extends AppCompatActivity {

    TextView username;

    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_employeractivity);

      Bundle extras= getIntent().getExtras();
      String name=extras.getString("name");
      String email=extras.getString("email");
      String profile_pic=extras.getString("profile_pic");
      username = (TextView) findViewById(R.id.username);
      username.setText(name);
      //Display Email and Profile Picture with other views.
      //Note profile_pic String is a URL/URI.
    }
}

(使用工作簿中的表名更新Public Function Financials(month As String, item As String) As String 'Object to represent the table Dim lo As ListObject: Set lo = Range("Table1").ListObject 'Integers to act as coordinates Dim x As Integer, y As Integer 'Find the column x = lo.HeaderRowRange.Find(month).Column - lo.HeaderRowRange.Column + 1 'Find the row y = lo.DataBodyRange.Columns(1).Find(item).Row - lo.HeaderRowRange.Row ' Return the value at the coordinates x,y Financials = lo.DataBodyRange(y, x).Value End Function ,或者您可以向该函数添加另一个参数)

然后,您将在工作簿单元格中调用此函数,例如此示例

Range("Table1").ListObject

答案 1 :(得分:0)

我很难相信Microsoft并未一直为客户提供解决方案!

根据@ maxhob17的提示,这是一种通用解决方案,可与 any 命名表一起使用:

Public Function TLookup(tablename As String, key As String, col As String)
    'From a table, find the corresponding value for a key (in the first column)

    'Object to represent the table
    Dim lo As ListObject: Set lo = range(tablename).ListObject

    'Integers to act as coordinates
    Dim x As Integer, y As Integer

    'Find the column
    x = lo.HeaderRowRange.Find(col).column - lo.HeaderRowRange.column + 1

    'Use the key to find the row
    y = lo.DataBodyRange.Columns(1).Find(key).Row - lo.HeaderRowRange.Row

    ' Return the value at the coordinates x,y
    TLookup = lo.DataBodyRange(y, x).Value

End Function
  

在VB窗口中,将以上代码插入模块中。记住要以xlsm格式保存Excel文件。

然后,您应该可以在表格中插入以下公式:

=TLookup("Financials", "Feb", "Profit")

这种技巧将改变我的生活;并可能会改变您的情况。这不可能简单吗?

I also documented this solution in github