简体   繁体   中英

If statement to use column header name instead of column number?

In my VBA code in Excel I wrote a bunch of stuff like:

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

The i is the iterator for the row number the loop is on. 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. 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?

Use find method to search for the Column header and get the column number

Dim ColumnNumber as long

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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