简体   繁体   English

如果声明使用列标题名称而不是列号?

[英]If statement to use column header name instead of column number?

In my VBA code in Excel I wrote a bunch of stuff like: 在Excel中的VBA代码中,我写了很多东西,例如:

If Worksheets("Original Data").Cells(i, 7) <> "" Then 
    'do stuff.

The i is the iterator for the row number the loop is on. i是循环所在行号的迭代器。 The 7 was* the column number for a specific column, but now in a new "original data" sheet I received this column is in a different place. 7是*特定列的列号,但是现在在新的“原始数据”表中,我收到的该列位于其他位置。 Is there a way to replace the 7 with the column header name instead so it doesn't break every time the columns change position? 有没有一种方法可以将7替换为列标题名称,以使它不会在列每次更改位置时都中断?

Use find method to search for the Column header and get the column number 使用find方法搜索Column标头并获取列号

Dim ColumnNumber as long

ColumnNumber =  Worksheets("Original Data").cells.Find("Column Name").column
Worksheets("Original Data").Cells(i, ColumnNumber ) <> "" 

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

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