Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Public Sub AddNewWorkBookTEST()
  4.  
  5. Dim nextline As Long, LastUsedRowList As Long
  6. Dim CodeString As String
  7.  
  8. Dim x As Long
  9. Dim KWATT As Double
  10.  
  11.  
  12. Dim folderPath As String
  13. folderPath = Application.ActiveWorkbook.Path
  14.  
  15. LastUsedRowList = Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
  16.  
  17. For x = 1 To LastUsedRowList
  18. KWATT = Sheet4.Cells(x, 1)
  19. CodeString = CodeStringGenerator(KWATT)
  20.  
  21. ''Update the module code
  22. With ActiveWorkbook.VBProject.VBComponents("MyNewTest").CodeModule
  23. .DeleteLines 1, .CountOfLines
  24. End With
  25.  
  26. With ActiveWorkbook.VBProject.VBComponents("MyNewTest").CodeModule
  27. nextline = .CountOfLines + 1
  28. .InsertLines nextline, CodeString
  29. End With
  30.  
  31. CallOtherModule x
  32. ''Calling the function in the second module (where the code was copied).
  33. '''Cannot call the function directly from this sub, since excel will crash:
  34. Call MyNewTest.SortedArray(x)
  35.  
  36. Next x
  37.  
  38.  
  39. End Sub
  40.  
  41. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  42. Public Sub CallOtherModule(ItemsCounter As Long)
  43. Call MyNewTest.SortedArray(ItemsCounter)
  44. End Sub
  45.  
  46. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  47. '''The function that writes the code of the second module as String
  48. Function CodeStringGenerator(KWATT As Double) As String
  49.  
  50. CodeStringGenerator = "'Option Explicit" & vbCrLf & "Public Function
  51. SortedArray(ItemsCounter As Long) As Variant()" & vbCrLf & vbCrLf _
  52. & "Dim TempSortedArray() As Variant" & vbCrLf _
  53. & "Sheet4.Cells(ItemsCounter, 2) = " & KWATT + 5 & vbCrLf _
  54. & "End Function" & vbCrLf
  55.  
  56. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement