A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
p1n and p1v are Range objects. When they are set like this:
Dim p1n As Range 'selected pipe
Dim p1v As Range
Set p1n = Range("RP1SA") 'selected pipe
Set p1v = Range("RP1S")
they always refer to the same cells, not to the values that were in those cells at the time of Set. Changing the value or formula in RP1SA or RP1S does not require resetting the Range variable; the Range object still points to that cell.
If later code appears to show p1n “keeping” the old value (2 instead of 1.5), the usual causes are:
- Reading
.Valuebefore the worksheet has recalculated. - Accidentally working with a different range (for example, a different sheet or workbook where the name
RP1SAalso exists). - Using a cached variable that stored the value instead of the Range.
To ensure the value is current and from the intended sheet:
With ThisWorkbook.Worksheets("SheetName")
.Calculate 'or Application.Calculate if needed
Debug.Print .Range("RP1SA").Value
Debug.Print .Range("RP1S").Value
End With
or, using the variables already declared:
Application.Calculate 'if calculation is manual
Debug.Print p1n.Value 'should now show 1.5
Debug.Print p1v.Value 'should show 1 1/2"
If there are multiple workbooks, qualify the Range with the correct workbook and worksheet when setting the variables:
Set p1n = ThisWorkbook.Worksheets("SheetName").Range("RP1SA")
Set p1v = ThisWorkbook.Worksheets("SheetName").Range("RP1S")
This guarantees that p1n.Value always returns the recalculated value of the RP1SA cell that contains the VLOOKUP formula.
References: