Consulting

Results 1 to 18 of 18

Thread: Run time error 91 object variables not set

  1. #1
    VBAX Regular
    Joined
    Sep 2023
    Posts
    6
    Location

    Unhappy Run time error 91 object variables not set

    Hi,

    I've been trying to figure out what could be wrong with my code. I tried all I can search to update the code seems the same error.

    This are my code

    Sub Consolidate ()
    
    On error GoTo 0
    Dim Destrange as range
    Dim DestWB as workbook
    Dim DestSh as worksheet
    Dim Lr as long
    With application
    . ScreenUpdating = False
    . EnableEvents = False
    End with 
    If bISBookOpen_RB("pathfolder.xlsx" ) Then
     Set DestWB = Workbooks(" pathfolder.xlsx") 
    Else
     Set DestWB = Workbooks.Open("pathfolder.xlsx")
    End If
    Set ws1 = This Workbook.ActiveSheet
    Set rg1 = ws1.Range ("A2:K2") 
    Set DestWB = Workbooks.Open("pathfolder.xlsx")
         Lr = Last(1, DestRange) 
    Set DestRange = DestSh.Range("A" & DestWB.Worksheets("Data").Rows.Count.End(xlUp)
    Set DestSh = Dest.Range("A2") 
    Do until IsEmpty(DestSh) 
        Set DestSh = DestRange.offset(1, 0)
    Loop
    With Rg1
    . Select
    . Copy
    Endwith
    With DestSh 
     DestRange.PasteSpecial _
                Paste:=xlPasteValues, _
                operation:=xlPasteSpecialOperationNone, _
                skipblanks:=False, _
                Transpose:=False
    End With
    Else
    End If
    Exit Sub
    DestWB.Close Savechanges:= True
    With application
    . ScreenUpdating = True
    . EnableEvents = True
    End With
    End Sub
    With this code
    Set DestRange = DestSh.Range("A" & DestWB.Worksheets("Data").Rows.Count.End(xlUp)
    Set DestSh = Dest.Range("A2")
    This two is highlighted in yellow and shows DestRange=Nothing DestSh = nothing

    Your help will be highly appreciated

    Thank you in advance
    Last edited by Paul_Hossler; 09-12-2023 at 04:56 PM.

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Please post code between CODE tags to retain indentation and readability. Can edit your post.

    What line triggers error? I don't see any yellow highlight.

    Could provide file for analysis. Follow instructions at bottom of my post.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. I added CODE tags for you

    2. Welcome to the forum, and please take a minute to read the FAQ at the link in my signature

    3. I'd look at the two lines below

    a. DestSh is not set (2) before you use it in (1)
    b. I'm not sure what 'Dest' is -- it could be the code name for a worksheet, but DestSh is Dim-ed as a worksheet


    Dim Destrange as range
    Dim DestWB as workbook
    Dim DestSh as worksheet
    
    ...........
    
    Set DestWB = Workbooks.Open("pathfolder.xlsx")
    
    .........
    
    Set DestRange = DestSh.Range("A" & DestWB.Worksheets("Data").Rows.Count.End(xlUp)  ' (1)
    Set DestSh = Dest.Range("A2")                                                                                      '  (2)

    Going way out a limb here, but maybe you meant this??


    Set DestWB = Workbooks.Open("pathfolder.xlsx")
    
    .........
    
    Set DestSh = DestWB.Worksheets("Data")
    
    Set DestRange = DestSh.Range("A" & DestSh.Rows.Count.End(xlUp))
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Sep 2023
    Posts
    6
    Location
    Quote Originally Posted by Paul_Hossler View Post
    1. I added CODE tags for you

    2. Welcome to the forum, and please take a minute to read the FAQ at the link in my signature

    3. I'd look at the two lines below

    a. DestSh is not set (2) before you use it in (1)
    b. I'm not sure what 'Dest' is -- it could be the code name for a worksheet, but DestSh is Dim-ed as a worksheet


    Dim Destrange as range
    Dim DestWB as workbook
    Dim DestSh as worksheet
    ...........
    Set DestWB = Workbooks.Open("pathfolder.xlsx")
    .........
    Set DestRange = DestSh.Range("A" & DestWB.Worksheets("Data").Rows.Count.End(xlUp)  ' (1)
    Set DestSh = Dest.Range("A2")                                                                                      '  (2)

    Going way out a limb here, but maybe you meant this??


    Set DestWB = Workbooks.Open("pathfolder.xlsx")
    .........
    Set DestSh = DestWB.Worksheets("Data")
    Set DestRange = DestSh.Range("A" & DestSh.Rows.Count.End(xlUp))

    Hi,

    When I run the code error that I received is run time error 91 object variable or with block variable not set
    Then once I click the debug
    It will highlighted in yellow the

    Set DestRange = DestSh.Range("A" & DestWB.Worksheets("Data").Rows.Count.End(xlUp) ' (1)
    And when I clicked the Set Destrange it shows DestRange = Nothing

    What I'm trying is to run a code saving each row from active sheet to another workbook
    Destsh is Destination sheet were the row from ActiveSheet will be save to this Destination sheet

    DestRange is the Destination Range

    I tried your your recommendation
    Set DestWB = Workbooks.Open("pathfolder.xlsx")
    .........
    Set DestSh = DestWB.Worksheets("Data")
    Set DestRange = DestSh.Range("A" & DestSh.Rows.Count.End(xlUp))
    I still have the same error ��

    Appreciate your response thank you ��
    Last edited by Megan07; 09-12-2023 at 06:00 PM. Reason: Adding additional information

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    On the last line of the suggested code from Paul:
    Set DestRange = DestSh.Range("A" & DestSh.Rows.Count.End(xlUp))
    Try the below instead:
    Set DestRange = DestSh.Range("A" & DestSh.Range("A" & Rows.Count).End(xlUp).Row)
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Wouldn't Rows still need to be qualified? DestSh.Rows.Count
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    VBAX Regular
    Joined
    Sep 2023
    Posts
    6
    Location
    Quote Originally Posted by georgiboy View Post
    On the last line of the suggested code from Paul:
    Set DestRange = DestSh.Range("A" & DestSh.Rows.Count.End(xlUp))
    Try the below instead:
    Set DestRange = DestSh.Range("A" & DestSh.Range("A" & Rows.Count).End(xlUp).Row)
    Hello Georgiboy,

    It' the same error

    I tried to change the code
    Sub copy_column_value_to_another_workbook 
    Dim SourceRange as Range
    Dim DestRange as Range
    Dim DestWB as workbook 
    Dim DestSh as worksheet
    Dim Lr as Long
    With application
        . ScreenUpdating = False
        . EnableEvents = False
    End with 
    If bISBookOpen_RB("pathfolder.xlsx" ) Then
        Set DestWB = Workbooks(" pathfolder.xlsx") 
    Else
        Set DestWB = Workbooks.Open("pathfolder.xlsx")
    End If
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK2")
    Set DestSh = DestWBworksheets("Data")
    Lr = LastRow(DestSh)
    Set DestRange = DestSh.Range("A" & DestWB.worksheets("Data").Rows.Count).End(xlUp).offset(1)
    SourceRange.copy
    DestRange.PasteSpecial _
                Paste:=xlPasteValues, _
                operation:=xlPasteSpecialOperationNone, _
                skipblanks:=False, _
                Transpose:=False
    Application.CutCopyMode = False
    DestWB.Close savechanges:= True
    With application
        . ScreenUpdating = True
        . EnableEvents = True
    End With
    End Sub
    Above code is working, however since I'm trying to save the each rows one by one if other user added on the activesheet which is sheet "Raw" on the Masterfile it only save the A2:AK2 range.

    Tried to change the SourceRange to
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK")

    I have diffrent error run time 1004
    Application-declined or object declined error
    That debug shows Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK")
    Last edited by Aussiebear; 09-13-2023 at 08:03 AM. Reason: Added code tags to supplied code

  8. #8
    VBAX Regular
    Joined
    Sep 2023
    Posts
    6
    Location
    Hello,

    I tried to change the code
    Sub copy_column_value_to_another_workbook
    Dim SourceRange as Range
    Dim DestRange as Range
    Dim DestWB as workbook
    Dim DestSh as worksheet
    Dim Lr as Long
    With application
        . ScreenUpdating = False
        . EnableEvents = False
    End with
    If bISBookOpen_RB("pathfolder.xlsx" ) Then
        Set DestWB = Workbooks(" pathfolder.xlsx")
    Else
        Set DestWB = Workbooks.Open("pathfolder.xlsx")
    End If
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK2")
    Set DestSh = DestWBworksheets("Data")
    Lr = LastRow(DestSh)
    Set DestRange = DestSh.Range("A" & DestWB.worksheets("Data").Rows.Count).End(xlUp).offset(1)
    SourceRange.copy
    DestRange.PasteSpecial _
    Paste:=xlPasteValues, _
    operation:=xlPasteSpecialOperationNone, _
    skipblanks:=False, _
    Transpose:=False
    Application.CutCopyMode = False
    DestWB.Close savechanges:= True
    With application
        . ScreenUpdating = True
        . EnableEvents = True
    End With
    End Sub
    Above code is working, however since I'm trying to save the each rows one by one if other user added on the activesheet which is sheet "Raw" on the Masterfile it only save the A2:AK2 range.

    Tried to change the SourceRange to
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK")

    I have diffrent error run time 1004
    Application-declined or object declined error
    That debug shows Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK")
    Last edited by Aussiebear; 09-13-2023 at 08:05 AM. Reason: Added code tags to supplied code

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Quote Originally Posted by June7 View Post
    Wouldn't Rows still need to be qualified? DestSh.Rows.Count
    Rows.Count would return the same number as DestSh.Rows.Count as the sheets would have the same amount of rows unless we were working with an older XL file.
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Again, please post code between CODE tags.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    VBAX Regular
    Joined
    Sep 2023
    Posts
    6
    Location
    Quote Originally Posted by June7 View Post
    Again, please post code between CODE tags.
    Hello June 7,

    I'm just new using vba not sure what to do. In order for me to have this code I just look here. And helps a lot.
    My challenge now is below. I'm not sure if this is the one you mention abot post code between code tags.

    I tried to change the code
    Sub copy_column_value_to_another_workbook
    Dim SourceRange as Range
    Dim DestRange as Range
    Dim DestWB as workbook
    Dim DestSh as worksheet
    Dim Lr as Long
    With application
        . ScreenUpdating = False
        . EnableEvents = False
    End with
    If bISBookOpen_RB("pathfolder.xlsx" ) Then
        Set DestWB = Workbooks(" pathfolder.xlsx")
    Else
        Set DestWB = Workbooks.Open("pathfolder.xlsx")
    End If
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK2")
    Set DestSh = DestWBworksheets("Data")
    Lr = LastRow(DestSh)
    Set DestRange = DestSh.Range("A" & DestWB.worksheets("Data").Rows.Count).End(xlUp).offset(1)
    SourceRange.copy
    DestRange.PasteSpecial _
    Paste:=xlPasteValues, _
    operation:=xlPasteSpecialOperationNone, _
    skipblanks:=False, _
    Transpose:=False
    Application.CutCopyMode = False
    DestWB.Close savechanges:= True
    With application
        . ScreenUpdating = True
        . EnableEvents = True
    End With
    End Sub
    Above code is working, however since I'm trying to save the each rows one by one if other user added on the activesheet which is sheet "Raw" on the Masterfile it only save the A2:AK2 range.

    Tried to change the SourceRange to
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK")

    I have diffrent error run time 1004
    Application-declined or object declined error
    That debug shows Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK")
    Last edited by Aussiebear; 09-13-2023 at 08:07 AM. Reason: Added code tags to supplied code

  12. #12
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Click # icon from post toolbar to generate CODE tags. Paste your code between the tags.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  13. #13
    VBAX Regular
    Joined
    Sep 2023
    Posts
    6
    Location
    Quote Originally Posted by June7 View Post
    Click # icon from post toolbar to generate CODE tags. Paste your code between the tags.
    Sub copy_column_value_to_another_workbook
    Dim SourceRange as Range
    Dim DestRange as Range
    Dim DestWB as workbook
    Dim DestSh as worksheet
    Dim Lr as Long
    With application
    .ScreenUpdating = False
    . EnableEvents = False
    End with
    If bISBookOpen_RB("pathfolder.xlsx" ) Then
    Set DestWB = Workbooks(" pathfolder.xlsx")
    Else
    Set DestWB = Workbooks.Open("pathfolder.xlsx")
    End If
    Set SourceRange = This workbook.Sheets("Raw").Range("A2:AK2")
    Set DestSh = DestWBworksheets("Data")
    Lr = LastRow(DestSh)
    Set DestRange = DestSh.Range("A" & DestWB.worksheets("Data").Rows.Count).End(xlUp).offset(1)
    SourceRange.copy
    DestRange.PasteSpecial _
    Paste:=xlPasteValues, _
    operation:=xlPasteSpecialOperationNone, _
    skipblanks:=False, _
    Transpose:=False
    Application.CutCopyMode = False
    DestWB.Close savechanges:= True
    With application
    . ScreenUpdating = True
    . EnableEvents = True
    End With
    End Sub
    Hello June7,

    I hope I did it correctly. Thank you
    Last edited by Aussiebear; 09-13-2023 at 04:44 PM. Reason: Corrected the Code tags issue.....

  14. #14
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Crud, I forgot this forum sometimes puts each line of code in its own CODE box. Very annoying. Don't know why it happens nor how to prevent.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  15. #15
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Welcome to VBAX Megan07. When submitting code to this forum, you can you either of two methods. Click the # icon and then write/ insert your code within the code tags or you can write/ insert your code into the post, then highlight the code and then click the # icon.

    @ June7, I've been a member here for nearly 20 years and I cannot recall anything like this before. There were two occasions within the last couple of years where there were two blocks of code tags, but this normally comes about because of actions by the user.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  16. #16
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    Well, I usually click # then paste code between tags. Maybe as you describe is better - paste then highlight and click #.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  17. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Either way works

    When I'm (and AussieBear) adding [CODE] ..... [/CODE] for someone who 'forgot' I usually select the text block(s) and hit the [#] icon

    When I'm adding my own macro i put the [CODE] ..... [/CODE] in first and paste between

    Maybe someone put each line into [CODE] ..... [/CODE] tags ??????
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  18. #18
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    337
    Location
    No, it happens to me often and cannot explain why. I copy code from Access module. Click # in forum. Paste code. And forum splits every line to its own block. I expect that happened to OP here.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Tags for this Thread

Posting Permissions

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