简体   繁体   English

如何基于Excel工作表中的列号创建变量

[英]How to create variable based on no of column in an excel sheet

Below is the code to get no of last used column 下面的代码获取没有最后使用的列

Dim lastColumn As Integer

 lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

which return suppose 6 .My use case is based on return value i want to create that no of long variable in vba. 该返回假定6。我的用例基于返回值,我想在vba中创建long变量。 How to do that please help 怎么做请帮忙

You should be defining 1 array variable of long data type. 您应该定义1个长数据类型的数组变量。

For eg. 例如。

Sub test()

' Get last column number
Dim lastColumn As Integer
lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

' Create a long data type array with limit equal last column number
Dim arrColVars(lastColumn) as Long

' Perform operations or assignment on variables
' arrColVars(0) = 123
' arrColVars(1) = 456
' ...
' arrColVars(n) = 789

End Sub

Note: 注意:

  • All array indexes declared but not defined will default to a value of zero (0). 所有声明但未定义的数组索引将默认为零(0)。
  • If you refer to an array index beyond the array limit specified, you will get an error 'Subscript out of range'. 如果引用的数组索引超出指定的数组限制,则会收到错误“下标超出范围”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM