VBA参数不是可选的错误userfrom listbox

时间:2017-11-14 23:41:03

标签: excel vba runtime-error

我为UserForm编写了以下代码,但我一直收到argument not optional错误。

Option Explicit
Private Sub CommandButton3_Click()
   Select Case LCase(Me.ListBox2.Text)
   Case "Analysis": Call macro1
   Case "" 
        MsgBox "nothing selected"
   Case Else
        MsgBox Me.ListBox2.Text
   End Select
End Sub

宏1代码:

Sub macro1()
'
' Macro1 Macro
Sheets.Add
ActiveSheet.Name = "Analysis"
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'
Range("A13").Select
ActiveCell.FormulaR1C1 = "Year"
Range("B13").Select
ActiveCell.FormulaR1C1 = "0"
Range("C13").Select
ActiveCell.FormulaR1C1 = "=IFERROR(IF(RC[-1]+1>R9C2,"""",RC[-1]+1),"""")"
Range("C13").Select
Selection.AutoFill Destination:=Range("C13:HS13"), Type:=xlFillDefault 
Range("C13:HQ13").Select
End Sub

1 个答案:

答案 0 :(得分:1)

首先,引用ListBox的方法是个问题。请参阅下面的代码另外,我必须在Range之前添加Sheet Name,然后这对我有用...

Option Explicit

Private Sub CommandButton3_Click()
Dim i as Integer
Dim sValue as String
   For i = 0 To Me.ListBox2.ListCount - 1
       If Me.ListBox2.Selected(i) Then
           sValue = ListBox2.List(i)
       End If
   Next i

   Select Case LCase(sValue)
      Case "Analysis": Call macro1
      Case "" 
          MsgBox "nothing selected"
      Case Else
          MsgBox sValue.Text
   End Select
End Sub

Sub macro1()
'
' Macro1 Macro
Sheets.Add
ActiveSheet.Name = "Analysis"
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'
Sheets("Analysis").Range("A13").Select
ActiveCell.FormulaR1C1 = "Year"
Sheets("Analysis").Range("B13").Select
ActiveCell.FormulaR1C1 = "0"
Sheets("Analysis").Range("C13").Select
ActiveCell.FormulaR1C1 = "=IFERROR(IF(RC[-1]+1>R9C2,"""",RC[-1]+1),"""")"
Sheets("Analysis").Range("C13").Select
Selection.AutoFill Destination:=Sheets("Analysis").Range("C13:HS13"), Type:=xlFillDefault
Sheets("Analysis").Range("C13:HQ13").Select
End Sub