title: VBA - Delete rows in another worksheet
body: <pre><code>Function DeleteRows()
  
  Dim wb As Workbook
  Set wb = Workbooks(ThisWorkbook.Name)
  
  Debug.Print wb.Name
  
  Dim wsName As String
  Dim ws As Worksheet
  wsName = "SheetX"
  Set ws = wb.Worksheets(wsName)
  
  Debug.Print ws.Name
  
  
  With ws
          Debug.Print "ws.name = " &amp; ws.Name
          Rows("3:31").Select
  
          Selection.Delete Shift:=xlUp
  
  End With
  End Function
  </code></pre>
  
  <p>I have this function, DeleteRows, that deletes the the rows 3:31 from the worksheet "SheetX". </p>
  
  <p>This only works when "SheetX" is the sheet that is displayed on the screen.</p>
  
  <p>I want it to work when another sheet, lets say "SheetY", in the workbook is displayed. How can I do this? </p>
  
  <p>One way would be to:
  1-activate "SheetY"
  2-delete the rows 
  3-activate "SheetX"</p>
  
  <p>But this would not be transparent to the user. Appreciate the help.</p>