I've been trying to create code to compare entries in a list from one workbook to two lists on another workbook (a master list). The end goal is to have a macro which would run and mark if an entry in the list is found in one of the two master lists, called "Statistical" and "Non-statistical" (sheets 1 and 2 respectively) and then labelled accordingly. In order to do this, I need to define the two ranges of the master list workbook as variables for use in the Range.Find method, or any other method which would work.
The lists are set up as such:
- In the original list which is having a check run against it (and also where the code resides), there is an output of typically around 150 entries, filtered (by pre-determined criteria) such so that only maybe 100 are visible, with the number changing for each day's output on a sheet labelled "Changes"
- The "Master Statistical List" workbook is set up with two sheets that I have another macro populating and adding entries to. Sheet1 is named "Statistical" and contains all entries that are deemed "Statistical" and Sheet2 is named "Non-statistical" and contains all entries deemed Non-statistical. The lists are in the thousands of rows, so I try to avoid using the .Select function.
- The two master lists are both stored in column A starting from A1, while the "Changes" list being checked starts from A2 (due to a header).
Here is what I have so far for just the range references code:
'Establish the list references
Dim MSL As Workbook 'Master Statistical List
Dim SP As Worksheet 'Statistical Properties
Dim NSP As Worksheet 'Non-statistical Properties
Dim TWS As Workbook 'This Worksheet - the original one in which this code resides
Set MSL = Workbooks.Open(Filename:="Filepath\Master Statistical List.xlsm")
Set SP = MSL.Worksheets("Statistical") 'Alternatively .Worksheets(1)
Set NSP = MSL.Worksheets("Non-statistical") 'Alternatively .Worksheets(2)
Set TWS = ThisWorkbook
'Establish the Range references
'Statistical Properties List
SP.Activate
Dim rngStat As Range
Set rngStat = Range("A1", .Range("A1").End(xlDown))
'check if it actually worked
rngStat.Select 'it seems to work
Range("B1").Select 'select random cell so we know if selecting it again actually works (I run through this line-by-line to debug)
TWS.Activate 'Lets go back to the current workbook and then see if it still works!
rngStat.Select 'Here be errors
I would be running similar Dim-set combos for both the "Changes" range and the Non-statistical one.
I've tried several different solutions, including using a With parameter as such:
With SP
Set rngStat = Range("A1", SP.Range("A1").End(xlDown))
End With
Or putting SP in front of the .Range function as shown above, but nothing works once the Active sheet is changed.
Is there a way to set rngStat as being universally-referring to the list of entries from A1 of its sheet down to the final entry in such a way that it can be used in whatever loops or functions later on in the macro? It is important that the range not be to a set cell but rather the last entry in the column A, as the count of entries is always changing on the source sheet and increasing on the master list.