userform to cycle through data

neilp

Well-known Member
Joined
Jul 5, 2004
Messages
529
Office Version
  1. 365
Platform
  1. Windows
I posted this a few days ago, but I am terrible at explaining myself, so no-one was able to help

I thought i would give it another attempt, simplified and with examples, so sorry for the length of this.

I have a spreadsheet and a User Form (attached)


Copy of shelf work.xls
ABCDE
1Collection DateConsignment NumberBarcode NumberCodeCount
22020-11-02 17:00ZZ302001061002131361317805652461243
32020-11-02 17:00ZZ302001061002131361317805652471243
42020-11-02 17:00ZZ302001061002131361317805652481243
52020-11-02 17:00ZZ302001061002131361417805652561358
62020-11-02 17:00ZZ302001061002131361417805652571358
72020-11-02 17:00ZZ302001061002131361517805652671232
82020-11-02 17:00ZZ302001061002131361517805652681232
92020-11-02 17:00ZZ302001061002131361517805652691232
102020-11-02 17:00ZZ302001061002131361517805652701232
112020-11-02 17:00ZZ302001061002131361617805652751321
122020-11-02 17:00ZZ302001061002131361617805652761321
132020-11-02 17:00ZZ302001061002131361717805652861203
142020-11-02 17:00ZZ302001061002131361717805652871203
152020-11-02 17:00ZZ302001061002131361717805652881203
data


the userform is fairly simple - the textbox in line1 is textbox1, the textboxes in line2 are textbox2 and textbox3, etc, etc.

I need a macro that, when the process button it pressed, excel will search through the data in the spreadsheet looking at the "code" column.

