VBA for Generating Cycle Counts

bneighbors9

New Member
Joined
Aug 22, 2024
Messages
10
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: 13
  • Sheet2.PNG
    Sheet2.PNG
    40.2 KB · Views: 12
I took out this line and it stopped doing that
Sheets("Sheet2").Range("A4").CurrentRegion.Clear
 
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.
Try.
VBA Code:
Sub SelectPartNumbers()
Dim T&, Lr&, cnt&, K&
Dim A, B, Dt
Lr = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("B2:D" & Lr)
Dt = Sheets("Sheet1").Range("E2:E" & Lr)
ReDim B(1 To UBound(A, 1), 1 To 1)
cnt = WorksheetFunction.Min(WorksheetFunction.CountIf(Sheets("Sheet1").Range("E2:E" & Lr), ""), 15)
If cnt > 0 Then

With CreateObject("Scripting.dictionary")
For T = 1 To cnt
Line1:
K = WorksheetFunction.RandBetween(1, UBound(A, 1))
If Not .exists(A(K, 1)) And Dt(K, 1) = "" Then
.Item(K) = Array(A(K, 1), A(K, 2), A(K, 3)): Dt(K, 1) = Date
Else
GoTo Line1
End If
Next T
Sheets("Sheet1").Range("E2:E" & Lr) = Dt
Sheets("Sheet1").Range("E2:E" & Lr).NumberFormat = "dd-mm-yyyy"
Sheets("Sheet2").Range("A4:C18").Clear
Sheets("Sheet2").Range("A4:C" & 4 + cnt - 1) = Application.Index(.items, 0, 0)
End With

End If
MsgBox cnt & " Part numbers are selected", vbOKOnly, "Free Part Numbers"
End Sub
 
Upvote 0
Solution
Change this
VBA Code:
Sheets("Sheet2").Range("A4:C" & 4 + cnt - 1) = Application.Index(.items, 0, 0)
End With
as
VBA Code:
Sheets("Sheet2").Range("A4:C" & 4 + cnt - 1) = Application.Index(.items, 0, 0)
Sheets("Sheet2").Range("B2") = Date
End With
 
Upvote 0
In image todays date is in B1. You are telling B2.
This line puts date in B2 of sheet2
VBA Code:
Sheets("Sheet2").Range("B2") = Date
Have you added this line in code.
 
Upvote 0
That was my problem.. i changed it to B1 and it is working whole code is now working perfectly. You are the best, I cannot thank you enough
 
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,006
Members
452,542
Latest member
Bricklin

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