Link to home
Start Free TrialLog in
Avatar of W.E.B
W.E.B

asked on

vba error 424

Hello,
can you please help,
I'm getting error 424, Object required on line
    Set rngLastCell = Sheet1.Cells(Sheet1.Rows.Count, strColumn)

Sub Insert()

    Dim cnn As adodb.Connection
    Dim strConnectionString As String
    Dim sh As Worksheet
    Dim strInsert As String
    Dim lngRow As Long

    Set sh = Sheets("Settlements")
    Set cnn = New adodb.Connection
     strConnectionString = "Provider=SQLOLEDB.1;User ID=sa; password=cccccccc!;Initial Catalog=xxxxx;Data Source=Wassim-XPS;"

    'Start
    cnn.Open strConnectionString

    For lngRow = 2 To LastUsedCell("A")
        strInsert = "INSERT INTO DriverSettlementMessages (DriverSettlementNo, DriverNumber, CompanyID, DateInserted) " & _
                    " VALUES (" & _
                    sh.Cells(lngRow, 1) & ", " & _
                    sh.Cells(lngRow, 2) & ", " & _
                    sh.Cells(lngRow, 3) & ", " & _
                    "'" & sh.Cells(lngRow, 4) & "')"

    cnn.BeginTrans
    cnn.CommitTrans

        On Error Resume Next
        cnn.Execute strInsert
    Next lngRow

    cnn.BeginTrans
    cnn.CommitTrans

        MsgBox "Done"
End Sub
                                           
Public Function LastUsedCell(strColumn As String) As String
    Dim rngLastCell As Range
    Set rngLastCell = Sheet1.Cells(Sheet1.Rows.Count, strColumn)

    If IsEmpty(rngLastCell.Value2) Then
        LastUsedCell = rngLastCell.End(xlUp).Row
    Else
        LastUsedCell = rngLastCell.Row
    End If
End Function
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The problem is that the .CELLS() accepts ONLY two numeric values for row & column. You are passing a number and a string.

Set rngLastCell = Sheet1.Cells(Sheet1.Rows.Count, strColumn)

strColumn is a string and NOT a number. You need a column number.
Avatar of W.E.B
W.E.B

ASKER

thank you