As others have pointed out: Range()
references the active sheet.
Which you don't want, so you need better references, one way to do this as pointed out by the other answers, is adding Sheets(index)
before every Range()
instance, whether by adding a With Sheets(index)
before the line and putting a .
before every Range()
instance or by adding Sheets(index)
itself before it.
This works for you, for now, but Sheets(index)
is not safe to use either, especially when one doesn't know what is referenced.
I used index
instead of 1
for a reason: Sheets(index)
references a Sheet
object of the ActiveWorkbook
at a particular index or with that particular name, say Sheets("Sheet 1")
would reference the first sheet of the workbook if you hadn't changed its name, and, to top it all off, the index is variable because it changes every time the Sheets are moved inside the Workbook.
Moving your second Sheet to be the first Sheet will result in your code referencing the Sheet which you don't want to be referenced.
To counter this you should use the sheet's Codename
which can be viewed and changed inside the VBE. However the Codename
cannot be changed at Run-time which helps a great deal when you or your users move Sheets manually around or change their name. Which can always happen by accident.
To sum everything up you should always use ThisWorkbook
, which references the Workbook in which the code is running, instead of the implied ActiveWorkbook
.
And you should use Codename
s of Sheets where applicable, in your case, if I assume you use the English Excel version and haven't changed the Codename
it would look like this:
Private Sub UserForm_Initialize() 'sets up combobox's
PurchUnit.List = Sheet1.Range("PurchasingSizes" & Sheet1.Range("K41").End(xlUp).Row).Value
UoM.List = Sheet1.Range("ConvAbv").Value
End Sub
For further reading on ActiveWorkbook
, ActiveSheet
and Select
in general I recommend this answer to a different question.