VBA for Generating Cycle Counts

bneighbors9

New Member
Joined
Aug 22, 2024
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I am looking for a VBA assigned to a button that will generate a cycle count for inventory control purposes. Each time I press the button, a new list will populate and not repeat any part numbers from a previous time of pressing the button. The list of part numbers with description/ location are on sheet 1 , Column B through D. The count sheet that I need this information to go to is on sheet2.

The button needs to do the following
Put todays date in the top of the sheet2
Randomly pull 15 part numbers from sheet 1 and paste that onto the count sheet (Sheet2 in A4 through A18). It needs to not select any part number that has a date in the E column.
Once a part number has been selected, put todays date in column E on sheet 1 to show the date it was selected and to stop it from being selected again.
Be able to click the button over and over again to create a new list until all part numbers have been selected 1 time.
 

Attachments

  • Sheet1.PNG
    Sheet1.PNG
    69.1 KB · Views: 39
  • Sheet2.PNG
    Sheet2.PNG
    40.2 KB · Views: 36
Try.
VBA Code:
Sub SelectPartNumbers()
Dim T&, Lr&, cnt&
Dim A, B, Dt, M
Lr = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("B2:D" & Lr)
Dt = Sheets("Sheet1").Range("E2:E" & Lr)
cnt = WorksheetFunction.Min(WorksheetFunction.CountIf(Sheets("Sheet1").Range("E2:E" & Lr), ""), 15)
M = Filter(Evaluate("transpose(If('Sheet1'!e2:e" & Lr & "="""",ROW('Sheet1'!b2:b" & Lr & ")-ROW($A$1),False))"), False, False)
If cnt > 0 Then
ReDim B(0 To cnt - 1, 1 To 3)
For T = 0 To cnt - 1
B(T, 1) = A(M(T), 1): B(T, 2) = A(M(T), 2): B(T, 3) = A(M(T), 3): Dt(M(T), 1) = Date
Next T
Sheets("Sheet1").Range("E2:E" & Lr) = Dt
Sheets("Sheet1").Range("E2:E" & Lr).NumberFormat = "dd-mm-yyyy"
With Sheets("Sheet2")
.Range("A4:C18").Clear
.Range("A4:C" & 4 + cnt - 1) = B
.Range("B1") = Date
End With
Else
Sheets("Sheet2").Range("A4:C18").Clear
End If

MsgBox cnt & " Part numbers are selected", vbOKOnly, "Free Part Numbers"
End Sub
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
There is no code for changing the Format of cells.
Here is the sample of Result for 1st 15 Lines
VBA for Generating Cycle Counts.xlsm
ABCD
107-02-2025
2
3
4157221225SEATHJ12FSP01A
508036D1630CLIPHJ12FSP01B
62A54611C50KCPLATE ASSY CLAMPHJ12FSP01C
72A670K1191BRACKETHJ12FSP01C
82079707420BRACKETHJ12FSP02A
92079707430BRACKETHJ12FSP02A
10207943A130BRACKETHJ12FSP02B
112069437A90small stripHJ12FSP02C
1220Y9703572BRACKETHJ12FSP03A
132089707180BRACKET ARMHJ12FSP03B
142089707190BRACKET ARMHJ12FSP03B
1520Y7042E51PLATEHJ12FSP03B
162067072B60PIPEHJ12FSP03C
1720Y7022H50Grease tubeHJ12FSP03C
182A76215610BRACKETHJ12FSP04A
19
Sheet2
 
Upvote 0
Try this code.
VBA Code:
Sub SelectPartNumbers()
Dim T&, Lr&, cnt&
Dim A, B, Dt, M
Lr = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("B2:D" & Lr)
Dt = Sheets("Sheet1").Range("E2:E" & Lr)
cnt = WorksheetFunction.Min(WorksheetFunction.CountIf(Sheets("Sheet1").Range("E2:E" & Lr), ""), 15)
M = Filter(Evaluate("transpose(If('Sheet1'!e2:e" & Lr & "="""",ROW('Sheet1'!b2:b" & Lr & ")-ROW($A$1),False))"), False, False)
If cnt > 0 Then
ReDim B(0 To cnt - 1, 1 To 3)
For T = 0 To cnt - 1
B(T, 1) = A(M(T), 1): B(T, 2) = A(M(T), 2): B(T, 3) = A(M(T), 3): Dt(M(T), 1) = Date
Next T
Sheets("Sheet1").Range("E2:E" & Lr) = Dt
Sheets("Sheet1").Range("E2:E" & Lr).NumberFormat = "dd-mm-yyyy"
With Sheets("Sheet2")
.Range("A4:C18").Clear
.Range("A4:C18").NumberFormat = "@"
.Range("A4:C" & 4 + cnt - 1) = B
.Range("A4:C18").EntireColumn.AutoFit
.Range("B1") = Date
End With
Else
Sheets("Sheet2").Range("A4:C18").Clear
End If

MsgBox cnt & " Part numbers are selected", vbOKOnly, "Free Part Numbers"
End Sub
 
Upvote 0
That fixed the numbers that started with zero.

I changed .Range("A4:C18").Clear to .Range("A4:C18").ClearContents and that fixed the problem !
 
Upvote 0

Forum statistics

Threads
1,226,494
Messages
6,191,363
Members
453,655
Latest member
lasvegasbuffet

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