-2

当我尝试运行此代码时,我在 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_users).Value。

4

3 回答 3

1

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

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

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

于 2013-10-19T16:36:19.010 回答
0

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

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

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

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

于 2013-10-19T18:13:42.113 回答
-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
于 2013-10-19T20:09:56.733 回答