1

我声明了 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 和其他人,但无法纠正这个问题。有人有想法吗?谢谢

4

1 回答 1

4

您不能连接变量名称。 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
于 2013-01-17T15:06:21.633 回答