Solved

vba error 424

Posted on 2014-10-27
3
205 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn how the fundamental information of how to create a table.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now