Hugo Almeida
Hugo Almeida

Reputation: 49

How to change value of cell in a different sheet

I'm trying to replace the value of a cell in the "Visualizer" sheet with the value of a cell in the "Automater" worksheet. However, nothing happens.

This is my code:

Sub RepForm_Click()
    Dim visualizer3 As Worksheet, automater As Worksheet

    Set visualizer3 = Sheets("Visualizer")
    Set automater = Sheets("Automater")
    Range("C22").Value = Range("D24").Value <-- Replaces a cell in the same sheet, works perfectly
    visualizer3.Range("E2").Value = Range("D24").Value <-- Replaces a cell in a different sheet, doesn't work
End Sub

Upvotes: 0

Views: 62

Answers (2)

Cyril
Cyril

Reputation: 6829

You need to qualify your ranges... you didn't specify automater.range().

Sub RepForm_Click()
    Dim visualizer3 As Worksheet, automater As Worksheet
    Set visualizer3 = Sheets("Visualizer")
    Set automater = Sheets("Automater")
    visualizer3.Range("E2").Value = automater.Range("D24").Value
End Sub

Edit1: Updated cell reference on visualizer3 from C22 to E2.

Upvotes: 2

ALeXceL
ALeXceL

Reputation: 661

Change the code including the parent sheets and the Workbook of the ranges you are dealing with:

   Option Explicit

    Sub RepForm_Click()
        Dim visualizer3 As Worksheet, automater As Worksheet
    
        Set visualizer3 = ThisWorkbook.Sheets("Visualizer")
        Set automater = ThisWorkbook.Sheets("Automater")
        visualizer3.Range("C22").Value = visualizer3.Range("D24").Value
        automater.Range("E2").Value = visualizer3.Range("D24").Value
    End Sub

Upvotes: 1

Related Questions