错误9:下标超出范围。

时间:2022-01-20 00:39:17

I have a problem in excel Vba when I try to run this code, I have an error of subscript out of range:

我在excel Vba中遇到了一个问题,当我试图运行这个代码时,我有一个下标超出范围的错误:

Private Sub UserForm_Initialize()
  n_users = Worksheets(Aux).Range("C1").Value

  Debug.Print Worksheets(Aux).Range("B1:B" & n_users).Value

  ListBox1.RowSource = Worksheets(Aux).Range("B1:B" & n_users).Value

  ComboBox1.RowSource = Worksheets(Aux).Range("B1:B" & n_users).Value
  ComboBox2.RowSource = Worksheets(Aux).Range("B1:B" & n_users).Value

End Sub

And Debug.Print works well, so the only problem is in Range("B1:B" & n_users).Value.

和调试。打印效果很好,所以唯一的问题是在范围内(“B1:B”& n_users). value。

3 个解决方案

#1


0  

Firstly, unless you have Aux defined somewhere in the actual code, this will not work. The sheet-name reference must be a string value, not an empty variable (which ARich explains in his answer).

首先,除非在实际代码中定义了Aux,否则这将不起作用。sheet-name引用必须是一个字符串值,而不是一个空变量(ARich在他的回答中解释了这一点)。

Second, the way in which you are trying to populate the rowsource value is incorrect. The rowsource property of a combobox is set using a string value that references the target range. By this I mean the same string value you would use in an excel formula to reference a cell in another sheet. For instance, if your worksheet is named "Aux" then this would be your code:

其次,您试图填充rowsource值的方法是不正确的。combobox的rowsource属性设置为引用目标范围的字符串值。我的意思是,在excel公式中使用相同的字符串值来引用另一个表单中的单元格。例如,如果您的工作表被命名为“Aux”,那么这将是您的代码:

ComboBox1.RowSource = "Aux!B1:B" & n_users

I think you can also use named ranges. This link explains it a little.

我认为你也可以使用命名范围。这个链接解释了一点。

#2


1  

If the name of your sheet is "Aux", change each Worksheets(Aux) reference to Worksheets("Aux"). Unless you make Aux a string variable, for example:

如果您的表的名称是“Aux”,请将每个工作表(Aux)引用更改为工作表(“Aux”)。除非你使Aux成为一个字符串变量,例如:

Dim Aux As String  
Aux = "YourWorksheetName"
n_users = Worksheets(Aux).Range(C1).Value

you must use quatations around sheet references.

您必须在页面引用周围使用quatations。

#3


-1  

I can't see how you can get an Error 9 on that line. As others have pointed out repeatedly, the place you'll get it is if the variable Aux doesn't have a string value representing the name of a worksheet. That aside, I'm afraid that there is a LOT wrong with that code. See the comments in the below revision of it, which as near as I can figure is what you're trying to get to:

我看不出你怎么能在这一行上出错。正如其他人反复指出的那样,如果变量Aux没有表示工作表名称的字符串值,就会得到它。除此之外,我担心这段代码有很多问题。看看下面修改的评论,我能想到的就是你想要达到的目的:

Private Sub UserForm_Initialize()

  'See below re this.
  aux = "Sheet2"

  'You should always use error handling.
  On Error GoTo ErrorHandler

  'As others have pointed out, THIS is where you'll get a
  'subscript out of range if you don't have "aux" defined previously.
  'I'm also not a fan of NOT using Option Explicit, which
  'would force you to declare exactly what n_users is.
  '(And if you DO have it declared elsewhere, I'm not a fan of using
  'public variables when module level ones will do, or module
  'level ones when local will do.)

  n_users = Worksheets(aux).Range("C1").Value

  'Now, I would assume that C1 contains a value giving the number of
  'rows in the range in column B. However this:

  '*****Debug.Print Worksheets(aux).Range("B1:B" & n_users).Value
   'will only work for the unique case where that value is 1.
   'Why? Because CELLS have values. Multi-cell ranges, as a whole,
   'do not have single values. So let's get rid of that.

  'Have you consulted the online Help (woeful though
  'it is in current versions) about what the RowSource property
  'actually accepts? It is a STRING, which should be the address
  'of the relevant range. So again, unless
  'Range("B1:B" & n_users) is a SINGLE CELL that contains such a string
  '(in which case there's no point having n_users as a variable)
  'this will fail as well when you get to it. Let's get rid of it.
  '****ListBox1.RowSource = Worksheets(aux).Range("B1:B" & n_users).Value

  'I presume that this is just playing around so we'll
  'ignore these for the moment.
  'ComboBox1.RowSource = Worksheets(aux).Range("B1:B" & n_users).Value
  'ComboBox2.RowSource = Worksheets(aux).Range("B1:B" & n_users).Value

  'This should get you what you want. I'm assigning to
  'variables just for clarity; you can skip that if you want.

    Dim l_UsersValue As Long
    Dim s_Address As String
    l_UsersValue = 0
    s_Address = ""

    'Try to get the n_users value and test for validity
    On Error Resume Next
    l_UsersValue = Worksheets(aux).Range("C1").Value
    On Error GoTo ErrorHandler

    l_UsersValue = CLng(l_UsersValue)

    If l_UsersValue < 1 Or l_UsersValue > Worksheets(aux).Rows.Count Then
        Err.Raise vbObjectError + 20000, , "User number range is outside acceptable boundaries. " _
        & "It must be from 1 to the number of rows on the sheet."
    End If

    'Returns the cell address
    s_Address = Worksheets(aux).Range("B1:B" & n_users).Address

    'Add the sheet name to qualify the range address
    s_Address = aux & "!" & s_Address

    'And now that we have a string representing the address, we can assign it.

    ListBox1.RowSource = s_Address

