Solved

vba error 424

Posted on 2014-10-27
3
203 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:Wass_QA
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:Wass_QA
ID: 40407415
thank you
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now