在“工程”“部件”中添加“Microsoft Common Dialog Control 6.0”,在窗体中加入此控件及Command1,再写入以下代码:
Private Sub Command1_Click()
Dim xlApp
Dim xlBook
Dim xlSheet1
CommonDialog1.ShowOpen
If CommonDialog1.FileName = "" Then Exit Sub
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(CommonDialog1.FileName)
Set xlSheet1 = xlBook.sheets(1)
msgbox xlSheet1.UsedRange.column.Count
'弹出窗口说明在第几列
xlBook.Close savechanges:=True
xlApp.Quit
Set xlSheet1 = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
end sub