Advertisement
Danila_lipatov

parse_files_com

Jan 10th, 2023 (edited)
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function onlyDigits(s As String) As String
  2.     ' Variables needed (remember to use "option explicit").   '
  3.    Dim retval As String    ' This is the return string.      '
  4.    Dim i As Integer        ' Counter for character position. '
  5.  
  6.     ' Initialise return string to empty                       '
  7.    retval = ""
  8.  
  9.     ' For every character in input string, copy digits to     '
  10.    '   return string.                                        '
  11.    For i = 1 To Len(s)
  12.         If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
  13.             retval = retval + Mid(s, i, 1)
  14.         End If
  15.     Next
  16.  
  17.     ' Then return the return string.                          '
  18.    onlyDigits = retval
  19. End Function
  20.  
  21. Sub test_macros_parse()
  22.     Application.EnableEvents = False
  23.     Application.ScreenUpdating = False
  24.     sFilePath = "path"
  25.    
  26.     k = 7   'get max num of years
  27.    a = 4   ' index using to start writing datas
  28.    
  29.      If Right(sFilePath, 1) <> "\" Then
  30.         sFilePath = sFilePath & "\"
  31.     End If
  32.    
  33.     sFileName = Dir(sFilePath & "*.xls")
  34.    
  35.    
  36.     Workbooks.Open Filename:=sFilePath & sFileName
  37.    
  38.     Debug.Print (sFileName)
  39.    
  40.      Do While Len(sFileName) > 0
  41.         If Right(sFileName, 4) = "xls" Then
  42.        
  43.             Workbooks.Open Filename:=sFilePath & sFileName
  44.             Do
  45.                 'Debug.Print (Workbooks(sFileName).Sheets("sheet").Cells(7, k).Value)
  46.                For i = 8 To 202
  47.            
  48.                     If Workbooks(sFileName).Sheets("sheet").Cells(i, 4).Interior.Color = 6724095 Then
  49.                    
  50.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 1).Value = sFileName
  51.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 4).Value = Workbooks(sFileName).Sheets("sheet").Cells(1, k).Value
  52.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 3).Value = Workbooks(sFileName).Sheets("sheet").Cells(1, 3).Value
  53.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 6).Value = Workbooks(sFileName).Sheets("sheet").Cells(i, k).Value
  54.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 7).Value = Workbooks(sFileName).Sheets("sheet").Cells(i, 4).Value
  55.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 5).Value = Workbooks(sFileName).Sheets("sheet").Cells(4, k).Value
  56.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 9).Value = k
  57.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 8).Value = i
  58.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 10).Value = onlyDigits(Workbooks(sFileName).Sheets("sheet").Cells(2, 1).Value)
  59.                             ThisWorkbook.Sheets("Ëèñò1").Cells(a, 11).Value = Workbooks(sFileName).Sheets("sheet_1").Cells(5, 4).Value
  60.                            
  61.                             'Debug.Print (Workbooks(sFileName).Sheets("sheet").Cells(2, 1).Value)
  62.                            'Debug.Print (Workbooks(sFileName).Sheets("sheet").Cells(5, 4).Value)
  63.                            'Debug.Print (Workbooks(sFileName).Sheets("sheet").Cells(4, k).Value)
  64.                            
  65.                             a = a + 1
  66.                     End If
  67.                
  68.                 Next i
  69.                 k = k + 1
  70.             Loop While Workbooks(sFileName).Sheets("sheet").Cells(7, k).Value <> ""
  71.                
  72.                
  73.            
  74.             Workbooks(sFileName).Close
  75.         End If
  76.        
  77.          k = 7
  78.          sFileName = Dir
  79.     Loop
  80.     Application.ScreenUpdating = True
  81.     Application.EnableEvents = True
  82.    
  83.    
  84. End Sub
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement