Solved

search for last record in range

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

856 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