Method 'Range' of object '_Worksheet' failed on using Range("Reference!D3")
I am trying to run a code to populate cell F4 with the value from a reference sheet and input a formula in G4 to calculate the number of people that are in my room. All of this is based on the information (room type) provided in D4. I have included a loop to ensure if any "new rooms" are added to the sheet it will check the cells below. The code was working before I made some adjustments and it shows an error of:
Method 'Range' of object '_Worksheet' failed
I have checked that all referenced cells contain data, but I am not sure why else there is an error. Here is a small sample of the code I have written:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("D4").Select
Do Until IsEmpty(ActiveCell)
'People Density
If Range("D4").Value = "Booking/Waiting" Then
Range("F4").Value = Range("Reference!D3").Value
ElseIf Range("D4").Value = "Cells without plumbing fixtures" Then
Range("F4").Value = Range("Reference!D4").Value
ElseIf Range("D4").Value = "Cells with plumbing fixtures" Then
Range("F4").Value = Range("Reference!D5").Value
End If
'# of People Calculation - ASHRAE
If Range("D4").Value = "Booking/Waiting" Then
Range("G4").Formula = "=(E4/1000)*Reference!D3"
ElseIf Range("D4").Value = "Cells without plumbing fixtures" Then
Range("G4").Formula = "=(E4/1000)*Reference!D4"
ElseIf Range("D4").Value = "Cells with plumbing fixtures" Then
Range("G4").Formula = "=(E4/1000)*Reference!D5"
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Thanks in advance for any and all help received!
1 answer
-
answered 2021-02-22 23:11
Dave
I suspect there are other issues with the code, or at least I do not understand the code purpose in its entirety.
But the specific issue can be resolved by correcting the reference to the other worksheet.
Simply use:
Range("F4").Value = Worksheets("Reference").Range("D3").Value
Instead of:
Range("F4").Value = Range("Reference!D3").Value
And do the same in all the 6 places you are using the
Reference!
.See more examples for references to other sheets from Microsoft: https://docs.microsoft.com/en-us/office/vba/api/Excel.Range(object)