Solved

vba error 424

Posted on 2014-10-27
3
207 Views
Last Modified: 2014-10-27
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
0
Comment
Question by:W.E.B
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40407198
The code doesn't know what Sheet1 is. Try change the two relevant lines so:

For lngRow = 2 To LastUsedCell("A", sh)

Open in new window

Public Function LastUsedCell(strColumn As String, Sheet1 as Worksheet) As String

Open in new window

0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40407247
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.
0
 

Author Closing Comment

by:W.E.B
ID: 40407415
thank you
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question