Vba to find the row number in the within a block of data

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
I have the below data set.



I'm trying to transfer a given block of data onto a userform based on finding the value (ticket #) in column A
In the case of ticket# 50503 there are a total of four line items that need to be captured (columns E, F & G) and listed in the respected text boxes on a userform.
The amount of rows varies by each ticket number. Some have two lines, some have 3, 7 8 etc.,

What can I use and how can I find the last row within any given block of data?
Once found, how can a loop be setup for this?
Ticket #MakeModelColorPart#QtyServiceDescriptionTechExpected ReturnReturned to ShopInvoice #Complete
or Partial
50500​
ChevyCamaroWhite1013AA33 LINE SVCBrian3/26/20204/9/202065231
1051AA23 LINE SVC
1051.11AB43 LINE CLR
50501​
ChevyCorvetteCream1013AB83 LINE SVCMatt3/27/20204/9/2020 & 4/9/202015440 / 626
1051AA23 LINE SVC
50502​
FordEscapeBlue1014AB23 LINE SVCChris3/30/20204/9/202084805
1051AA23 LINE SVC
1011.11215z3 LINE CLR
1051.11314z3 LINE CLR
1051.2111sz3 LINE CLR
50503​
ToyotaCamrySilver1011AB23 LINE SVCSteve3/31/20204/9/2020010810
1011.11215z3 LINE CLR
1051.11314z3 LINE CLR
1051.2111z3 LINE CLR
50504​
FordF-150Black10115z2z2 LINE SVCAlex4/1/20204/9/2020515008
10121z3z2 LINE SVC
10133z4z2 LINE SVC
10142z6z2 LINE SVC
21143szCLR
50505​
DodgeChallengerSilver21243swCLRCharlie4/2/20204/9/202000156
2132AW1CLR
2142Aw6CLR
2151z9zMXA X
10512zc5MK4
50506​
ChevyMalibuWhite10521zc6RGwBrian4/3/20204/9/202010510
10531zc7Feg
11612zd6RTG
50507​
BMW640iRed11622zd7GXRSteve4/7/20204/11/20203215
10521zdQGSE
50508​
BMW440iWhite10113z2z2 LINE SVCJerry4/8/20204/11/202012131
2152z9zFEM
10511zc5PLG
11614zd6RWE
11622zd7ETW
1076ZC4SSR
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Are the numbers always sequential?

if so, you could use the matchformula to get the start row. You could then use the same formula to get the end row by searching for the next ticket in the sequence and deducting one.


edited: not find but match formula.
 
Upvote 0
For the first part, how about
VBA Code:
Sub Jim()
   Dim Fnd As Range, Rng As Range
   Dim Ticket As Long
   
   Ticket = 50508
   Set Fnd = Range("A:A").Find(Ticket, , , xlWhole, , , , , False)
   If Fnd Is Nothing Then Exit Sub
   If Fnd.Offset(1).Value = "" Then
      Set Rng = Range(Fnd, Fnd.End(xlDown).Offset(-1))
   Else
      Set Rng = Fnd
   End If
   Set Rng = Rng.Offset(, 4).SpecialCells(xlConstants)
   Rng.Select
End Sub
 
Upvote 0
Yes, the numbers are sequential.

Could you provide an example of the matchformula? I don't know it.
Thank you.
 
Upvote 0
Yes, the numbers are sequential.

Could you provide an example of the matchformula? I don't know it.
Thank you.
This should work. But it does require a seperate worksheet for calculations.

The source data (your table) is listed in worksheet Sheet2.
The calculation sheet is called CalculationSheet (no points for originality, I know)


VBA Code:
Sub Macro1()

'Go to calculation sheet
    Sheets("CalculationSheet").Select


' Determine first and last ticket numbers
    Range("A6").FormulaR1C1 = "First ticket"
    Range("A7").FormulaR1C1 = "Last ticket"

    Range("B6").FormulaR1C1 = "=MIN(Sheet2!C[-1])"
    Range("B7").FormulaR1C1 = "=MAX(Sheet2!C[-1])"

    firstticket = Range("CalculationSheet!B6").Value
    lastticket = Range("CalculationSheet!B7").Value
    Numberoftickets = Range("CalculationSheet!B8").Value


Dim i As Long
For i = firstticket To lastticket


' Determine data range to be selected
    Sheets("CalculationSheet").Select
    Range("A1").FormulaR1C1 = "Search value"
    Range("A2").FormulaR1C1 = "First row"
    Range("A3").FormulaR1C1 = "Last row"

    Range("B1").FormulaR1C1 = i
    Range("B2").FormulaR1C1 = "=MATCH(R1C2,Sheet2!C1,0)"
    Range("B3").FormulaR1C1 = "=IF(R[-2]C=R[4]C,COUNTA(Sheet2!C[3]),MATCH(R1C2+1,Sheet2!C1,0)-1)"

    firstrow = Range("CalculationSheet!B2").Value
    lastrow = Range("CalculationSheet!B3").Value


' Select data
    Sheets("Sheet2").Select

    firstrow = Range("CalculationSheet!B2").Value
    lastrow = Range("CalculationSheet!B3").Value

    Range("E" & firstrow & ":H" & lastrow).Select


' TYPE HERE THE CODE FOR WHAT YOU WANT TO DO WITH THE SELECTED DATA


Next i

End Sub
 
Last edited:
Upvote 0
Peter,

Thank you very much. However, going to another sheet is not an option.
I need to stay within the module and keep the code short.

Fluff,

I'm working with your code, but don's see how it finds the last row in the block.

Once the last row is found in the block, I want to loop to the last row within the block and copy cell values onto the userform.

Perhaps what I have so far you, or someone can correct so it does what I need?

Since some tickets may have just one line item, my thought is to insert first Ticket Item row onto the userform. Then (if next row below ticket number is blank) loop for rest of the block and load those lines (Item #'s, Descriptions, and Quantitiy line items) onto Userform


VBA Code:
   Me.txtPcMk1 = ws3.Cells(aRow, 7).Value
Me.txtItem1 = ws3.Cells(aRow, 10).Value
   Me.txtQS1 = ws3.Cells(aRow, 8).Value

   Dim Fnd As range, Rng As range
Dim Ticket As Long

Ticket = Me.txtTicket.Value
Set Fnd = range("C:C").Find(Ticket, , , xlWhole, , , , , False)
Set Rng = Fnd
' Set Rng = range(Fnd, Fnd.End(xlDown).Offset(-1))
If Fnd.Offset(1).Value = "" Then
For i = 2 To Last Row in block ' <------------------------------------ This is wat I need
Me.Controls("txtPcMk" & i) = ws3.Cells(aRow + 1, 7).Value
Me.Controls("txtItem" & i) = ws3.Cells(aRow + 1, 10).Value
Me.Controls("txtQS" & i) = ws3.Cells(aRow + 1, 8).Value
aRow = aRow +1
    Next i
 
Upvote 0
In your op the ticket is in col A & you said you wanted the value from columns E:G, your code is not doing that.
What column is the ticket number in & what columns should go in the textboxes?
 
Upvote 0
Does this mean that you have sorted it?
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top