2

I'm a relatively uninformed programmer, apologies.

Below code for an Excel 2010 UserForm command button returns a application-defined or object-defined error.

Private Sub CommandButton1_Click()

Dim y, vfx, sapx, prx As String

y = Sheets(ComboBox1.Value).Range("B:B").Find(ComboBox2.Value).Row
vfx = Sheets(ComboBox1.Value).Range("1:1").Find("Vendor No").Column
sapx = Sheets(ComboBox1.Value).Range("1:1").Find("SAP Number").Column
prx = Sheets(ComboBox1.Value).Range("1:1").Find("Amey Price").Column

Lrow = Sheets("Macropage").Range("B:B")(Rows.Count).End(xlUp).Row
Userow = Lrow + 1

Sheets("Macropage").Range("A" & Userow) = ComboBox1.Value
Sheets("Macropage").Range("B" & Userow) = ComboBox2.Value
Sheets("Macropage").Range("C" & Userow) = Sheets(ComboBox1.Value).Cells(y, vfx).Value
Sheets("Macropage").Range("D" & Userow) = Sheets(ComboBox1.Value).Cells(y, sapx).Value
Sheets("Macropage").Range("E" & Userow) = Sheets(ComboBox1.Value).Cells(y, prx).Value

Sheets("Macropage").Range("A3:E" & Userow).Columns.AutoFit

Unload UserForm1

End Sub

The code causing the error appears to be: Sheets(ComboBox1.Value).Cells(y, prx).Value which I verified by sticking it in a MsgBox.

The only thing different about the column referenced by prx is it contains decimals rather than some text. I don't see why this should be a problem though.

Any info appreciated, even if it's just about the nature of a application-defined or object-defined error. From googling it only seems to mean Excel has a problem executing the script even if the compiler thinks it is fine.

3
  • 4
    Are you certain that prx has a value? I would put a break point on that line, and see what its value is in the locals window.
    – rory.ap
    Commented Oct 20, 2015 at 13:52
  • 2
    And, if it doesn't have a value, my guess is that there is no column "Amey Price".
    – rory.ap
    Commented Oct 20, 2015 at 13:54
  • 4
    Dim y, vfx, sapx, prx As String - not related to your issue, but you realize only prx is actually declared as a String right? The others are implicitly declared as Variant. Commented Oct 20, 2015 at 14:07

2 Answers 2

4

This is not a solution to question, but a rewrite of the code to better track the error.

Private Sub CommandButton1_Click()

    On Error GoTo ErrExit
    Dim y, vfx, sapx, prx As String

    Dim sheet, Macropage
    Set thisSheet = Sheets(ComboBox1.Value)
    Set Macropage = Sheets("Macropage")

    If thisSheet Then
        MsgBox "You have to select an input page", vbExclamation
        Exit Sub
    End If

    If Macropage Then
        MsgBox "You have to select a Macro page", vbExclamation
        Exit Sub
    End If

    y = thisSheet.Range("B:B").Find(ComboBox2.Value).Row
    vfx = thisSheet.Range("1:1").Find("Vendor No").Column
    sapx = thisSheet.Range("1:1").Find("SAP Number").Column
    prx = thisSheet.Range("1:1").Find("Amey Price").Column

    Lrow = Macropage.Range("B:B")(Rows.Count).End(xlUp).Row
    Userow = Lrow + 1

    Macropage.Range("A" & Userow) = ComboBox1.Value
    Macropage.Range("B" & Userow) = ComboBox2.Value

    Debug.Print "y= " & y, "vfx= " & vfx, "sapx= " & sapx, "prx= " & prx

    Macropage.Range("C" & Userow) = thisSheet.Cells(y, vfx).Value
    Macropage.Range("D" & Userow) = thisSheet.Cells(y, sapx).Value
    Macropage.Range("E" & Userow) = thisSheet.Cells(y, prx).Value

    Macropage.Range("A3:E" & Userow).Columns.AutoFit

SafeExit:
    Unload UserForm1
    Exit Sub
ErrExit:
    MsgBox Error, vbCritical, "Error"
    Resume SafeExit

End Sub
2
  • please post the Debug.Print's string to get an idea of the error
    – milevyo
    Commented Oct 20, 2015 at 14:26
  • Hello, I eliminated the error by removing as string from my declaration of the variables. prx could not be interpreted correctly by cells() as a string. (See Mat's Mug comment to my initial post). Thanks for the response though, I'm likely to refer to this if I never need to hunt down an error cause. Commented Oct 20, 2015 at 14:40
1

Oh god I'm an idiot. Mat's Mug pointed me to it, prx was a string - so Cells() couldn't interpret it correctly as a column index.

Not completely sure why I thought var1, var2, var3 as type would apply same type to all. I thought I had seen variables declared that way before..

Thanks all. It's OK to answer my own question, right?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.