如何让使用访问VBA从列名擅长列号 [英] how to get excel column number from column name using access vba

查看:305
本文介绍了如何让使用访问VBA从列名擅长列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列名的Excel的列数(指数)。

I'm trying to get the excel column number (index) from the column name.

Dim ExcelApp As New Excel.Application
Dim ExcelBook As New Excel.Workbook
Dim ColName As string 

ColName = "sample name"
Debug.Print ExcelBook.Worksheets("Sheet1").Range(ColName & 1).Column

但是,这code未打印的列数。 我反而得到运行时错误1004:应用程序定义或对象定义的错误。 我要如何从列名返回Excel的列数?

But this code is not printing column number. I instead get runtime error 1004: Application-defined or object-defined error. How do i get return the excel column number from the column name?

推荐答案

请看到的 MSDN文档Range对象和的列属性

我相信你是混淆了头的名称在工作表中的列的地址。

I believe you are confusing the "Header" name in your worksheet with the column's address.

在code以下将返回列索引。

The code below will return the column index.

Debug.Print ThisWorkbook.Worksheets("Sheet1").Range("A1").Column 'returns 1
Debug.Print ThisWorkbook.Worksheets("Sheet1").Range("B:B").Column 'returns 2
' to do this by row & col index, use the cells object
Debug.Print Thisworkbook.worksheets(1).Cells(1,5).Column 'returns 5

这篇关于如何让使用访问VBA从列名擅长列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