Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

search for last record in range

Posted on 2014-01-11
1
Medium Priority
?
362 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

636 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