Compile error: Constant expression required for Random Sample code

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
886
Office Version
  1. 365
Platform
  1. Windows
I have a code I have been using for awhile now that I had assistance writing, however now after an excel update to 365 the code no longer works and get the below error. I am unsure how to modify the code.

1622650449488.png


VBA Code:
Sub RS()
'
' RS Macro
'
 
      If Application.CountIf(Sheets("Random Sample").Range("E1"), "") > 0 Then
        MsgBox "Please Enter User Name"
        Exit Sub
    End If

    Ans = MsgBox(Msg, vbYesNo)

    Select Case Ans

        Case vbYes
 
  sCellText = Sheets("Random Sample").Range("C1").Value
 
  Dim R As Long, Cnt As Long, RandomIndex As Long, Arr As Variant, Tmp As Variant

 ' the below line is where the error is occuring
 Const HowMany = sCellText
  Randomize
  Arr = Sheets("DATA").ListObjects("DATA").DataBodyRange.Value
  With CreateObject("Scripting.Dictionary")
    For R = 1 To UBound(Arr)
      If Arr(R, 5) = "N" Then .Item(CStr(Arr(R, 1))) = 1
    Next
    Arr = .Keys
  End With
  For Cnt = UBound(Arr) To LBound(Arr) Step -1
    RandomIndex = Int((Cnt - LBound(Arr) + 1) * Rnd + LBound(Arr))
    Tmp = Arr(RandomIndex)
    Arr(RandomIndex) = Arr(Cnt)
    Arr(Cnt) = Tmp
  Next
  Sheets("Random Sample").Range("B3").Resize(HowMany) = Application.Transpose(Arr)

End Sub

If anyone could help me with this it would be very much appreciated!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
What sort of data does
VBA Code:
Sheets("Random Sample").Range("C1").Value
hold?
 
Upvote 0
The value assigned in a Const statement must be a constant... it cannot be or be derived from a calculation. This is because VB physically replaces the object name with the value to be assigned to it throughout the code during the pre-compile process which occurs before any lines of code are executed. You are going to have to make HowMany a normal variable.
 
Upvote 0
What sort of data does
VBA Code:
Sheets("Random Sample").Range("C1").Value
hold?

There are 3 sheets: one live database with workorder numbers, one sheet to keep record of which workorders where previously audited and the last (Random Sample) sheet where the macro's function is to grab a random number of workorder numbers from the database sheet.

Hope this makes sense...

Traveller Spot Audit Generator.xlsm
ABCDE
1Weekly Audit Sample Size:10User:
2Row #W/O Number
3#N/A91025
4#N/A109114
5#N/A110439
6#N/A110459
7#N/A110549
8#N/A110807
9#N/A111014
10#N/A111247
11#N/A111319
12#N/A111362
13#N/A111444
14#N/A111483
Random Sample
Cell Formulas
RangeFormula
A3:A14A3=MATCH(B3,DATA!A:A,0)
Named Ranges
NameRefers ToCells
DATA!Query_from_Uniflyte_Inc__WPG=DATA!$A$1:$D$339A3:A14


Traveller Spot Audit Generator.xlsm
ABCDE
1W/O NumberDate ReceivedDate ShippedSerial NumberPreviously Audited
21135762021-04-262021-05-180509-0001N
31139172021-05-272021-05-3107ALT0045N
41136382021-04-292021-05-180912-0009N
51134032021-04-072021-04-281088-125N
61135482021-04-222021-04-2710-95-123N
71136912021-05-072021-05-20112752N
81121582020-12-112021-04-3011418N
91130742021-03-102021-04-2911800N
101132912021-03-242021-05-3111953N
111123392020-12-222021-04-2812401N
121134762021-04-142021-05-071260N
131136022021-04-292021-05-1212-76-81N
141132262021-03-222021-05-191384509/003N
151132002021-03-182021-05-0713850N
161133432021-03-292021-05-14139N
171133142021-03-262021-05-181431358/001N
181135082021-04-192021-06-01181N
191137752021-05-132021-05-271-84-31N
201118062020-11-132021-04-2921242N
211131262021-03-152021-04-2821508N
DATA
Cell Formulas
RangeFormula
E2:E21E2=IFERROR(INDEX('Previously Audited'!B:B,MATCH([@[W/O Number]],'Previously Audited'!A:A,FALSE)),"N")


Traveller Spot Audit Generator.xlsm
AB
1WorkOrderPreviously Audited
2111376Y
3111264Y
4110499Y
5111155Y
6110138Y
7111109Y
8110836Y
9111288Y
10111364Y
11110622Y
12107365Y
13110244Y
Previously Audited
 
Upvote 0
The value assigned in a Const statement must be a constant... it cannot be or be derived from a calculation. This is because VB physically replaces the object name with the value to be assigned to it throughout the code during the pre-compile process which occurs before any lines of code are executed. You are going to have to make HowMany a normal variable.
The data it is grabbing is not derived from a calculation....

I have posted all the data so not sure why I am getting the error then
 
Upvote 0
On this line
VBA Code:
Const HowMany = sCellText
sCellText is a variable & you cannot assign a variable to a constant as it can change.
As you don't seem to be using the HowMany variable, just delete the line
 
Upvote 0
The data it is grabbing is not derived from a calculation....
In the following line of code, sCellText is not a constant... it gets its value from a "calculation"...

sCellText = Sheets("Random Sample").Range("C1").Value

Remember, I said VB will substitute constants for the object name you used in the Const statement BEFORE any code lines are executed... sCellText will not have a value assigned to it until code lines are executed.
 
Upvote 0
Hi,​
and End Select codeline is missing so this code as it is can't work whatever the Excel version …​
 
Upvote 0
On this line
VBA Code:
Const HowMany = sCellText
sCellText is a variable & you cannot assign a variable to a constant as it can change.
As you don't seem to be using the HowMany variable, just delete the line

I would require the HowMany variable as C1 contains the number of WorkOrders the code must randomly select.
I am not sure what is missing or what happened but the macro used to work. Any idea how to fix it and still have it select the number recorded in C1?
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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