userform listbox中有多个值 - 我的代码出了什么问题
问题描述:
我不知道如何用这种语言编程,所以我不得不依赖Google。从这里到那边拿点东西,我怀疑我有两个目前不适合的谜题。我越来越runtime error 9 subscript out of range
因为这行:userform listbox中有多个值 - 我的代码出了什么问题
ThisWorkbook.Sheets("Sheet4").Range("c2").End(xlDown).Select = myVar
这里是我的代码:
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
ThisWorkbook.Sheets("Sheet4").Range("c2").End(xlDown).Select = myVar
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
答
我想我明白你正在尝试做的:
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.range("A" & rows.count).End(xlUp).Row + 1
ws.Range("C" & iRow).value = myVar
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
编辑: 我有更改了如何计算iRow
,并删除了对工作表4的引用
这是否做你需要的?
答
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Skráningar")
'Taeki gögn í gagnagrunn
myVar = ""
For X = 0 To Me.taeki.ListCount - 1
If Me.taeki.Selected(X) Then
If myVar = "" Then
myVar = Me.taeki.List(X, 0)
Else
myVar = myVar & "," & Me.taeki.List(X, 0)
End If
End If
Next X
'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("C" & iRow).Value = myVar
'check for a Name number
If Trim(Me.timutbox.Value) = "" Then
Me.dagsbox.SetFocus
MsgBox "Vinsamlega skráðu hversu lengi tækið var í notkun"
Exit Sub
End If
Me.Hide
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.dagsbox.Value
ws.Cells(iRow, 2).Value = Me.timutbox.Value
ws.Cells(iRow, 5).Value = Me.sandbox.Value
ws.Cells(iRow, 6).Value = Me.vedurbox.Value
ws.Cells(iRow, 8).Value = Me.bilunbox.Value
ws.Cells(iRow, 7).Value = Me.athbox.Value
ws.Cells(iRow, 3).Value = Me.taeki.Value
ws.Cells(iRow, 4).Value = Me.svaedi.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
Me.dagsbox.Value = ""
Me.timutbox.Value = ""
Me.sandbox.Value = ""
Me.vedurbox.Value = ""
Me.bilunbox.Value = ""
Me.athbox.Value = ""
Me.taeki.Value = ""
Me.svaedi.Value = ""
Me.dagsbox.SetFocus
这是当前的代码。
你好Unnar。在你指定的行中,你试图选择一个单元格,然后将它等同于一个值。而是试试这个'ThisWorkbook.Sheets(“Sheet4”)。Range(“C2”).End(xlDown).value = myVar'但是确保'Sheet4'存在。 – Calico
使用此行: ThisWorkbook.Sheets(“Skráningar”)。Range(“C2”)。Value = myVar 我能够从列表框中获取值,但我需要它查看下一个空单元格行为下一个条目。我可以对这个问题做些什么? –
并且非常感谢Calico的帮助!对此,我真的非常感激! –