ExitPoint:

   Exit Sub

ErrorHandler:

MsgBox "Error: " & Err.Description

Resume ExitPoint

End Sub

#1


0  

Firstly, unless you have Aux defined somewhere in the actual code, this will not work. The sheet-name reference must be a string value, not an empty variable (which ARich explains in his answer).

首先,除非在实际代码中定义了Aux,否则这将不起作用。sheet-name引用必须是一个字符串值,而不是一个空变量(ARich在他的回答中解释了这一点)。

Second, the way in which you are trying to populate the rowsource value is incorrect. The rowsource property of a combobox is set using a string value that references the target range. By this I mean the same string value you would use in an excel formula to reference a cell in another sheet. For instance, if your worksheet is named "Aux" then this would be your code:

其次,您试图填充rowsource值的方法是不正确的。combobox的rowsource属性设置为引用目标范围的字符串值。我的意思是,在excel公式中使用相同的字符串值来引用另一个表单中的单元格。例如,如果您的工作表被命名为“Aux”,那么这将是您的代码:

ComboBox1.RowSource = "Aux!B1:B" & n_users

I think you can also use named ranges. This link explains it a little.

我认为你也可以使用命名范围。这个链接解释了一点。

#2


1  

If the name of your sheet is "Aux", change each Worksheets(Aux) reference to Worksheets("Aux"). Unless you make Aux a string variable, for example:

如果您的表的名称是“Aux”,请将每个工作表(Aux)引用更改为工作表(“Aux”)。除非你使Aux成为一个字符串变量,例如:

Dim Aux As String  
Aux = "YourWorksheetName"
n_users = Worksheets(Aux).Range(C1).Value

you must use quatations around sheet references.

您必须在页面引用周围使用quatations。

#3


-1  

I can't see how you can get an Error 9 on that line. As others have pointed out repeatedly, the place you'll get it is if the variable Aux doesn't have a string value representing the name of a worksheet. That aside, I'm afraid that there is a LOT wrong with that code. See the comments in the below revision of it, which as near as I can figure is what you're trying to get to:

我看不出你怎么能在这一行上出错。正如其他人反复指出的那样,如果变量Aux没有表示工作表名称的字符串值,就会得到它。除此之外,我担心这段代码有很多问题。看看下面修改的评论,我能想到的就是你想要达到的目的:

Private Sub UserForm_Initialize()

  'See below re this.
  aux = "Sheet2"

  'You should always use error handling.
  On Error GoTo ErrorHandler

  'As others have pointed out, THIS is where you'll get a
  'subscript out of range if you don't have "aux" defined previously.
  'I'm also not a fan of NOT using Option Explicit, which
  'would force you to declare exactly what n_users is.
  '(And if you DO have it declared elsewhere, I'm not a fan of using
  'public variables when module level ones will do, or module
  'level ones when local will do.)

  n_users = Worksheets(aux).Range("C1").Value

  'Now, I would assume that C1 contains a value giving the number of
  'rows in the range in column B. However this:

  '*****Debug.Print Worksheets(aux).Range("B1:B" & n_users).Value
   'will only work for the unique case where that value is 1.
   'Why? Because CELLS have values. Multi-cell ranges, as a whole,
   'do not have single values. So let's get rid of that.

  'Have you consulted the online Help (woeful though
  'it is in current versions) about what the RowSource property
  'actually accepts? It is a STRING, which should be the address
  'of the relevant range. So again, unless
  'Range("B1:B" & n_users) is a SINGLE CELL that contains such a string
  '(in which case there's no point having n_users as a variable)
  'this will fail as well when you get to it. Let's get rid of it.
  '****ListBox1.RowSource = Worksheets(aux).Range("B1:B" & n_users).Value

  'I presume that this is just playing around so we'll
  'ignore these for the moment.
  'ComboBox1.RowSource = Worksheets(aux).Range("B1:B" & n_users).Value
  'ComboBox2.RowSource = Worksheets(aux).Range("B1:B" & n_users).Value

  'This should get you what you want. I'm assigning to
  'variables just for clarity; you can skip that if you want.

    Dim l_UsersValue As Long
    Dim s_Address As String
    l_UsersValue = 0
    s_Address = ""

    'Try to get the n_users value and test for validity
    On Error Resume Next
    l_UsersValue = Worksheets(aux).Range("C1").Value
    On Error GoTo ErrorHandler

    l_UsersValue = CLng(l_UsersValue)

    If l_UsersValue < 1 Or l_UsersValue > Worksheets(aux).Rows.Count Then
        Err.Raise vbObjectError + 20000, , "User number range is outside acceptable boundaries. " _
        & "It must be from 1 to the number of rows on the sheet."
    End If

    'Returns the cell address
    s_Address = Worksheets(aux).Range("B1:B" & n_users).Address

    'Add the sheet name to qualify the range address
    s_Address = aux & "!" & s_Address

    'And now that we have a string representing the address, we can assign it.

    ListBox1.RowSource = s_Address

ExitPoint:

   Exit Sub

ErrorHandler:

MsgBox "Error: " & Err.Description

Resume ExitPoint

End Sub