If, as in this example, it find 3 lines that all have the same code (1243), then i would like it to populate column "count" with whatever is in the textboxes next to line 3 on the userform (the first textbox would go in the first in the first row that code appears, the second in the second row and the third textbox in the third row.
it would then do a similar thing if it found 2 lines with the same code (1358), but would use the textboxes from "line2" on the userform etc

so the following would be the output from the "Userform example"

Copy of shelf work.xls
ABCDE
1Collection DateConsignment NumberBarcode NumberCodeCount
22020-11-02 17:00ZZ3020010610021313613178056524612433
32020-11-02 17:00ZZ3020010610021313613178056524712431
42020-11-02 17:00ZZ3020010610021313613178056524812432
52020-11-02 17:00ZZ3020010610021313614178056525613587
62020-11-02 17:00ZZ3020010610021313614178056525713585
72020-11-02 17:00ZZ3020010610021313615178056526712323
82020-11-02 17:00ZZ3020010610021313615178056526812323
92020-11-02 17:00ZZ3020010610021313615178056526912321
102020-11-02 17:00ZZ3020010610021313615178056527012326
112020-11-02 17:00ZZ3020010610021313616178056527513217
122020-11-02 17:00ZZ3020010610021313616178056527613215
132020-11-02 17:00ZZ3020010610021313617178056528612033
142020-11-02 17:00ZZ3020010610021313617178056528712031
152020-11-02 17:00ZZ3020010610021313617178056528812032
data


has this helped anyone decifer my brain?

thanks for sticking with this to the bottom

Neil
 

Attachments

  • userform example.JPG
    userform example.JPG
    61.7 KB · Views: 10
  • userform blank.JPG
    userform blank.JPG
    64.5 KB · Views: 10

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
@neilp - Please do not post the same question as a new thread. If you will provide more details, then please do that in the same thread instead creating a new one. Otherwise the helpers who are already trying to understand your question will never know that you posted a duplicate thread.
Please read the Message Board Rules carefully - Rule #12.

Now, about your question.

Create the text boxes by using a matrix naming pattern.
  • Line 1: TextBox11
  • Line 2: TextBox21 - TextBox22
  • Line 3: TextBox31 - TextBox32 - TextBox33
  • ....
  • Line X: TextBoxX1 - ... - TextBoxXX
Create a command button named as btnProcess, and copy and paste the following event procedure for this command button in the Userform module.

VBA Code:
Private Sub btnProcess_Click()
Dim sht As Worksheet
Dim rng As Range
Dim firstMatch As Range
Dim lastMatch As Range
Dim matchRange As Range

    ' Define the worksheet
    Set sht = ActiveWorkbook.Worksheets("data")

    ' Set the data range
    ' Using the existing range for this sample code (skipping the header row)
    ' Adjust it for your real data range
    Set rng = sht.Range("A2:E15")
    
    Set firstMatch = rng.Rows(1).Cells(4)
    Set lastMatch = firstMatch
    
    ' Loop through the rows in the range until the current code cell value is empty
    Do Until IsEmpty(firstMatch.Value)
        DoEvents
        ' Loop for each matching value in the Code column cells
        Do
            Set lastMatch = lastMatch.Offset(1)
        Loop Until firstMatch.Value <> lastMatch.Offset(1).Value
        Set matchRange = sht.Range(firstMatch, lastMatch)
        
        ' This is where the Count column cells are filled with the
        ' corresponding text box values.
        ' That's why we used the matrix pattern to name the text boxes
        For i = 1 To matchRange.Rows.Count
            matchRange.Rows(i).Offset(, 1).Value = Me.Controls("TextBox" & matchRange.Rows.Count & i).Value
        Next i
        
        Set firstMatch = lastMatch.Offset(1)
    Loop
End Sub

There might be alternative solutions. This one I wrote is looping through the rows, detect the same codes, define a range contains the same code values, and pick the corresponding text box values to fill in the Count cells.

I hope this gives the idea.
 
Upvote 0
Solution
@neilp - Please do not post the same question as a new thread. If you will provide more details, then please do that in the same thread instead creating a new one. Otherwise the helpers who are already trying to understand your question will never know that you posted a duplicate thread.
Please read the Message Board Rules carefully - Rule #12.

Now, about your question.

Create the text boxes by using a matrix naming pattern.
  • Line 1: TextBox11
  • Line 2: TextBox21 - TextBox22
  • Line 3: TextBox31 - TextBox32 - TextBox33
  • ....
  • Line X: TextBoxX1 - ... - TextBoxXX
Create a command button named as btnProcess, and copy and paste the following event procedure for this command button in the Userform module.

VBA Code:
Private Sub btnProcess_Click()
Dim sht As Worksheet
Dim rng As Range
Dim firstMatch As Range
Dim lastMatch As Range
Dim matchRange As Range

    ' Define the worksheet
    Set sht = ActiveWorkbook.Worksheets("data")

    ' Set the data range
    ' Using the existing range for this sample code (skipping the header row)
    ' Adjust it for your real data range
    Set rng = sht.Range("A2:E15")
   
    Set firstMatch = rng.Rows(1).Cells(4)
    Set lastMatch = firstMatch
   
    ' Loop through the rows in the range until the current code cell value is empty
    Do Until IsEmpty(firstMatch.Value)
        DoEvents
        ' Loop for each matching value in the Code column cells
        Do
            Set lastMatch = lastMatch.Offset(1)
        Loop Until firstMatch.Value <> lastMatch.Offset(1).Value
        Set matchRange = sht.Range(firstMatch, lastMatch)
       
        ' This is where the Count column cells are filled with the
        ' corresponding text box values.
        ' That's why we used the matrix pattern to name the text boxes
        For i = 1 To matchRange.Rows.Count
            matchRange.Rows(i).Offset(, 1).Value = Me.Controls("TextBox" & matchRange.Rows.Count & i).Value
        Next i
       
        Set firstMatch = lastMatch.Offset(1)
    Loop
End Sub

There might be alternative solutions. This one I wrote is looping through the rows, detect the same codes, define a range contains the same code values, and pick the corresponding text box values to fill in the Count cells.

I hope this gives the idea.
Hi @smozgur.

Apologies for contravening the forum rules, and thank you for not only still allowing the post, but also answering it with what I can only describe as a piece of genius.

Not only have you managed to understand my mind, but you have solved the problem.

Brilliant.

Thanks so Much

Neil
 
Upvote 0

Forum statistics

Threads
1,215,062
Messages
6,122,923
Members
449,094
Latest member
teemeren

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