Solved

search for last record in range

Posted on 2014-01-11
1
321 Views
Last Modified: 2014-01-11
Folks

I have a for loop , but the loop is limited to a hardcored range(B2:AZ150) where the
where the "X" value is found

however what I require is the loop to be able to  by itself to determine where the last colum is found , this way the loop does not look further if the range value is not present or beyond, if there are

below is my code


Dim rngCell As Range, rngData As Range  
Dim strSQL As String
Set rngData = Sheet1.Range("B2:AZ150")
For Each rngCell In rngData
   If rngCell.Value = "X" Then
     strSQL = "insert into car (brand,make,dealer)values (rowseq.nextval,'" & Cells(rngCell.Row, 1).Value & "','" & Cells(1, rngCell.Column).Value & "');"
     With Sheets("Sheet1")
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = strSQL
     End With
   End If
Next rngCell
0
Comment
Question by:rutgermons
1 Comment
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39773951
Try this:

Sub Demo()

    Dim rngCell As Range, rngData As Range
    Dim rLastCell As Range
    Dim strSQL As String

    Set rLastCell = Sheets("Sheet1").Cells.Find(What:="*", After:=Sheets("Sheet1").Cells(1, 2), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)

    Set rngData = Sheet1.Range("B2:" & ColLtr(rLastCell.Column) & "150")

    For Each rngCell In rngData
        If rngCell.Value = "X" Then
            strSQL = "insert into car (brand,make,dealer)values (rowseq.nextval,'" & Cells(rngCell.Row, 1).Value & "','" & Cells(1, rngCell.Column).Value & "');"
            With Sheets("Sheet1")
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = strSQL
            End With
        End If
    Next rngCell

End Sub

Function ColLtr(iCol As Long) As String
    If iCol > 0 And iCol <= Columns.Count Then ColLtr = Replace(Cells(1, iCol).Address(0, 0), 1, "")
End Function

Open in new window

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

Suggested Solutions

Title # Comments Views Activity
Excel:  Find text between 2nd hyphen and next space 10 30
Excel Automation VBA 19 37
IF ELSE Statement in Excel Macro VBA 16 34
excel help 4 19
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

20 Experts available now in Live!

Get 1:1 Help Now