Results 1 to 14 of 14

Thread: Object variable or with set variable not set. (resolved)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Object variable or with set variable not set. (resolved)

    Using codes from marco (excel) i have use codes like in vb like :

    worksheet.Range("A5:P9").Select
    Selection.Copy

    ..

    but i got the above error msg at the selection.copy ..and worst of all.sometimes it works sometimes it does not..( ie.hit the error)..what happen?

    Regard
    Goh
    Last edited by Goh; Sep 23rd, 2005 at 01:37 AM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Object variable or with set variable not set.

    I have this example of creating a text file of an Excel sheet.
    VB Code:
    1. Option Explicit
    2. ' Set a reference to M$ Excel xx.0 Object Library
    3.  
    4. Private Sub Form_Load()
    5. Dim objExcel As Object
    6. Dim objWorkbook As Object
    7. Dim objSheet As Excel.Worksheet
    8. Dim ff As Integer, r As Integer, c As Integer
    9. Dim txt As String
    10. Set objExcel = CreateObject("Excel.Application")
    11. Set objWorkbook = objExcel.Workbooks.Open(App.Path & "\book1.xls")
    12. Set objSheet = objExcel.Worksheets.Item(1)
    13.  
    14. ff = FreeFile
    15. Open App.Path & "\Grid.txt" For Output As #ff
    16.  
    17.  
    18. For r = 1 To objSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
    19.   txt = ""
    20.   For c = 1 To objSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    21.     If objSheet.Cells(r, c).Value <> "" Then
    22.       txt = txt & objSheet.Cells(r, c).Value & ", "
    23.     End If
    24.   Next c
    25.   If Len(txt) > 0 Then txt = Left(txt, Len(txt) - 2)  ' remove last ", "
    26.   Print #ff, txt
    27. Next r
    28. Close #ff
    29. Set objSheet = Nothing
    30. objWorkbook.Close
    31. Set objWorkbook = Nothing
    32. objExcel.Quit
    33. Set objExcel = Nothing
    34. Unload Me
    35. End Sub

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Object variable or with set variable not set.

    You need to post more of your code so we can see what is really going on. You state your using vb, but is it vb6 or vb.net, etc.?


    @dglienna, whay are you using Object as a variable type when you have set a reference to excel? Your early binding AND late binding in the same project. No need, just do one or the other.


    Moved from Classic VB forum
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Re: Object variable or with set variable not set.

    I am using this version of coding:
    ..
    ..

    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Add
    Set xlsheet = xlApp.ActiveSheet
    ..

    is there a difference between the one you have given and this? I mean which is a better one? Whats the difference?Sorry to ask this as what i am doing now is amending codes that have been written in the past. Thus careful consideration has to be done if there is a need to use the 'object'..thanks

    Regards
    Alfred

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Object variable or with set variable not set.

    Can you post the code where you posted

    ???

    worksheet.Range("A5:P9").Select
    Selection.Copy

    ???
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Object variable or with set variable not set.

    Hmmm. How would you write it? I pulled that from somewhere, and modified it slightly.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Object variable or with set variable not set.

    @Dave:

    Just like post #4.


    VB Code:
    1. Dim objExcel As Exce.Application
    2. Dim objWorkbook As Excel.Workbook
    3. Dim objSheet As Excel.Worksheet
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Re: Object variable or with set variable not set.

    VB Code:
    1. Function LoadListInExcel() As Boolean
    2. LoadListInExcel= True
    3. Set rstLoadRecord = New ADODB.Recordset
    4. Set xlApp = New Excel.Application
    5. Set xlBook = xlApp.Workbooks.Add
    6. Set xlsheet = xlApp.ActiveSheet
    7. .
    8. .
    9. .
    10. Call setBorder(xlsheet)
    11. Call setColTitleWidth(xlsheet)
    12. End function
    13.  
    14. Private Sub setBorder(worksheet As Excel.worksheet)
    15. With worksheet
    16. .cell...
    17. .range...
    18.  
    19. end with
    20. End Function
    21.  
    22.  
    23. Private Sub setColTitleWidth(worksheet As Excel.worksheet)
    24.  
    25. If lstr <> Trim$(rstLoadRecord![Ready]) Then
    26.                    
    27.  
    28.                    'cut and paste heading
    29.                    .Range("A5:P9").Select
    30.                     [COLOR=Sienna]Selection.Copy[/COLOR]                
    31.                     NextRows = CStr(NextRows + 5)
    32.                    lstrCellCopy = "A" & NextRows
    33.                
    34.                    .Range(lstrCellCopy).Select
    35.                    ActiveSheet.Paste
    36.                    NextRows = CStr(NextRows + 5)
    37.                
    38. End if
    39.  
    40.  
    41.  
    42. End Function

    ...sorry that i miss out those DIM ....Does it got smthing to do with instance..?
    Last edited by RobDog888; Sep 23rd, 2005 at 12:20 AM. Reason: Added vbcode tags

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Object variable or with set variable not set.

    I see. Thanks.

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Object variable or with set variable not set.

    No prob. Anytime Dave


    Goh, you have a block of code here that is not correct.

    VB Code:
    1. If lstr <> Trim$(rstLoadRecord![Ready]) Then
    2.                    
    3.  
    4.                    'cut and paste heading
    5.                    .Range("A5:P9").Select '<---WHERE IS THE WITH BLOCK?
    6.                     Selection.Copy                
    7.                     NextRows = CStr(NextRows + 5)
    8.                    lstrCellCopy = "A" & NextRows
    9.                
    10.                    .Range(lstrCellCopy).Select '<---WHERE IS THE WITH BLOCK?
    11.                    ActiveSheet.Paste
    12.                    NextRows = CStr(NextRows + 5)
    13.                
    14. End if
    And this other procedure you have multiple dots???
    VB Code:
    1. Private Sub setBorder(worksheet As Excel.worksheet)
    2. With worksheet
    3. .cell...  '<-- WHATS UP WITH THE "..." ?
    4. .range...
    5.  
    6. end with
    7. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Re: Object variable or with set variable not set. (Resolved)

    Hi thanks for the reply..so that i have given incomplete codes...but some how it seems there is no problem now..thank you to all will reflect if any more problems.

    Regards
    Alfred

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Re: Object variable or with set variable not set. (resolved)

    hi guys i still got errors at times thanks for any suggestion.

    Function LoadListInExcel() As Boolean
    LoadListInExcel= True
    Set rstLoadRecord = New ADODB.Recordset
    Set xlApp = New Excel.Application
    Set xlBook = xlApp.Workbooks.Add
    Set xlsheet = xlApp.ActiveSheet
    '==================
    ' codes
    '==================
    Call setBorder(xlsheet)
    Call setColTitleWidth(xlsheet)
    End function

    Private Sub setBorder(worksheet As Excel.worksheet)
    With worksheet
    ' .cell ' sorry i did not complete this part of the codes as it works fine here.
    ' .range ' sorry i did not complete this part of the codes as it works fine here.
    '==================
    ' codes
    '==================
    End with
    End Function


    Private Sub setColTitleWidth(worksheet As Excel.worksheet)
    With worksheet
    If lstr <> Trim$(rstLoadRecord![Ready]) Then


    'cut and paste heading
    .Range("A5:P9").Select
    Selection.Copy ' this is the part the error ocurs.
    NextRows = CStr(NextRows + 5)
    lstrCellCopy = "A" & NextRows

    .Range(lstrCellCopy).Select
    ActiveSheet.Paste
    NextRows = CStr(NextRows + 5)

    End if

    End With

    End Function

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Re: Object variable or with set variable not set. (resolved)

    Hi everyone I just found out that the error occurs only when the codes rerun the second time ie consecutively. the first time is ok. Only the second time the error hits. Is there something i need to close or set something as new ? thanks

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    Re: Object variable or with set variable not set. (resolved)

    mm..for the "selection.copy" how do i amend to avoid the error ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width