Solved

search for last record in range

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

717 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