Home > Net >  How to change value of cell in a different sheet
How to change value of cell in a different sheet

Time:07-25

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

CodePudding user response:

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("C22").Value = automater.Range("D24").Value
End Sub

CodePudding user response:

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
  • Related