Results 1 to 6 of 6

Thread: Looping Issue resolved

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    Washington
    Posts
    14

    Resolved Looping Issue resolved

    I'm trying to create a code that will take information from one excel workbook with multiple sheets and put it into another excel workbook on one sheet.
    The problem I'm encountering is when the code gets to the 4th loop in setting the wshtarget I get a run-time error type-mismatch. Below is the code which I'm using, anyone that can offer any help it would be much appriciated.

    VB Code:
    1. Public Sub Name_Match_Click()
    2.  
    3.    'get the current workbook and sheet
    4.     Set wbSource = ActiveWorkbook
    5.     Set wshtSource = wbSource.Worksheets("QCTotals")
    6.    
    7.      'open a external workbook
    8.     Set wbTarget = Workbooks.Open("C:\WINNT\Profiles\nmayer\Desktop\learn excel\IITC Offload Master List.xls")
    9.  
    10.  For z = 1 To 5
    11.  
    12.  If z = 1 Then
    13.     Set wshtTarget = wbTarget.Worksheets("LTI Archive")
    14.     ElseIf z = 2 Then
    15.      Set wshtTarget = wbTarget.Worksheets("SITCO Archive")
    16.     ElseIf z = 3 Then
    17.     Set wshtTarget = wbTarget.Worksheets("THTI Archive")
    18.     ElseIf z = 4 Then
    19.     Set wshtTarget = wbTarget.Worksheets("HHT Archive")
    20.     ElseIf z = 5 Then
    21.     Set wshtTarget = wbTarget.Worksheets("CNI Archive")
    22.  End If
    23.     'get the required data and do some tricky maths with it
    24.    Rowcount = wshtTarget.UsedRange.Rows.Count
    25.  
    26.  
    27. 'defining size of array
    28. ReDim DName(3 To Rowcount)
    29. ReDim Var1(3 To Rowcount)
    30. ReDim SList(3 To Rowcount)
    31.  
    32.  
    33. '_Builds the arrays that contains all Drawing Information
    34. For x = 3 To Rowcount
    35.  
    36.     DName(x) = wshtTarget.Cells(x, 3).Value
    37.     Var1(x) = wshtTarget.Cells(x, 1).Value
    38.     SList(x) = wshtSource.Cells(x, 2).Value
    39.  
    40. Next
    41.  
    42. 'set homebook as active workbook here
    43. For x = 3 To Rowcount
    44.  
    45.     For y = 3 To Rowcount
    46.    
    47.         If UCase(DName(x)) = UCase(SList(y)) Then
    48.             wshtSource.Cells(y, 3).Value = Var1(x)
    49.             y = Rowcount
    50.         End If
    51.     Next
    52.  
    53. Next
    54.  
    55.    'close the external workbook and clear up
    56.    If z = 3 Then
    57.     wbTarget.Close
    58.         Set wshtSource = Nothing
    59.         Set wbSource = Nothing
    60.         Set wshtTarget = Nothing
    61.         Set wbTarget = Nothing
    62.     End If
    63.  Next
    64. End Sub

    Thanks for your help.
    Last edited by elocin_rae; Mar 8th, 2005 at 04:13 PM. Reason: resolved

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

    Re: Looping Issue

    Welcome to the Forums.

    I formatted your code using the vbcode tags so its easier to read.

    Your getting the error on this line - Set wshtTarget = wbTarget.Worksheets("HHT Archive")
    Does the sheet exist and is it spelled correctly?

    Setp through your code checking the values as you go.
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    Washington
    Posts
    14

    Re: Looping Issue

    Yes the sheet exsists and it is spelled correctly, I even changed the sheets around to see if it was that sheet or any sheet and it happened to anysheet that was in the fourth loop.

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

    Re: Looping Issue

    The IITC Offload Master List.xls workbook has how many sheets in it?
    Also, the last functions to be performed are destroying the objects. If you
    are reusing the smae ones there is no need to do this. It should be on the
    outside of the For Next loop.

    You may also check during each iteration the sheet count of
    "wbTarget.Worksheets.Count", just a hunch.
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Location
    Washington
    Posts
    14

    Re: Looping Issue

    Thank you for your help, my last arguement was messing me up since it only went to 3 instead of 5.

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

    Re: Looping Issue

    A second set of eyes always helps.

    Ps, dont forget to Resolve your thread
    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

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