Solved

vba error 424

Posted on 2014-10-27
3
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

707 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