0

I have a problem with using cells as range parameters. I am using a workbook from a different excel file that i imported like this :

FileLocation = Application.GetOpenFilename
    If FileLocation = "False" Then
        Beep
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    Set Importworkbook = Workbooks.Open(Filename:=FileLocation)

And i'm trying to select some cells from a worksheet in this workbook :

Public Sub selection()

    Debug.Print (Importworkbook.Name)
    Importworkbook.Activate

    Importworkbook.Worksheets(4).Range(Cells(1, 6), Cells(1, 9)).Select
    
End Sub

But the selection line doesnt work and i don't understand why (I am fairly new to vba so sorry if there's horrible coding )

  • You need to select that sheet first (or avoid selecting at all as it's rarely necessary) and then specify the sheet for the cells too: `Importworkbook.Worksheets(4).Range(Importworkbook.Worksheets(4).Cells(1, 6), Importworkbook.Worksheets(4).Cells(1, 9)).Select` – Rory Feb 10 '23 at 09:50
  • Thanks it works, why do we need to specify the sheet for the cells in this case but not when used alone ? – SkyHickory Feb 10 '23 at 09:57
  • 1
    You should *always* specify the sheet. Otherwise the result will depend on where the code is located and/or which sheet is currently active. – Rory Feb 10 '23 at 10:04
  • 1
    Obligatory link: https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – FunThomas Feb 10 '23 at 10:06

0 Answers0