简体   繁体   中英

VBA error 9 can't find the solution

I have read plenty of articles here on runtime error 9 but didn't manage to understand how it applied to my case. I'm also very new to VBA so apologies if this has to do with the basics...

Anyways, I'm trying to copy range("A39:D39") to the next available row in one of three tables in another sheet. Which table (not specifically formatted as a table by the way) depends on the input that the user has given in the Listbox.

Here is my code:

Sub ListBoxValue_Method3()

    Worksheets("Calculations").Activate

     Dim lbValue As Integer

    lbValue = Worksheets("Calculations").ListBoxes("List Box 8").Value

    Worksheets("Calculations").Range("A39:D39").Copy


Select Case lbValue

    Case 1

    Worksheets("Dashboard").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues

    Case 2

    Worksheets("Dashboard").Range("H" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues   %%%%%ERROR HERE%%%%%

    Case 3

    Worksheets("Dashboard").Range("N" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues

    Case Else

    MsgBox ("Check input")

End Select


End Sub

The error is:

Run-time error '9':

Subscript out of range

I'm using Excel 2016 (365). Thanks in advance to anyone who is willing to take the time to have a look.

you seem to use MAC, which I don't.

but you could give this code a try

Sub ListBoxValue_Method3()
    Dim lbValue As Integer

    With Worksheets("Calculations")
        lbValue = .OLEObjects("List Box 8").Object.value

        With .Range("A39:D39")
            Select Case lbValue
                Case 1
                    Worksheets("Dashboard").Range("B" & Rows.Count).End(xlUp).offset(1, 0).Resize(, .Columns.Count).value = .value
                Case 2
                    Worksheets("Dashboard").Range("H" & Rows.Count).End(xlUp).offset(1, 0).Resize(, .Columns.Count).value = .value
                Case 3
                    Worksheets("Dashboard").Range("N" & Rows.Count).End(xlUp).offset(1, 0).Resize(, .Columns.Count).value = .value
                Case Else
                    MsgBox ("Check input")
            End Select
        End With
    End With
End Sub

where I assumed your "List Box 8" listbox is an "ActiveX" one

Always make sure the name of the sheet is correct -.-'

Code was fine, just an inexistent Worksheet.

How missing the simplest things can take ages to realise!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM