【问题标题】:excel 2010 vba loop through declared variabels syntax results in Compile error Variable not definedexcel 2010 vba循环通过声明的变量语法导致编译错误变量未定义
【发布时间】:2013-01-01 03:46:00
【问题描述】:

我声明了5个整数

    Public Hol_1 as integer
    Public Hol_2 as integer
    Public Hol_3 as integer
    Public Hol_4 as integer
    Public Hol_5 as integer

假设我有 3 个客户,那么我将使用 Hol_1 到 Hol_3。这也意味着:iClients = 3 我需要做的第一件事是查看我的工作表(“假期”),以确定我的 3 个客户每个人有多少假期。

    Sub CountHolidays()

    Dim i as integer
    Dim iclients as integer
    iclients = 3
    For i=1 to iclients 
        Hol_i = WorksheetFunction.CountA(ActiveWorkbook.Sheets("Holidays").Range(Cells(2, 3 + i), Cells(50, 3 + i))) 
       'The worksheetfunction calculates the amount of Holiday-dates I have for each of my three clients
    Next i
    End sub

我收到一个编译错误,提示未定义变量:未定义 Hol_i。我尝试了 "Hol_" & i 和其他人,但无法纠正这个问题。有人有想法吗?谢了

【问题讨论】:

    标签: excel vba variables loops syntax-error


    【解决方案1】:

    您不能连接变量名。 Hol_i 是一个完全独立于 Hol_1 的变量,即使 i=1 也是如此。

    您需要一个数组来执行此操作:

    Dim Hol(5) as Integer
    
    For i=1 to iclients 
      Hol(i) = WorksheetFunction.CountA(ActiveWorkbook.Sheets("Holidays").Range(Cells(2, 3 + i), Cells(50, 3 + i)))
    Next i
    

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2019-12-04
      • 1970-01-01
      相关资源
      最近更新 更多