-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.bas
131 lines (104 loc) · 4.04 KB
/
Utils.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Option Explicit
Option Private Module
Public Enum SpeedSetting
Normal = 0
Fast = 1
End Enum
Public Sub SetSpeed(ByVal Speed As SpeedSetting, Optional ByVal DisableAlerts As Boolean = False)
With Application
Select Case Speed
Case SpeedSetting.Normal
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.DisplayStatusBar = True
Case SpeedSetting.Fast
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
.DisplayStatusBar = False
End Select
.DisplayAlerts = Not DisableAlerts
End With
End Sub
Public Function Delay(ByVal MilliSeconds As Long) As Variant
Delay = Timer + MilliSeconds / 1000
Do While Timer < Delay: DoEvents: Loop
End Function
Public Sub PasteDataIntoTable(ByVal Data As Variant, ByVal ws As Worksheet, ByVal TableName As String)
Dim j As Long
Dim i As Long
ClearFilters ws
Dim Table As ListObject
Set Table = ws.ListObjects(TableName)
With Table
' Check if the table has any data
If .ListRows.Count > 0 Then
.DataBodyRange.Value2 = vbNullString
End If
' Temporarily disable the Total Row to prevent issues during data insertion
Dim HasTotal As Boolean
If .ShowTotals Then
HasTotal = True
.ShowTotals = False
End If
If TypeName(Data) = "Recordset" Then
If Not Data.EOF And Not Data Is Nothing Then
Data.MoveLast
Dim RecordCount As Long
RecordCount = Data.RecordCount
Data.MoveFirst
.Resize ws.Range(.Range.Cells(1, 1), ws.Cells(.HeaderRowRange.Row + RecordCount, .ListColumns.Count + .Range.Cells(1, 1).Column - 1))
For i = 1 To RecordCount
For j = 1 To Data.Fields.Count
.DataBodyRange.Cells(i, j).Value = Data.Fields(j - 1).Value
Next
Data.MoveNext
Next
End If
ElseIf IsArray(Data) Then
.Resize ws.Range(.Range.Cells(1, 1), ws.Cells(.HeaderRowRange.Row + UBound(Data) - LBound(Data) + 1, .ListColumns.Count + .Range.Cells(1, 1).Column - 1))
' Check if the incoming data is a single row
If LBound(Data) = UBound(Data) Then
If LBound(Data, 2) = 0 Then j = 1
For i = 1 To Table.Range.Columns.Count
.Range(2, i).Value2 = Data(LBound(Data), i - j)
Next
Else
.DataBodyRange.Value = Data
End If
End If
If HasTotal Then
.ShowTotals = True
End If
End With
End Sub
Public Sub ExecuteShellWait(ByVal cmd As String)
Dim Shell As Object
Set Shell = CreateObject("WScript.Shell")
Shell.Run cmd, 0, True
Set Shell = Nothing
End Sub
Private Sub ClearFilters(ByVal ws As Worksheet)
Dim Table As ListObject
For Each Table In ws.ListObjects
With Table
If .ShowAutoFilter Then
If .AutoFilter.FilterMode Then .AutoFilter.ShowAllData
End If
.ShowAutoFilter = False
.Range.AutoFilter
.Sort.SortFields.Clear
End With
Next
End Sub
Public Sub KillProcessAndChildren(ByVal parentId As Long)
Dim wmi As Object
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Dim processes As Object
Set processes = wmi.ExecQuery("SELECT * FROM Win32_Process WHERE ParentProcessId = " & parentId & " OR ProcessId = " & parentId)
Dim proc As Object
For Each proc In processes
proc.Terminate
Next
End Sub