Excel VBA - 使用列标题动态选择列

时间:2017-03-01 14:05:36

标签: excel vba excel-vba

我正在尝试根据VBA中的列标题动态选择列。这是我正在使用的代码,

Sub trial()

Dim colm As Long
Dim nwb As Workbook, wb As Workbook
Dim nwk As Worksheet, wk As Worksheet, wk1 As Worksheet

co1m = WorksheetFunction.Match("Header", Sheets("Sheet1").Rows(1), 0)

Columns(colm).Select

End Sub

当我尝试调试它时,它在colm中显示5的值,同时在下一行列(colm).select上显示,它在colm的位置显示0。请帮助我解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:1)

你那里有拼写错误!您有co1mcolm

使用Option Explicit会抛出编译错误,说明co1m没有声明! ;)

Option Explicit

Sub trial()

Dim colm As Long
Dim nwb As Workbook, wb As Workbook
Dim nwk As Worksheet, wk As Worksheet, wk1 As Worksheet

colm = WorksheetFunction.Match("Header", Sheets("Sheet1").Rows(1), 0)

Columns(colm).Select

End Sub