Visual Basic(Excel)上表格范围的大小
我是Visual studio for Excel的初学者。 我对桌子的大小有疑问 我使用以下代码:Visual Basic(Excel)上表格范围的大小,excel,vba,Excel,Vba,我是Visual studio for Excel的初学者。 我对桌子的大小有疑问 我使用以下代码: Sub Comparaison() Dim sheet1 As Worksheet Set sheet1 = Sheets("feuil1") Dim Tableau_dataBase () Tableau_dataBase = Range("B3:D8") MsgBox " Size of table" & Tableau_dataBase.Height // error MsgBox
Sub Comparaison()
Dim sheet1 As Worksheet
Set sheet1 = Sheets("feuil1")
Dim Tableau_dataBase ()
Tableau_dataBase = Range("B3:D8")
MsgBox " Size of table" & Tableau_dataBase.Height // error
MsgBox " Size of table" & Tableau_dataBase.Length // error
End Sub
Excel上的我的表格(仅字符串):
7数据1数据2
43数据1数据2
8数据1数据2
但我有一个错误
我怎么知道这张桌子的大小
谢谢
高度
和长度
不是范围
对象的属性,这可能是导致错误的原因
确保变量的类型正确,在这种情况下,Tableau\u数据库
应为范围
。在分配范围
对象时,确保始终使用设置
。创建范围
对象时,最好明确参考数据所在的工作表
Sub Comparaison()
'Declare Variables
Dim ws As Worksheet
Dim Tableau_dataBase As Range
'Set object references
Set ws = Sheets("feuil1")
Set Tableau_dataBase = ws.Range("B3:D8")
'Get Height(number of rows) and length(number of columns) of Tableau_dataBase
MsgBox " Height of table" & Tableau_dataBase.Rows.Count
MsgBox " Length of table" & Tableau_dataBase.Columns.Count
End Sub
注意:我已将
sheet1
变量重命名为ws
,因为sheet1
可能通过其代码名引用现有的工作表。将范围分配给数组时,将得到一个基于一的二维数组。用于确定数组大小的函数有UBound和LBound。每个参数都有两个参数:数组和维度(可选)
Sub Comparaison()
Dim sheet1 As Worksheet
Dim Tableau_dataBase()
Set sheet1 = Sheets("feuil1")
Tableau_dataBase = sheet1.Range("B3:D8")
MsgBox " Table Height: " & UBound(Tableau_dataBase, 1)
MsgBox " Table Width: " & UBound(Tableau_dataBase, 2)
End Sub
我测试了Rows.count和columns.count,我也有一个错误。=>量词不正确我认为这可能是由于缺少
Set
,因此我对我的答案进行了相应的编辑。无法为数组指定Tableau\u数据库所引用的对象/变量类型?我假设这是一个范围
或变量
,但如果您收到数组分配错误,我发现不太可能是这种情况。我修改了代码。它只是一个字符串范围,但我认为它只是一个方法。对不起,我只是VB的新手。