6 Replies - 423 Views - Last Post: 23 June 2012 - 07:38 PM Rate Topic: -----

#1 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Help with Syntax

Posted 21 June 2012 - 12:07 PM

Hi I have defined wb1 as a Workbook.

I am trying to run this
 wb1.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Select 

It is supposed to select the last row in a cell, however it gives me an error

Run-time error '1004'
Application-defined or object-defined error.

I am sure it has something to do with Syntax, not sure how to correct it. Any help will be greatly appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Syntax

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: Help with Syntax

Posted 21 June 2012 - 03:34 PM

One way to figure this out is to go into Excel, open VBA, and try to execute the line there. You'll get more information on what your "application-defined error" is.
Was This Post Helpful? 0
  • +
  • -

#3 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Help with Syntax

Posted 22 June 2012 - 05:23 AM

View PostBobRodes, on 21 June 2012 - 03:34 PM, said:

One way to figure this out is to go into Excel, open VBA, and try to execute the line there. You'll get more information on what your "application-defined error" is.



Not really sure if you are referring to something else. I am writing this code in the VBA window in Excel. The error I specifically received is what I posted. Are you referring to something else to execute the line on its own. If so, then I am not aware of how to do that. Can you elaborate.
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: Help with Syntax

Posted 22 June 2012 - 12:46 PM

Oh sorry, thought you were using Automation with VB6. Ok, so how have you "defined wb1 as a workbook" exactly?
Was This Post Helpful? 0
  • +
  • -

#5 nquadr  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 30-October 10

Re: Help with Syntax

Posted 22 June 2012 - 01:01 PM

View PostBobRodes, on 22 June 2012 - 12:46 PM, said:

Oh sorry, thought you were using Automation with VB6. Ok, so how have you "defined wb1 as a workbook" exactly?



Here is an abbreviated version of the code.

'Modified code to make it easier to review


Dim xlapp As Object
Public wb1 As Workbook
Public wb2 As Workbook

Sub LinkOtherFiles()

    Set xlapp = CreateObject("Excel.Application")
    
    'Set pathnames
    Set wb1 = xlapp.Workbooks.Open("")
    Set wb2 = xlapp.Workbooks.Open("")
    
End Sub

Sub WorkACol()

    wb1.Activate
    wb1.ActiveSheet.Range("B:C").Insert
    
End Sub


'********************Run this code after modifying pathnames***********************************


Sub RunProject()

    Call LinkOtherFiles
    Call WorkACol
    
End Sub

'Run this after Running Sub RunProject
'I want to select all data and create a range so that I can create a filter



Sub WorkA()

    Dim lastRow As Long
    Dim lastCol As Long
    
    wb1.ActiveSheet.Cells(1, 2).Value = "Mnemonic"
    wb1.ActiveSheet.Cells(1, 3).Value = "Comments"
    wb1.Activate
    'this next line produces error
    wb1.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Select  'this will be changed to wb1.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    'lastCol = wb1.ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column


End Sub


Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: Help with Syntax

Posted 22 June 2012 - 05:09 PM

All right, so you can see why I thought you were using automation. It's because you were. I have the feeling that's where your problem is, although I'm not sure. I've never tried this before. Why aren't you using the intrinsic application object instead, since it's simpler and much faster?
Was This Post Helpful? 0
  • +
  • -

#7 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 172
  • View blog
  • Posts: 1,560
  • Joined: 17-April 07

Re: Help with Syntax

Posted 23 June 2012 - 07:38 PM

this is just a suggestion,
you just activate the work book, not the sheet so activate the sheet or a cell, then try it again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1