Advertisement
Guest User

VBA to help write functions for variables

a guest
Sep 5th, 2023
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 7.88 KB | Source Code | 0 0
  1. Sub test_ExtractAndGenerateStringFunctionsFromFile()
  2. WriteBlankFunctions "String", "C:\Users\" & Environ("UserName") & "\Documents\TestTextFiles\MyCode.txt"
  3. End Sub
  4. Sub test_ZeroOutStringVariables()
  5. ZeroOutStringVariables "String", "C:\Users\" & Environ("UserName") & "\Documents\TestTextFiles\MyCode.txt"
  6. End Sub
  7. Sub ZeroOutStringVariables(DimType As String, FilePathToSourceCode As String)
  8. ' DimType = string paramerter (only works with String fo now) that identifies the declaration/variable type to extract
  9. ' FilePathToSourceCode = file path a a copy and paste of your VBA subs, from which it will extract the names of the DimType's
  10. ' Output: A text file with pre-built sub listing all string variable set to = ""
  11. ' Written to aid in making sure all string variables get reset to an empty string
  12.    Dim FilePath As String
  13.     Dim fileNum As Integer
  14.     Dim FileContent As String
  15.     Dim Lines() As String
  16.     Dim aStringVarNames() As String
  17.     Dim Line As String
  18.     Dim StringVarNames As String
  19.     Dim i As Integer
  20.     Dim l As Integer
  21.  
  22.     ' Set the path to a file where you have pasted your VBA subs
  23.    FilePath = FilePathToSourceCode
  24.    
  25.     ' Open/read file to string -->FileContent
  26.    fileNum = FreeFile
  27.     Open FilePath For Input As fileNum
  28.     FileContent = Input$(LOF(fileNum), fileNum)
  29.     Close fileNum
  30.  
  31.     ' Split by line
  32.    Lines = Split(FileContent, vbCrLf)
  33.     ' Get Len so we can use it in the RIGHT() function within the loop
  34.    l = Len(DimType) + 3
  35.    
  36.     ' Loop through lines & extract variable declarations
  37.    For i = LBound(Lines) To UBound(Lines)
  38.         Line = Trim(Lines(i))
  39.         If Right(Line, l) = "As " & DimType Then
  40.             Debug.Print Line
  41.             ' Extract the variable name (assumes "Dim variableName As DimType" format)
  42.            Dim parts() As String
  43.             parts = Split(Line, " ")
  44.             If UBound(parts) = 3 Then
  45.                 Dim variableName As String
  46.                 variableName = parts(1)
  47.                 If StringVarNames <> "" Then
  48.                     StringVarNames = StringVarNames & "," & variableName
  49.                 Else
  50.                     StringVarNames = variableName
  51.                 End If
  52.             End If
  53.         End If
  54.     Next i
  55.     Debug.Print StringVarNames
  56.     aStringVarNames = Split(StringVarNames, ",")
  57.     aStringVarNames = RemoveDuplicatesFromString(StringVarNames)
  58.  
  59.     ' Check if the variableArray is empty
  60.    If Not IsArrayEmpty(aStringVarNames) Then
  61.         On Error GoTo ErrorHandler
  62.         ' Open the text file for writing
  63.        Dim fileName As String
  64.         fileName = "C:\Users\" & Environ("UserName") & "\Downloads\ZeroOut_" & DimType & ".txt"
  65.         fileNum = FreeFile
  66.         Open fileName For Output As fileNum
  67.             Print #fileNum, "Sub ZeroOutStringVariables()"
  68.         ' Loop through the variableArray and generate variable assignments
  69.        For i = LBound(aStringVarNames) To UBound(aStringVarNames)
  70.             ' Assign an empty string to the variable
  71. '            Dim variableName As String
  72.            variableName = aStringVarNames(i)
  73.             Print #fileNum, "    " & variableName & " = " & """" & """" ' Assign empty string
  74.        Next i
  75.             Print #fileNum, "End Sub"
  76.         ' Close the text file
  77.        Close fileNum
  78.     End If
  79.    
  80.     Application.FollowHyperlink fileName
  81.    
  82.     Exit Sub
  83.  
  84. ErrorHandler:
  85.     MsgBox "Error: " & Err.Description, vbCritical
  86.     Close fileNum
  87. End Sub
  88. Sub WriteBlankFunctions(DimType As String, FilePathToSourceCode As String)
  89. ' DimType = string paramerter (String, Long, Integer) that identifies the declaration/variable type to extract
  90. ' FilePathToSourceCode = file path a a copy and paste of your VBA subs, from which it will extract the names of the DimType's
  91. ' Output: A text file with pre-built (empty) functions
  92. ' Written to aid in "function-izing" my code instead of building huge subs that do too much
  93.  
  94.     Dim FilePath As String
  95.     Dim fileNum As Integer
  96.     Dim FileContent As String
  97.     Dim Lines() As String
  98.     Dim aStringVarNames() As String
  99.     Dim Line As String
  100.     Dim StringVarNames As String
  101.     Dim i As Integer
  102.     Dim l As Integer
  103.  
  104.     ' Set the path to a file where you have pasted your VBA subs
  105.    FilePath = FilePathToSourceCode
  106.     ' Open/read file to string -->FileContent
  107.    fileNum = FreeFile
  108.     Open FilePath For Input As fileNum
  109.     FileContent = Input$(LOF(fileNum), fileNum)
  110.     Close fileNum
  111.  
  112.     ' Split by line
  113.    Lines = Split(FileContent, vbCrLf)
  114.     ' Get Len so we can use it in the RIGHT() function within the loop
  115.    l = Len(DimType) + 3
  116.    
  117.     ' Loop through lines & extract variable declarations
  118.    For i = LBound(Lines) To UBound(Lines)
  119.         Line = Trim(Lines(i))
  120.         If Right(Line, l) = "As " & DimType Then
  121.             Debug.Print Line
  122.             ' Extract the variable name (assumes "Dim variableName As DimType" format)
  123.            Dim parts() As String
  124.             parts = Split(Line, " ")
  125.             If UBound(parts) = 3 Then
  126.                 Dim variableName As String
  127.                 variableName = parts(1)
  128.                 If StringVarNames <> "" Then
  129.                     StringVarNames = StringVarNames & "," & variableName
  130.                 Else
  131.                     StringVarNames = variableName
  132.                 End If
  133.             End If
  134.         End If
  135.     Next i
  136.     Debug.Print StringVarNames
  137.     aStringVarNames = Split(StringVarNames, ",")
  138.     aStringVarNames = RemoveDuplicatesFromString(StringVarNames)
  139.  
  140.    
  141.  
  142.     ' Check if the variableArray is empty
  143.    If Not IsArrayEmpty(aStringVarNames) Then
  144.         On Error GoTo ErrorHandler
  145.         ' Open the text file for writing
  146.        Dim fileName As String
  147.         fileName = "C:\Users\" & Environ("UserName") & "\Downloads\BlankFunctions_" & DimType & ".txt"
  148.         fileNum = FreeFile
  149.         Open fileName For Output As fileNum
  150.  
  151.         ' Loop through the variableArray and generate functions
  152.        For i = LBound(aStringVarNames) To UBound(aStringVarNames)
  153.             ' Construct the function name and header
  154.            Dim functionName As String
  155.             functionName = "Get_" & Trim(aStringVarNames(i))
  156.  
  157.             ' Write the function header to the file
  158.            Print #fileNum, "Function " & functionName & "(str As String) As " & DimType
  159.             Print #fileNum, "    ' Function code here"
  160.             Print #fileNum, "End Function"
  161.             Print #fileNum, ""  '
  162.        Next i
  163.  
  164.         ' Close the text file
  165.        Close fileNum
  166.     End If
  167.    
  168.     Application.FollowHyperlink fileName
  169.    
  170.     Exit Sub
  171.  
  172.  
  173. ErrorHandler:
  174.     MsgBox "Error: " & Err.Description, vbCritical
  175.     Close fileNum
  176. End Sub
  177. Function IsArrayEmpty(arr As Variant) As Boolean
  178.     On Error Resume Next
  179.     IsArrayEmpty = (UBound(arr) < LBound(arr))
  180.     On Error GoTo 0
  181. End Function
  182. Function RemoveDuplicatesFromString(ByVal inputString As String) As Variant
  183.     ' Declare a dictionary object to store unique values
  184.    Dim dict As Object
  185.     Set dict = CreateObject("Scripting.Dictionary")
  186.  
  187.     ' Split the input string into an array
  188.    Dim inputArray() As String
  189.     inputArray = Split(inputString, ",")
  190.  
  191.     ' Iterate through the array and add unique values to the dictionary
  192.    Dim i As Long
  193.     For i = LBound(inputArray) To UBound(inputArray)
  194.         ' Use the item as the key in the dictionary
  195.        ' This automatically handles duplicates by overwriting them
  196.        dict(inputArray(i)) = inputArray(i)
  197.     Next i
  198.  
  199.     ' Create a new array to store the unique values
  200.    Dim uniqueArray() As String
  201.     ReDim uniqueArray(0 To dict.Count - 1)
  202.  
  203.     ' Copy the unique values from the dictionary to the new array
  204.    Dim key As Variant
  205.     i = 0
  206.     For Each key In dict.Keys
  207.         uniqueArray(i) = dict(key)
  208.         i = i + 1
  209.     Next key
  210.  
  211.     ' Return the uniqueArray
  212.    RemoveDuplicatesFromString = uniqueArray
  213. End Function
  214.  
  215.  
Tags: vba
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement