错误9:下标超出范围

时间:2013-10-19 15:15:48

标签: excel vba excel-vba 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

Debug.Print效果很好,所以唯一的问题是在Range(“B1:B”和&n; n_users).Value。

3 个答案:

答案 0 :(得分:1)

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

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

你必须在工作表引用周围使用quatations。

答案 1 :(得分:0)

首先,除非您在实际代码中的某处定义了Aux,否则这将无效。工作表名称引用必须是字符串值,而不是空变量(ARich在其答案中解释)。

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

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

我认为你也可以使用命名范围。 This link 解释了一下。

答案 2 :(得分:-1)

我无法看到你如何在该行上获得错误9。正如其他人反复指出的那样,如果变量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