Excel VBA: Can we refer a column by its name? -
i want refer column header name.
currently column 4th 1 , header name "preference". , column consists of "yes" or "no"
5th column header "reason" , filled when "preference" column "no"
my code is
private sub commandbutton1_click() dim integer dim myworksheetlastrow byte dim myworksheetlastcolumn byte myworksheetlastrow = worksheets(1).cells(rows.count, "a").end(xlup).row myworksheetlastcolumn = worksheets(1).cells(1, columns.count).end(xltoleft).column worksheets(1).cells(1, myworksheetlastcolumn + 1).value = "result" = 2 myworksheetlastrow if cells(i, 4).value = "yes" cells(i, myworksheetlastcolumn + 1).value = cells(i, 4).value else: cells(i, myworksheetlastcolumn + 1).value = cells(i, 5).value end if next end sub
what want instead of cells(i,4) , want call column header example: cells(i,"preference"). because won't column number of "preference" in prior. , use excel vba because have deal 20-30 similar files.
further comments, if want direct, have this:
cells(i,application.worksheetfunction.match("preference", range("1:1"), 0)).
Comments
Post a Comment