【问题标题】:Counting rows and columns for named ranges in vba计算vba中命名范围的行和列
【发布时间】:2017-12-07 23:06:30
【问题描述】:

我正在尝试从 excel 中的命名范围计算 vba 中的行数和列数。然后在二维数组中使用行数和列数来执行其他计算。命名范围是“CF_Inputs”,它来自名为“Price_Volumes_Inputs”的工作表中的 A2:Z60。我似乎无法使用命名范围获得行和列计数,因此我使用下面的代码和偏移方法来计算行和列。有没有办法使用实际的命名范围来做到这一点?我是整个编程领域的新手,所以对于这个简单的问题,我深表歉意。

Dim Inputs As Variant    
Sheets("Price_Volumes_Input").Activate

Inputs = Range("A2", Range("Z2").End(xlDown).End(xlToRight)) 

【问题讨论】:

  • Worksheets("Price_Volumes_Inputs").Range("CF_Inputs").Rows.Count
  • 感谢您的反馈非常有帮助。我能够使用代码来修改行数和列数,还可以填充动态数组。 Sheets("Input").Activate Dim Row_Count As Long Dim Column_Count As Long Row_Count = Worksheets("Input").Range("CF_Inputs").Rows.Count Column_Count = Worksheets("Input").Range("CF_Inputs") .Columns.Count Inputs = Worksheets("Input").Range("CF_Inputs", Range("CF_Inputs").End(xlDown).End(xlToRight))

标签: excel vba


【解决方案1】:

欢迎来到 VBA 编程世界。如果您要进行 Excel VBA 编程,那么您需要在 Chip Pearson 的网站上添加书签。我发现它非常有用。链接是http://www.cpearson.com/Excel/MainPage.aspx

有很多方法可以做到这一点。例如,一种简单的方法是

Dim dataRows As Long
Dim dataCols As Long
Dim workArray as Variant

With ActiveWorkbook.Sheets("Price_Volumes_Input").Range("CF_Inputs")
    dataRows = .Rows.Count
    dataCols = .Columns.Count
End With

Dim workArray() As Variant
Redim workArray(dataRows, dataCols) as Variant
Dim i as Long, j as Long
For i = 0 to dataRows - 1
    For j = 0 to dataCols - 1
        'do the calculations
    Next j
Next i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多