Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 1132
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Post by Grovkillen » Thu Oct 13, 2016 11:04 am

I cannot get this to work with my VB call.

This work within the VBA modul in Excel:

Code: Select all

Range(Cells(2, 2), Cells(40, 40)).Select
But this will not work when I run VB through MS:

Code: Select all

...
Set ExcelApp = CreateObject("Excel.Application")
    Set MyBook = ExcelApp.Workbooks.Open(filename)
    Set MySheet = MyBook.Worksheets(sheetname)
MySheet.Range(Cells(2,2), Cells(40,40)).Select
...
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1132
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Post by Grovkillen » Thu Oct 13, 2016 12:03 pm

I ended up doing this:

Code: Select all

MyRange = MySheet.Cells(40, 40).Address
    MySheet.Range("B2:" & MyRange).Select
This would equal to:

Code: Select all

Range(Cells(2, 2), Cells(40, 40)).Select
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1132
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Post by Grovkillen » Thu Oct 13, 2016 12:15 pm

Just found out that this would work...

Code: Select all

MySheet.Range(MySheet.Cells(2, 2), MySheet.Cells(40, 40)).Select
I.E. the Cells command need to have "MySheet" specified ...
Let>ME=%Script%

Running: 15.0.27
version history

zabros2020
Pro Scripter
Posts: 70
Joined: Sun May 03, 2009 11:49 pm
Location: AU

Re: Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Post by zabros2020 » Fri Oct 14, 2016 12:48 am

Depends on what you want to do, could do something like this...

Code: Select all

Let>localFileDir=C:\Temp\temp.xls
Let>sheetName=Sheet1
XLSheetToArray>%localFileDir%,%sheetName%,sheetArray
'Select range
Let>rRow=40
Let>rCol=40
'Select first row and column to start from
'if 1, this will select 2
Let>fRow=1
Let>fCol=1
GoSub>SelectRange
MessageModal>%cText%

SRT>SelectRange
  Let>cText=
  Let>row=%fRow%
  Repeat>row
    Add>row,1
    Let>col=%fCol%
    Repeat>col
      Add>col,1
      Let>sText=sheetArray_%row%_%col%
      Position>sheetArray_,%sText%,1,pos
      If>pos=0
          ConCat>%cText%,%sText%
          If>col=rCol
            ConCat>%cText%,%CRLF%
          EndIf
      EndIf
    Until>col,%rCol%
  Until>row,%rRow%
END>SelectRange
Or if you know exactly where 1 value is that you need:

Code: Select all

Let>localFileDir=C:\Temp\temp.xls
Let>sheetName=Sheet1
XLSheetToArray>%localFileDir%,%sheetName%,sheetArray
Let>row=2
Let>col=2
Let>sText=sheetArray_%row%_%col%
Loving MS's Capabilities!!!

User avatar
Grovkillen
Automation Wizard
Posts: 1132
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Post by Grovkillen » Sat Oct 15, 2016 6:57 am

Thanks for your ideas but in my case I create a new Excel-file and simultaneously add conditional formatting to parts of my sheet.
Let>ME=%Script%

Running: 15.0.27
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Excel VBA "Range(Cells(X,X), Cells(Y,Y)).Select

Post by Marcus Tettmar » Sat Oct 15, 2016 2:53 pm

The "Cells" object belongs to the "Sheet" object which is why you need to do MySheet.Cells. Without doing that VBScript has no idea what you mean by Cells.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts