Advertisement
ExcelStore

Модуль iFunction

Jan 14th, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Public CN As ADODB.Connection, RS As ADODB.Recordset
  3.  
  4.  
  5.  
  6. Function SearchData(ByVal iSTR As Variant) As Long
  7.     On Error Resume Next
  8.     SearchData = 0: If VBA.Trim(iSTR) = "" Then Exit Function
  9.     SearchData = WorksheetFunction.Match(iSTR, Sheets("сводная").Range("G:G").Value, 0)
  10. End Function
  11.  
  12.  
  13. Function TransposeArray(ByVal arr As Variant) As Variant
  14.     Dim tempArray As Variant, x As Long, y As Long
  15.     ReDim tempArray(LBound(arr, 2) To UBound(arr, 2), LBound(arr, 1) To UBound(arr, 1))
  16.     For x = LBound(arr, 2) To UBound(arr, 2)
  17.         For y = LBound(arr, 1) To UBound(arr, 1)
  18.             tempArray(x, y) = arr(y, x)
  19.         Next y
  20.     Next x
  21.     TransposeArray = tempArray
  22. End Function
  23.  
  24.  
  25. Function ЦветЗаливки(Ячейка As Range) As Double
  26.     ЦветЗаливки = Ячейка.Interior.Color
  27. End Function
  28.  
  29.  
  30. Function CheckName(ByVal iCol As Integer) As String
  31.     Select Case iCol
  32.         Case 9: CheckName = "СЧЕТ_РСБУ"
  33.         Case 10: CheckName = "СЧЕТ_МСФО"
  34.         Case 11: CheckName = "ДАТА_ДОКУМЕНТА"
  35.         Case 12: CheckName = "СУММА_RUR"
  36.     End Select
  37. End Function
  38.  
  39.  
  40. Sub ConnectOpen()
  41.     Set CN = New ADODB.Connection
  42.     CN.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & """" & Sheets("макрос").Range("ПутькБазе").Value & """" & "; Persist Security Info=False;"
  43.     'CN.Mode = adModeRead            'режим подключения
  44.    CN.CommandTimeout = 600
  45.     CN.Open
  46. End Sub
  47. Sub InsertBase(ByVal iTime As Date, ByVal iUser As String, ByVal iPath As String, ByVal iFileName As String, ByVal s1 As String, ByVal s2 As String, ByVal s3 As String, ByVal s4 As String, ByVal s5 As String)
  48.     Dim iSQL As String
  49.     iSQL = "INSERT INTO LOGS (STIME, USER_NAME, PATH, FILE_NAME, BAN, ORG_NAME, PARAMETER, OLD_VALUE, NEW_VALUE)" & vbCrLf & _
  50.            "VALUES('" & iTime & "', '" & iUser & "', '" & iPath & "', '" & iFileName & "', '" & s1 & "', '" & s2 & "', '" & s3 & "', '" & s4 & "', '" & s5 & "')"
  51.     CN.Execute iSQL
  52. End Sub
  53. Sub SelectHistory()
  54.     Dim iArray() As Variant, iSQL As String, iBAN As String, iParameter As String
  55.     On Error Resume Next: Err.Clear
  56.    
  57.     If ActiveSheet.Name = "сводная" And (ActiveCell.Column >= 9 And ActiveCell.Column <= 12) Then
  58.         iBAN = ActiveSheet.Cells(ActiveCell.Row, 7).Value: iParameter = ActiveSheet.Cells(1, ActiveCell.Column).Value
  59.        
  60.         Call ConnectOpen: Set RS = New ADODB.Recordset
  61.        
  62.         iSQL = "select * from LOGS where BAN = '" & iBAN & "' and PARAMETER = '" & iParameter & "'"
  63.         RS.Open iSQL, CN
  64.         iArray = RS.GetRows
  65.        
  66.         RS.Close: Set RS = Nothing
  67.         CN.Close: Set CN = Nothing
  68.        
  69.         If Err.Number <> 0 Then
  70.             MsgBox "По указанной ячейке история отсутствует.", vbInformation: Exit Sub
  71.         End If
  72.        
  73.         iArray = TransposeArray(iArray)
  74.         With Sheets("история ячейки")
  75.             If .FilterMode = True Then .ShowAllData: .AutoFilter.Sort.SortFields.Clear
  76.             .Cells.ClearContents
  77.             .Range(.Cells(2, 1), .Cells(UBound(iArray, 1) + 2, 9)).Value = iArray
  78.             .Range("A1:I1") = Array("STIME", "USER_NAME", "PATH", "FILE_NAME", "BAN", "ORG_NAME", "PARAMETER", "OLD_VALUE", "NEW_VALUE")
  79.             If .AutoFilterMode = False Then .Range("A1").AutoFilter
  80.             .Activate: .Cells(1, 10).Activate
  81.         End With
  82.     Else
  83.         MsgBox "Выбранная ячейка должна располагаться в диапазоне столбцов: от I до L (включительно).", vbInformation
  84.     End If
  85. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement