Advertisement
Danila_lipatov

find_audit

Sep 13th, 2022 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Sh_Exist(wb As Workbook, sName As String) As Boolean
  2.     Dim wsSh As Worksheet
  3.     On Error Resume Next
  4.     Set wsSh = wb.Sheets(sName)
  5.     Sh_Exist = Not wsSh Is Nothing
  6. End Function
  7.  
  8. Sub check()
  9.     Application.EnableEvents = False
  10.     Application.ScreenUpdating = False
  11.     sFilePath = "path"
  12.     checkpath = "path"
  13.    
  14.    
  15.     'Sh_Exist function checks existence of sheet to save datas
  16.    'find_ogrn is finding on Sheet1 everything. If does not find => N/A OGRN
  17.    'find_name is the same as find_ogrn, bit N/A Auditor
  18.    'It's necessary to open book from we should take vars
  19.    'sFileName the files where we try to find our vars
  20.    '
  21.    
  22.    
  23.     Dim wbCheck As Workbook
  24.     Count = 0
  25.    
  26.     'Check for back slash
  27.    
  28.     If Right(sFilePath, 1) <> "\" Then
  29.         sFilePath = sFilePath & "\"
  30.     End If
  31.     If Right(checkpath, 1) <> "\" Then
  32.        checkpath = checkpath & "\"
  33.     End If
  34.    
  35.      
  36.     checkname = Dir(checkpath & "where_find.xlsm")
  37.    
  38.     'Workbooks.Open Filename:="path" & checkname
  39.    sFileName = Dir(sFilePath & "*.xlsx")
  40.    
  41.     Set wbCheck = Workbooks(checkname)
  42.    
  43.     If Not Sh_Exist(wbCheck, "output2") Then
  44.         wbCheck.Sheets.Add(, wbCheck.Sheets(wbCheck.Sheets.Count)).Name = "output2"
  45.     End If
  46.     Sheet_work = "output2"
  47.     Workbooks(checkname).Sheets(Sheet_work).Cells(1, 2).Value = "smth"
  48.     Workbooks(checkname).Sheets(Sheet_work).Cells(1, 1).Value = "File_name"
  49.     Workbooks(checkname).Sheets(Sheet_work).Cells(1, 3).Value = "smth"
  50.     j = 1
  51.     k = 1
  52.     Do While Len(sFileName) > 0
  53.         If Right(sFileName, 4) = "xlsx" Then
  54.             'Display file name in immediate window
  55.            'Debug.Print sFileName
  56.            Workbooks.Open Filename:=sFilePath & sFileName
  57.                     For i = 2 To 4974 'last row of file
  58.                        find_ogrn = Workbooks(checkname).Sheets("Sheet1").Cells(i, 4).Value
  59.                         find_name = Workbooks(checkname).Sheets("Sheet1").Cells(i, 2).Value
  60.                         With Workbooks(sFileName).Sheets("Sheet1").Range("A1:A500")
  61.                             'lastrow forfile with names
  62.                            'Set mycell = .Find(what:=Workbooks(checkname).Worksheet("Sheet1").Cells(i, 2).Value, LookAt:=xlWhole)
  63.                            Set x = .Find(find_ogrn)
  64.                             Set y = .Find(find_name)
  65.                             If Not x Is Nothing Then
  66.                                 j = j + 1
  67.                                 Count = Count + 1
  68.                                 Debug.Print (sFileName & " " & x.Row & " " & find_ogrn & " " & "iter: " & k)
  69.                                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = Workbooks(checkname).Sheets("Sheet1").Cells(i, 4).Value
  70.                                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  71.                                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  72.                             End If
  73.                             If Not y Is Nothing Then
  74.                                 j = j + 1
  75.                                 Count = Count + 1
  76.                                 Debug.Print (sFileName & " " & y.Row & " " & find_name)
  77.                                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = Workbooks(checkname).Sheets("Sheet1").Cells(i, 2).Value
  78.                                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  79.                                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  80.                             End If
  81.                         End With
  82.                     Next i
  83.             'Workbooks(sFileName).Save
  84.            If Count = 1 Then
  85.                 If IsNumeric(Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value) = True Then
  86.                     j = j + 1
  87.                    
  88.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = "N/A smth"
  89.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  90.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  91.                 Else:
  92.                     j = j + 1
  93.                    
  94.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = "N/A smth"
  95.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  96.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  97.                 End If
  98.                
  99.             ElseIf Count = 0 Then
  100.                 j = j + 1
  101.                
  102.                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = "N/A smth"
  103.                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  104.                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  105.                
  106.                 j = j + 1
  107.                
  108.                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = "N/A smth"
  109.                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  110.                 Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  111.                
  112.             ElseIf Count >= 2 Then
  113.                 If Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = Workbooks(checkname).Sheets(Sheet_work).Cells(j - 1, 3).Value And IsNumeric(Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value) = True Then
  114.                
  115.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = "N/A smth"
  116.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  117.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  118.                    
  119.                 ElseIf Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = Workbooks(checkname).Sheets(Sheet_work).Cells(j - 1, 3).Value And IsNumeric(Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value) = False Then
  120.                
  121.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 3).Value = "N/A smth"
  122.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 2).Value = "smth"
  123.                     Workbooks(checkname).Sheets(Sheet_work).Cells(j, 1).Value = sFileName
  124.                 End If
  125.             End If
  126.             Workbooks(sFileName).Close
  127.             k = k + 1
  128.         End If
  129.         'Set the fileName to the next available file
  130.        sFileName = Dir
  131.         Count = 0
  132.         'checkname = Dir
  133.    Loop
  134.    
  135.     Application.ScreenUpdating = True
  136.     Application.EnableEvents = True
  137.  
  138. End Sub
  139.  
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement