我正在尝试在EXCEL / VBA中创建一个用户窗体,允许您选择一系列数据,并根据选项卡索引将数据范围复制到特定工作表,一旦选择了命令按钮“预测”。
这是我到目前为止改变标签的方法:
Private Sub tbsMethod_Change()
Dim tabIndex As Integer
tabIndex = tbsMethod.SelectedItem.Index
Select Case tabIndex
Case 0
Label2.Visible = True
RefEdit1.Visible = True
Label_a.Visible = False
Label_b.Visible = False
Label_g.Visible = False
TextBox_a.Visible = False
TextBox_b.Visible = False
TextBox_g.Visible = False
Case 1
Label2.Visible = True
....
这是我认为问题所在的地方(commandbutton_click):
Sub ForecastButton_Click()
Dim tabIndex As Integer
Dim ws As Worksheet
tabIndex = tbsMethod.SelectedItem.Index
With ThisWorkbook
Select Case tabIndex
'copies to worksheet or "module"
Case 0
Application.GoTo ActiveWorkbook.Sheets("Sheet1").Range("B7")
Range(RefEdit1.Value).Copy Destination:=Sheet1.Range("B7:B26")
Case 1
Range(RefEdit1.Value).Copy Destination:=Sheet3.Range("B7:B26")
Application.GoTo ActiveWorkbook.Sheets("Sheet3").Range("B7")
Case 2
Application.GoTo ActiveWorkbook.Sheets("Sheet4").Range("B7")
.Cells(29, 2).Value = Me.TextBox_a.Value
Range(RefEdit1.Value).Copy Destination:=Sheet4.Range("B7:B26")
Case 3
Application.GoTo ActiveWorkbook.Sheets("Sheet5").Range("B7")
.Cells(29, 2).Value = Me.TextBox_a.Value
.Cells(30, 2).Value = Me.TextBox_b.Value
Range(RefEdit1.Value).Copy Destination:=Sheet5.Range("B7:B26")
Case 4
Application.GoTo ActiveWorkbook.Sheets("Sheet6").Range("B7")
.Cells(29, 2).Value = Me.TextBox_a.Value
.Cells(30, 2).Value = Me.TextBox_b.Value
.Cells(31, 2).Value = Me.TextBox_g.Value
Range(RefEdit1.Value).Copy Destination:=Sheet6.Range("B7:B26")
End Select
Unload dfMod
End With
Me.R.Value = ""
Private Sub cmdClose_Click()
Unload dfMod
End Sub
我收到错误消息“错误9下标超出范围”。 现在它说“找不到方法或数据成员”。 VBA / EXCEL相当新,所以任何帮助都会非常感激
谢谢, 莱恩
答案 0 :(得分:1)
您的代码中有
With thisworkbook
...
.cells(...
cells
不是workbook
对象的属性或方法,会返回错误