Solved

search for last record in range

Posted on 2014-01-11
1
310 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 26

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

757 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

21 Experts available now in Live!

Get 1:1 Help Now