Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. Sub NRI_compile()
  2. 'be sure to have raw sheet active
  3.  
  4. Dim NRI_arr(1 To 18) As Variant
  5. Dim rng As Range
  6. Dim captured As Boolean
  7. Dim IDS_table_width As Long, pasteOffset As Long
  8. Dim IDsheet As Worksheet
  9. Dim lookupRange As Range
  10. Dim IDtableSheet As Worksheet
  11. Dim datasheet As Worksheet
  12. Dim startSheet As Worksheet
  13. Set IDtableSheet = Sheets("IDS")
  14.  
  15. 'find current sheet and active other sheet
  16. Set startSheet = Sheets(ActiveSheet.Name)
  17. Set datasheet = Sheets(InputBox("Name of sheet where data is stored?"))
  18. datasheet.Activate
  19. searchColumns = ActiveSheet.UsedRange.Columns.Count
  20. searchrows = ActiveSheet.UsedRange.Rows.Count
  21.  
  22. 'first determine placement of array output in IDS table
  23. IDS_table_width = Range("ids[#all]").Columns.Count
  24. pasteOffset = IDS_table_width - 2
  25.  
  26. 'add variable headers
  27. For i = 1 To 18
  28. IDtableSheet.Cells(1, IDS_table_width + i) = "NRI" & i
  29. Next i
  30.  
  31. 'begin search loop and build data array
  32. For Each rng In Range("ids[T1_ID (if participant)]")
  33. If rng <> "" Then
  34. On Error GoTo Handler1
  35. rowNum = WorksheetFunction.Match(rng.Value, Range(Cells(1, 1), Cells(searchrows, 1)), 0)
  36. On Error GoTo 0
  37. For i = 1 To searchColumns
  38. Set lookupRange = Cells(rowNum, i)
  39. 'search for beginning of PGN variables and determine which set to pull data from
  40. If Mid(Cells(1, i), 7, 3) = "NRI" Then
  41. If Cells(1, i).Offset(2, 0) Like "How close is * to you?" Then
  42. Range(Cells(1, i), Cells(searchrows, i + 29)).Replace What:="#NULL!", Replacement:="", LookAt:=xlPart, _
  43. SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
  44. ReplaceFormat:=False
  45. If WorksheetFunction.CountA(Range(Cells(1, i), Cells(1, i + 1)).Offset(rowNum - 1, 0)) <> 0 Then
  46. 'find initial variable and insert into array
  47. NRI_arr(1) = WorksheetFunction.Max(Range(Cells(1, i), Cells(1, i + 1)).Offset(rowNum - 1, 0))
  48. 'find second and third variables and insert into array
  49. For Z = 2 To 3
  50. NRI_arr(Z) = Cells(1, i + Z).Offset(rowNum - 1, 0)
  51. Next Z
  52. 'bind remaining variables to array
  53. If WorksheetFunction.CountA(Range(Cells(1, i).Offset(rowNum - 1, 4), Cells(1, i).Offset(rowNum - 1, 18))) <> 0 Then
  54. For x = 1 To 15
  55. NRI_arr(x + 3) = Cells(1, i).Offset(rowNum - 1, 4 + x - 1)
  56. Next x
  57. ElseIf WorksheetFunction.CountA(Range(Cells(1, i).Offset(rowNum - 1, 4 + 15), Cells(1, i).Offset(rowNum - 1, 18 + 15))) <> 0 Then
  58. For x = 1 To 15
  59. NRI_arr(x + 3) = Cells(1, i).Offset(rowNum - 1, 19 + x - 1)
  60. Next x
  61. End If
  62. 'set flag to skip second array calc, already found data
  63. captured = True
  64. End If
  65. 'extract array to IDS table, exit loop if data found and extracted
  66. For y = 1 To 18
  67. rng.Offset(0, pasteOffset + y - 1) = NRI_arr(y)
  68. Next y
  69. If captured = True Then Exit For
  70. End If
  71. End If
  72. 'delete array before continuing with next ID, reset flags
  73. captured = False
  74. Erase NRI_arr
  75. Next
  76. End If
  77.  
  78. Nextloop:
  79. Next rng
  80.  
  81. 'Return to previous functionality
  82. startSheet.Activate
  83. Exit Sub
  84.  
  85. Handler1:
  86. On Error GoTo 0
  87. Resume Nextloop
  88.  
  89. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement