VBA- To Select a range based on repeated values from Order id

Stranger8421

New Member
Joined
Jun 30, 2022
Messages
23
Office Version
  1. 365
Hey Guys,

Need one small help from you. Need to select an Order_Num range and cust_Num range from the given below sheet.

Order_NumDateCust_NumOrder_rangeCust_range
141​
09/21/2022C123
141​
09/21/2022C123
146​
09/21/2022C125
146​
09/21/2022C125
146​
09/21/2022C125
146​
09/21/2022C127
148​
09/21/2022C127
148​
09/21/2022C127
148​
09/21/2022C129

So In this First Order id - 141 its have two lines and cust_num for Order id is also have same Cust_num. So Order_range in D3 is "A2 to A3" and Cust_range in E3 is "C2 to C3"
2nd Order id - 146 its have four lines and cust_num for Order id have two cust_num. So Order_range in D7 is "A4 to A7" and Cust_range have two cust_id so in E5 is "C4 to C6" and in E6 range is "C7 to C7".
3rd Order id - 148 has 3 lines and two cust_num for the same. And Order_range in D10 is "A8 to A10" and Cust_range in E9 is "C8 to C9" and in E10 is "C10 to C10"

So the final Sheet should be like:-

Order_NumDateCust_NumOrder_rangeCust_range
141​
09/21/2022C123
141​
09/21/2022C123A2 to A3C2 to C3
146​
09/21/2022C125
146​
09/21/2022C125
146​
09/21/2022C125C4 to C6
146​
09/21/2022C127A4 to A7C7 to C7
148​
09/21/2022C127
148​
09/21/2022C127C8 to C9
148​
09/21/2022C129A8 to A10C10 to C10

I have written a code till the Order_range selection. My code:-

Private Sub CommandButton1_Click()

Dim ws1 As Worksheet, rng As Long, lastrow As Long, FirstInvoice As String, Count As Long, A As Long, Count1 As Long
Dim intComp As Integer, B As Long, intComp1 As Integer, FirstShip As String, C As Long, C1 As Long

Set ws1 = Sheets("Sheet1")
lastrow = ws1.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

FirstInvoice = Cells(2, "A").Value


If FirstInvoice <> "" Then
Count = 1
Count1 = 2
End If

C = 1
C1 = 2


For A = 2 To lastrow + 1


intComp = StrComp(FirstInvoice, Cells(A, "A").Value)

If intComp = 0 Then

Count = Count + 1

Else

' MsgBox (" First invoice range from " & Count1 & "to range = " & Count)
ws1.Cells(A - 1, "D").Value = "A" & Count1 & " to " & "A" & Count


'''Start- Enter here for Cust_range selection


'''End- Enter here for Cust_range selection
Count = Count + 1
Count1 = A
FirstInvoice = Cells(A, "A").Value

End If

Next A

End Sub

My Code Output:-
Order_NumDateCust_NumOrder_rangeCust_range
141​
09/21/2022C123
141​
09/21/2022C123A2 to A3
146​
09/21/2022C125
146​
09/21/2022C125
146​
09/21/2022C125
146​
09/21/2022C127A4 to A7
148​
09/21/2022C127
148​
09/21/2022C127
148​
09/21/2022C129A8 to A10

So could you please help me to select Cust_range.

Regards,
Joe
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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