Advertisement
optimussnorr

excel dissection script

Jun 26th, 2021
2,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Set Variables Here
  2. strSheetName = "Sheet1" 'Name of the spreadsheet inside the Excel file to work in
  3. strDeliminationSym = "|" 'Character that will split the input string
  4.  
  5. '===============================================================================================================
  6.  
  7. Function OpenFileDialog(sDir, sFilter, sTitle)
  8.     With Createobject("Scripting.FileSystemObject")
  9.  
  10.         If .FileExists("OUTPUT") Then .DeleteFile("OUTPUT")
  11.  
  12.         CreateObject("WScript.Shell").Run _
  13.             "powershell.exe -command ""& {"& _
  14.                 "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null;"& _
  15.                 "$o = New-Object System.Windows.Forms.OpenFileDialog;"& _
  16.                 "$o.InitialDirectory = '"& sDir &"';"& _
  17.                 "$o.Filter = '"& sFilter &"';"& _
  18.                 "$o.Title = '"& sTitle &"';"& _
  19.                 "$o.ShowDialog() | Out-Null;"& _
  20.                 "$o.filename > OUTPUT"& _
  21.             "}""",0
  22.  
  23.         Do
  24.             WScript.Sleep 100
  25.         Loop While Not .FileExists("OUTPUT")
  26.  
  27.         With .OpenTextFile("OUTPUT", 1, False, -1)
  28.             Do While .AtEndOfStream
  29.                 WScript.Sleep 100
  30.             Loop
  31.             OpenFileDialog = .ReadLine
  32.         End With
  33.  
  34.         .DeleteFile("OUTPUT")
  35.  
  36.     End With
  37. End Function    
  38.  
  39. ExcelFile = OpenFileDialog("", "Excel Files|*.xlsx;*xls", "Select an Excel document")
  40.  
  41. If Len(ExcelFile) = 0 Then
  42.     MsgBox "You need to select an Excel document."
  43.     WScript.Quit
  44. End If
  45.  
  46. Set obj = createobject("Excel.Application")
  47. obj.visible=True
  48. Set obj1 = obj.Workbooks.open(ExcelFile)
  49. Set obj2 = obj1.Worksheets(strSheetName)
  50.  
  51. Found = False
  52. whileCount = 1
  53. Do While Found = False
  54.     rowResult = obj2.Cells(whileCount,1).Value
  55.     whileCount = whileCount + 1
  56.     if Len(rowResult) = 0 Then
  57.         Found = True
  58.         whileCount = whileCount - 2
  59.     End If
  60. Loop
  61.  
  62. for x = 1 to whileCount
  63.     strInput =  obj2.Cells(x,1).Value
  64.     arrContent = Split(strInput, strDeliminationSym)
  65.  
  66.     for i = 1 to UBound(arrContent)
  67.      obj2.Cells(x,i).Value = arrContent(i)
  68.     Next
  69. Next
  70.  
  71. obj1.Close
  72. obj.Quit
  73. Set obj1=Nothing
  74. Set obj2=Nothing
  75. Set obj=Nothing
  76. Set Found = Nothing
  77. Set whileCount = Nothing
  78. Set x = Nothing
  79. Set i = Nothing
  80. Set arrContent = Nothing
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement