Hi all,
I am trying desperately to get this spreadsheet to conform, my VBA knowledge is limited, and I have tried recording macros etc, but cannot seem to get this to work.
I have attached the spreadsheet to help out with my explanation.
In Sheet "CP", the allocate button has the following code attached, but I need it to allocate the numbers based on the volume in F5 for block 1, and F6 for block 2 and so on for each individual based on their values.
Here is the code I have so far. I have tried to get the button to check first that the remaining amount of numbers in J4 & K4 and if below 0 a message box appears canceling the request and requesting the person to go back and check their allocation figures.
Private Sub CommandButton2_Click()
Dim CPsh As Worksheet, NAsh As Worksheet
Dim blocks() As Range, trainers, blockaddress
Dim i As Long, j As Long, to_allocate As Long, allocated As Long, counter As Long, where As Long
Randomize
Set CPsh = Sheets("CP")
Set NAsh = Sheets("Number Allocation")
trainers = WorksheetFunction.Transpose(CPsh.Range("A4:A50").Value)
blockaddress = Split("N3:N1002,N1004:N2900", ",")
For i = 1 To 2 'to number of blocks
ReDim Preserve blocks(1 To i)
Set blocks(i) = NAsh.Range(blockaddress(i - 1))
blocks(i).ClearContents
For j = 1 To 40 'all trainers
counter = 0
to_allocate = F6 'of course calculate based on data from CPsh and already assigned
allocated = 0
Do
where = 1 + Int(blocks(i).Rows.Count * Rnd)
If blocks(i).Cells(where) = "" Then
blocks(i).Cells(where) = trainers(j)
allocated = allocated + 1
End If
Loop Until allocated = to_allocate Or counter > 1000
If counter > 1000 Then
MsgBox "Sorry, SparkCRM Auto Allocation has incurred an error. Please press the clear button in Number Allocation and reset."
End If
Next j
Next i
Me.Hide
MsgBox ("SparkCRM - Thanks! You have successfully allocated the required number of numbers to the trainers!")
End Sub
Once it has done this, I am wanting to filter the information on the 'Number Allocation' sheet and email the respective individuals their allocated numbers. I have looked into filtering the data one by one into another sheet and emailing, but this seems a long rounded way to go about it?
Any help would be greatly appreciated.
I have already lost the sheet once due to a '400' error!
Thanks in advance, Dan
Bookmarks