Results 1 to 6 of 6

Thread: Looping Issue resolved

Threaded View

  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

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