Geographos
Geographos

Reputation: 1456

Populate value from one worksheet to another worksheet

I would like to have my frontsheet value populated on another worksheet.

I tried something like this:

Sub THPlow()
    If Range("D39").Value < 24 Then
    Sheets("BoM").Range("E7").Value = D39
    End If
End Sub

But the macro returns nothing:
enter image description here

There is no comment or debug, so it seems that code is fine, but there must be an error somewhere.

Upvotes: 0

Views: 34

Answers (1)

JvdV
JvdV

Reputation: 75850

Perfect example for explicit range references:

Sub THPlow()

Dim ws1 as worksheet: Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Dim ws2 as worksheet: Set ws2 = ThisWorkbook.Worksheets("Sheet2")

If ws1.Range("D39").Value < 24 Then 
    ws2.Range("E7").Value = ws1.Range("D39").Value
End If

End Sub

Or maybe use .Evaluate (less conventional)

Sub THPlow()

Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Worksheets("Sheet2")

If ws1.[D39] < 24 Then ws2.Cells(7, 5).Value = ws1.[D39]

End Sub

Obviously change around the worksheet names as needed. You can also include a variable to hold your Workbook object as it may not always be ThisWorkbook.

Upvotes: 2

Related Questions