I am doing this simple routine - checking for values that contain prefix IK column A in all sheets except the IK ouput which is the ouput sheet only.

I am having difficulty setting the varaible in the correct way and getting it to work.

Please see comments which explains some of the difficulties.

Can anyone help debug??


VB Code:
  1. Sub IKlist()
  2.  
  3.  
  4. Dim wksSheet As Worksheet
  5. Dim i As Integer
  6. Dim thiscell As String
  7. Dim Filelength As Integer
  8. Dim obook As Workbook
  9. Dim xlapp As Excel.Application
  10. Dim j As Integer
  11.  
  12. ' CREATE OUTPUT SHEET IK Output
  13.  
  14. 'Count Worksheets
  15. sheetcount = Worksheets.Count
  16.  
  17. 'Add worksheets after the last sheet
  18. Worksheets.Add After:=Sheets(sheetcount)
  19.  
  20. 'Count Worksheets
  21. sheetcount = Worksheets.Count
  22.  
  23. 'Name the sheet and add the number of sheets
  24. Worksheets(sheetcount).Name = "IK output"
  25.  
  26. 'set varaibles to work with the active workbook and any sheet except the ouput sheet
  27.  
  28. Set xlapp = Excel.Application    'work with active excel application
  29. Set obook = xlapp.ActiveWorkbook    'work with active workbook - not specific
  30. Set wksSheet = ??????????????  'set wksheet to be any worksheet in the active workbook. This is because I am looping through each worksheet
  31.  
  32. ' Loop through each worksheet in active workbook - not a specific work book- looking for values that contain prefix IK  and copying it to the IK output sheet
  33.  
  34. ' The output sheet  is the ouput sheet only - therefore we should not loop through this sheet - its only used for poulating results
  35.  
  36.  
  37. Filelength = wksSheet.usedrange.Rows.Count
  38.    j = 1
  39. For Each wksSheet In obook.Worksheets
  40.  
  41.     For i = 1 To Filelength
  42.     thiscell = Cells(i, 1)
  43.     If Left(thiscell, 2) = "IK" Then
  44.     Worksheets("IK output").Cells(j, 1).Value = Cells(i, 1).Value
  45.     End If
  46.     j = j + 1
  47.     Next i
  48. Next wksSheet
  49.  
  50.  
  51.  
  52. End Sub