Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.25 KB | None | 0 0
  1. Materials Person1 Person2
  2. --------- --------- ---------
  3. 563718 20 40
  4. 837563 15 35
  5.  
  6. Person Materials Data
  7. --------- --------- ---------
  8. Person1 563718 20
  9. Person1 837563 15
  10. Person2 563718 40
  11. Person2 837563 35
  12.  
  13. Option Explicit
  14.  
  15. Sub MatrixConverter2_3()
  16.  
  17. '--------------------------------------------------
  18. ' This section declares variables for use in the script
  19.  
  20. Dim book, head, cels, mtrx, dbase, v, UserReady, columnsToCombine, RowName, DefaultRowName, DefaultColName1, DefaultColName2, ColName As String
  21. Dim defaultHeaderRows, defaultHeaderColumns, c, r, selectionCols, ro, col, newro, newcol, rotot, coltot, all, rowz, colz, tot As Long
  22. Dim headers(100) As Variant
  23. Dim dun As Boolean
  24.  
  25.  
  26. '--------------------------------------------------
  27. ' This section sets the script defaults
  28.  
  29. defaultHeaderRows = 1
  30. defaultHeaderColumns = 2
  31.  
  32. DefaultRowName = "MyColumnName"
  33.  
  34. '--------------------------------------------------
  35. ' This section asks about data types, row headers, and column headers
  36.  
  37. UserReady = MsgBox("Have you selected the entire data set (not the column headers) to be converted?", vbYesNoCancel)
  38. If UserReady = vbNo Or UserReady = vbCancel Then GoTo EndMatrixMacro
  39.  
  40. all = MsgBox("Exclude zeros and empty cells?", vbYesNoCancel)
  41. If all = vbCancel Then GoTo EndMatrixMacro
  42.  
  43.  
  44. ' UN-COMMENT THIS SECTION TO ALLOW FOR MULTIPLE HEADER ROWS
  45. rowz = 1
  46. ' rowz = InputBox("How many HEADER ROWS?" & vbNewLine & vbNewLine & "(Usually 1)", "Header Rows & Columns", defaultHeaderRows)
  47. ' If rowz = vbNullString Then GoTo EndMatrixMacro
  48.  
  49. colz = InputBox("How many HEADER COLUMNS?" & vbNewLine & vbNewLine & "(These are the columns on the left side of your data set to preserve as is.)", "Header Rows & Columns", defaultHeaderColumns)
  50. If colz = vbNullString Then GoTo EndMatrixMacro
  51.  
  52.  
  53. '--------------------------------------------------
  54. ' This section allows the user to provide field (column) names for the new spreadsheet
  55.  
  56. selectionCols = Selection.Columns.Count ' get the number of columns in the selection
  57. For r = 1 To selectionCols
  58. headers(r) = Selection.Cells(1, r).Offset(rowOffset:=-1, columnOffset:=0).Value ' save the column headers to use as defaults for user provided names
  59. Next r
  60.  
  61. colz = colz * 1
  62. columnsToCombine = "'" & Selection.Cells(1, colz + 1).Offset(rowOffset:=-1, columnOffset:=0).Value & "' to '" & Selection.Cells(1, selectionCols).Offset(rowOffset:=-1, columnOffset:=0).Value & "'"
  63.  
  64. Dim Arr(20) As Variant
  65. newcol = 1
  66. For r = 1 To rowz
  67. If r = 1 Then RowName = DefaultRowName
  68. Arr(newcol) = InputBox("Field name for the fields/columns to be combined" & vbNewLine & vbNewLine & columnsToCombine, , RowName)
  69. If Arr(newcol) = vbNullString Then GoTo EndMatrixMacro
  70. newcol = newcol + 1
  71. Next
  72. For c = 1 To colz
  73. ColName = headers(c)
  74. Arr(newcol) = InputBox("Field name for column " & c, , ColName)
  75. If Arr(newcol) = vbNullString Then GoTo EndMatrixMacro
  76. newcol = newcol + 1
  77. Next
  78. Arr(newcol) = "Data"
  79. v = newcol
  80.  
  81. '--------------------------------------------------
  82. ' This section creates the new spreadsheet, names it, and color codes the new worksheet tab
  83.  
  84. mtrx = ActiveSheet.Name
  85. Sheets.Add After:=ActiveSheet
  86. dbase = "DB of " & mtrx
  87.  
  88. '--------------------------------------------------
  89. ' If the proposed worksheet name is longer than 28 characters, truncate it to 29 characters.
  90. If Len(dbase) > 28 Then dbase = Left(dbase, 28)
  91.  
  92.  
  93. '--------------------------------------------------
  94. ' This section checks if the proposed worksheet name
  95. ' already exists and appends adds a sequential number
  96. ' to the name
  97. Dim sheetExists As Variant
  98. Dim Sheet As Worksheet
  99. Dim iName As Integer
  100.  
  101. Dim dbaseOld As String
  102. dbaseOld = dbase ' save the original proposed name of the new worksheet
  103.  
  104. iName = 0
  105.  
  106. sheetExists = False
  107. CheckWorksheetNames:
  108.  
  109. For Each Sheet In Worksheets ' loop through every worksheet in the workbook
  110. If dbase = Sheet.Name Then
  111. sheetExists = True
  112. iName = iName + 1
  113. dbase = Left(dbase, Len(dbase) - 1) & " " & iName
  114. GoTo CheckWorksheetNames
  115. ' Exit For
  116. End If
  117. Next Sheet
  118.  
  119.  
  120. '--------------------------------------------------
  121. ' This section notify the user if the proposed
  122. ' worksheet name is already being used and the new
  123. ' worksheet was given an alternate name
  124.  
  125. If sheetExists = True Then
  126. MsgBox "The worksheet '" & dbaseOld & "' already exists. Renaming to '" & dbase & "'."
  127. End If
  128.  
  129.  
  130. '--------------------------------------------------
  131. ' This section creates and names a new worksheet
  132. On Error Resume Next 'Ignore errors
  133. If Sheets("" & Range(dbase) & "") Is Nothing Then ' If the worksheet name doesn't exist
  134. ActiveSheet.Name = dbase ' Rename newly created worksheet
  135. Else
  136. MsgBox "Cannot name the worksheet '" & dbase & "'. A worksheet with that name already exists."
  137. GoTo EndMatrixMacro
  138. End If
  139. On Error GoTo 0 ' Resume normal error handling
  140.  
  141. Sheets(dbase).Tab.ColorIndex = 41 ' color the worksheet tab
  142.  
  143.  
  144. '--------------------------------------------------
  145. ' This section turns off screen and calculation updates so that the script
  146. ' can run faster. Updates are turned back on at the end of the script.
  147. Application.Calculation = xlCalculationManual
  148. Application.ScreenUpdating = False
  149.  
  150.  
  151. '--------------------------------------------------
  152. 'This section determines how many rows and columns the matrix has
  153.  
  154. dun = False
  155. rotot = rowz + 1
  156. Do
  157. If (Sheets(mtrx).Cells(rotot, 1) > 0) Then
  158. rotot = rotot + 1
  159. Else
  160. dun = True
  161. End If
  162. Loop Until dun
  163. rotot = rotot - 1
  164.  
  165. dun = False
  166. coltot = colz + 1
  167. Do
  168. If (Sheets(mtrx).Cells(1, coltot) > 0) Then
  169. coltot = coltot + 1
  170. Else
  171. dun = True
  172. End If
  173. Loop Until dun
  174. coltot = coltot - 1
  175.  
  176.  
  177. '--------------------------------------------------
  178. 'This section writes the new field names to the new spreadsheet
  179.  
  180. For newcol = 1 To v
  181. Sheets(dbase).Cells(1, newcol) = Arr(newcol)
  182. Next
  183.  
  184.  
  185. '--------------------------------------------------
  186. 'This section actually does the conversion
  187.  
  188. tot = 0
  189. newro = 2
  190. For col = (colz + 1) To coltot
  191. For ro = (rowz + 1) To rotot 'the next line determines if data are nonzero
  192. If ((Sheets(mtrx).Cells(ro, col) <> 0) Or (all <> 6)) Then 'DCB modified ">0" to be "<>0" to exclude blank and zero cells
  193. tot = tot + 1
  194. newcol = 1
  195. For r = 1 To rowz 'the next line copies the row headers
  196. Sheets(dbase).Cells(newro, newcol) = Sheets(mtrx).Cells(r, col)
  197. newcol = newcol + 1
  198. Next
  199. For c = 1 To colz 'the next line copies the column headers
  200. Sheets(dbase).Cells(newro, newcol) = Sheets(mtrx).Cells(ro, c)
  201. newcol = newcol + 1
  202. Next 'the next line copies the data
  203. Sheets(dbase).Cells(newro, newcol) = Sheets(mtrx).Cells(ro, col)
  204. newro = newro + 1
  205. End If
  206. Next
  207. Next
  208.  
  209.  
  210. '--------------------------------------------------
  211. 'This section displays a message box with information about the conversion
  212.  
  213. book = "Original matrix = " & ActiveWorkbook.Name & ": " & mtrx & Chr(10)
  214. head = "Matrix with " & rowz & " row headers and " & colz & " column headers" & Chr(10)
  215. cels = tot & " cells of " & ((rotot - rowz) * (coltot - colz)) & " with data"
  216.  
  217.  
  218. '--------------------------------------------------
  219. ' This section turns screen and calculation updates back ON.
  220. Application.Calculation = xlCalculationAutomatic
  221. Application.ScreenUpdating = True
  222.  
  223.  
  224. MsgBox (book & head & cels)
  225.  
  226.  
  227. '--------------------------------------------------
  228. ' This is an end point for the macro
  229.  
  230. EndMatrixMacro:
  231.  
  232. End Sub
  233.  
  234. Dim book, head, cels, mtrx, dbase, v, UserReady, columnsToCombine, RowName, DefaultRowName, DefaultColName1, DefaultColName2, ColName As String
  235. Dim defaultHeaderRows, defaultHeaderColumns, c, r, selectionCols, ro, col, newro, newcol, rotot, coltot, all, rowz, colz, tot As Long
  236.  
  237. '--------------------------------------------------
  238. ' This section declares variables for use in the script
  239.  
  240. Dim book
  241. Dim head
  242. Dim cels
  243. Dim mtrx
  244. Dim dbase
  245. Dim v
  246. Dim UserReady
  247. Dim columnsToCombine
  248. Dim RowName
  249. Dim DefaultRowName
  250. Dim DefaultColName1
  251. Dim DefaultColName2
  252. Dim ColName As String
  253.  
  254. Dim defaultHeaderRows
  255. Dim defaultHeaderColumns
  256. Dim c
  257. Dim r
  258. Dim selectionCols
  259. Dim ro
  260. Dim col
  261. Dim newro
  262. Dim newcol
  263. Dim rotot
  264. Dim coltot
  265. Dim all
  266. Dim rowz
  267. Dim colz
  268. Dim tot As Long
  269.  
  270. Dim headers(100) As Variant
  271. Dim dun As Boolean
  272.  
  273. '--------------------------------------------------
  274. ' This section asks about data types, row headers, and column headers
  275.  
  276. UserReady = MsgBox("Have you selected the entire data set (not the column headers) to be converted?", vbYesNoCancel)
  277. If UserReady = vbNo Or UserReady = vbCancel Then GoTo EndMatrixMacro
  278.  
  279. Dim UserReady As VbMsgBoxResult
  280.  
  281. If MsgBox("Have you selected the entire data set (not the column headers) to be converted?", vbYesNoCancel) <> vbYes Then Exit Sub
  282.  
  283. all = MsgBox("Exclude zeros and empty cells?", vbYesNoCancel)
  284. If all = vbCancel Then GoTo EndMatrixMacro
  285.  
  286. If ((Sheets(mtrx).Cells(ro, col) <> 0) Or (all <> 6)) Then
  287.  
  288. If Sheets(mtrx).Cells(ro, col) <> 0 Or Not IsExcludingZeroAndEmpty Then
  289.  
  290. colz = InputBox("How many HEADER COLUMNS?" & vbNewLine & vbNewLine & "(These are the columns on the left side of your data set to preserve as is.)", "Header Rows & Columns", defaultHeaderColumns)
  291. If colz = vbNullString Then GoTo EndMatrixMacro
  292.  
  293. If StrPtr(colz) = 0 Then Exit Sub
  294.  
  295. colz = colz * 1
  296.  
  297. columnsToCombine = "'" & Selection.Cells(1, colz + 1).Offset(rowOffset:=-1, columnOffset:=0).Value & "' to '" & Selection.Cells(1, selectionCols).Offset(rowOffset:=-1, columnOffset:=0).Value & "'"
  298.  
  299. If Not IsNumeric(colz) Then 'user is playing smartypants
  300.  
  301. '--------------------------------------------------
  302. ' If the proposed worksheet name is longer than 28 characters, truncate it to 29 characters.
  303. If Len(dbase) > 28 Then dbase = Left(dbase, 28)
  304.  
  305. ' Maximum length allowed for a sheet name is 31 characters
  306. If Len(dbase) > 28 Then dbase = Left(dbase, 28)
  307.  
  308. Private Const SHEETNAME_MAXLENGTH As Integer = 28 ' actually it's 31, but we're keeping a little buffer to append a digit if needed
  309.  
  310. If Len(dbase) > SHEETNAME_MAXLENGTH Then dbase = Left(dbase, SHEETNAME_MAXLENGTH)
  311.  
  312. '--------------------------------------------------
  313. ' This section checks if the proposed worksheet name
  314. ' already exists and appends adds a sequential number
  315. ' to the name
  316.  
  317. '--------------------------------------------------
  318. ' This section belongs in its own procedure or function
  319.  
  320. columnsToCombine = "'" & Selection.Cells(1, colz + 1).Offset(rowOffset:=-1, columnOffset:=0).Value & "' to '" & Selection.Cells(1, selectionCols).Offset(rowOffset:=-1, columnOffset:=0).Value & "'"
  321.  
  322. If (Sheets(mtrx).Cells(1, coltot) > 0) Then
  323.  
  324. If Sheets(mtrx).Cells(1, coltot) > 0 Then
  325.  
  326. If ((Sheets(mtrx).Cells(((((1)))), ((coltot)))) > (((0)))) Then
  327.  
  328. mtrx = ActiveSheet.Name
  329.  
  330. If (Sheets(mtrx).Cells(rotot, 1) > 0) Then
  331.  
  332. Dim matrix as Worksheet
  333. Set matrix = ActiveSheet
  334.  
  335. If matrix.Cells(rotot, 1) > 0 Then
  336.  
  337. Sheets.Add After:=ActiveSheet
  338.  
  339. Dim dbase As Worksheet
  340. Set dbase = matrix.Parent.Sheets.Add(After:=matrix)
  341.  
  342. Do
  343. If (Sheets(mtrx).Cells(rotot, 1) > 0) Then
  344. rotot = rotot + 1
  345. Else
  346. dun = True
  347. End If
  348. Loop Until dun
  349.  
  350. With Sheets(mtrx)
  351. Do
  352. If .Cells(rotot, 1) > 0 Then
  353. rotot = rotot + 1
  354. Else
  355. dun = True
  356. End If
  357. Loop Until dun
  358. End With
  359.  
  360. v = newcol
  361.  
  362. For newcol = 1 To v
  363.  
  364. Dim sheetExists As Variant
  365. Dim Sheet As Worksheet
  366. Dim iName As Integer
  367.  
  368. Dim dbaseOld As String
  369. dbaseOld = dbase ' save the original proposed name of the new worksheet
  370.  
  371. iName = 0
  372.  
  373. sheetExists = False
  374. CheckWorksheetNames:
  375.  
  376. For Each Sheet In Worksheets ' loop through every worksheet in the workbook
  377. If dbase = Sheet.Name Then
  378. sheetExists = True
  379. iName = iName + 1
  380. dbase = Left(dbase, Len(dbase) - 1) & " " & iName
  381. GoTo CheckWorksheetNames
  382. ' Exit For
  383. End If
  384. Next Sheet
  385.  
  386. Private Function GetUniqueSheetName(book As Workbook, ByVal proposed As String) As String
  387. Dim existing As New Scripting.Dictionary
  388. Dim sheet As Worksheet
  389.  
  390. For Each sheet In book.Worksheets
  391. existing.Add sheet.Name, vbNull
  392. Next
  393.  
  394. Dim unique As String
  395. unique = proposed
  396. Dim suffix As Long
  397. Do
  398. If Not existing.Exists(unique) Then
  399. GetUniqueSheetName = unique
  400. Exit Function
  401. End If
  402. suffix = suffix + 1
  403. unique = proposed & " " & suffix
  404. Loop
  405. End Function
  406.  
  407. '--------------------------------------------------
  408. 'This section determines how many rows and columns the matrix has
  409.  
  410. With Sheets(mtrx)
  411. 'Number of rows in column A
  412. rotot = .Range("A" & .Rows.Count).End(xlUp).Row
  413. 'Number of columns in row 1
  414. coltot = .Range("XFD" & 1).End(xlToLeft).Column
  415. End With
  416.  
  417. '--------------------------------------------------
  418. 'This section actually does the conversion
  419.  
  420. Dim matixValues As Variant
  421. With Sheets(mtrx)
  422. matixValues = .Range(.Cells(rowz + 1, colz + 1), .Cells(rowtot, coltot)).Value
  423. End With
  424.  
  425. For col = LBound(matixValues, 2) To UBound(matixValues, 2)
  426. For ro = LBound(matixValues, 1) To UBound(matixValues, 1)
  427. If matixValues(ro, col) <> 0 Or all = vbNo Then
  428. '...
  429. End If
  430. Next
  431. Next
  432.  
  433. InputBox("How many HEADER ROWS?" & vbNewLine ...
  434.  
  435. If colz = vbNullString Then GoTo EndMatrixMacro
  436.  
  437. On Error GoTo CleanExit
  438. Dim calcState As XlCalculation
  439.  
  440. calcState = Application.Calculation
  441. Application.Calculation = xlCalculationManual
  442. '...
  443. CleanExit:
  444. Application.Calculation = calcState
  445. End Sub
  446.  
  447. .Offset(rowOffset:=-1, columnOffset:=0)
  448.  
  449. Dim sheetExists As Variant
  450. Dim Sheet As Worksheet
  451. Dim iName As Integer
  452.  
  453. `... nothing to do with iName or sheetExists here.
  454. iName = 0
  455. sheetExists = False
  456.  
  457. Dim foo As Variant
  458. Debug.Print foo = False 'Prints True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement