-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv
334 lines (245 loc) · 9.25 KB
/
v
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
'add below string to cells A1
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
Sub get_your_json_value()
your_json_string = [a1].Value
'--------------------use below script if excel is 32-bit and remove blow one---------------------------------------
Dim ScriptControl As New MSScriptControl.ScriptControl
ScriptControl.Language = "JavaScript"
ScriptControl.AddCode ("var query = " & your_json_string)
'--------------------use below script if excel is 64-bit and remove above one---------------------------------------
'Dim ScriptControl As Object
'Set ScriptControl = CreateObjectx86("MSScriptControl.ScriptControl")
ScriptControl.Language = "JavaScript"
ScriptControl.AddCode ("var query = " & your_json_string)
'------get item from json--------
var_id = ScriptControl.Eval("query.menu.id")
var_value = ScriptControl.Eval("query.menu.value")
'------loop all menuitem--------
For i = 0 To ScriptControl.Eval("query.menu.popup.menuitem.length") - 1
i_value = ScriptControl.Eval("query.menu.popup.menuitem[" & i & "].value")
i_onclick = ScriptControl.Eval("query.menu.popup.menuitem[" & i & "].onclick")
Debug.Print i_value, i_onclick
Next i
Debug.Print var_id, var_value
Set ScriptControl = Nothing
End Sub
'--------------------add below script if excel is 64-bit---------------------------------------
Function CreateObjectx86(Optional sProgID, Optional bClose = False)
Static oWnd As Object
Dim bRunning As Boolean
#If Win64 Then
bRunning = InStr(TypeName(oWnd), "HTMLWindow") > 0
If bClose Then
If bRunning Then oWnd.Close
Exit Function
End If
If Not bRunning Then
Set oWnd = CreateWindow()
oWnd.execScript "Function CreateObjectx86(sProgID): Set CreateObjectx86 = CreateObject(sProgID): End Function", "VBScript"
End If
Set CreateObjectx86 = oWnd.CreateObjectx86(sProgID)
#Else
Set CreateObjectx86 = CreateObject("MSScriptControl.ScriptControl")
#End If
End Function
Function CreateWindow()
Dim sSignature, oShellWnd, oProc
On Error Resume Next
sSignature = Left(CreateObject("Scriptlet.TypeLib").GUID, 38)
CreateObject("WScript.Shell").Run "%systemroot%\syswow64\mshta.exe about:""about:<head><script>moveTo(-32000,-32000);document.title='x86Host'</script><hta:application showintaskbar=no /><object id='shell' classid='clsid:8856F961-340A-11D0-A96B-00C04FD705A2'><param name=RegisterAsBrowser value=1></object><script>shell.putproperty('" & sSignature & "',document.parentWindow);</script></head>""", 0, False
Do
For Each oShellWnd In CreateObject("Shell.Application").Windows
Set CreateWindow = oShellWnd.GetProperty(sSignature)
If Err.Number = 0 Then Exit Function
Err.Clear
Next
Loop
End Function
Sheets.Add(after:=Sheets(1)).Name = "NH Answer"
control As IRibbonControl
Sub GRABINFO()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Dim path As String, filename As String
Dim st As Worksheet, sheet2 As Worksheet
Dim i As Integer
Dim lr As Long, lc As Long, LastRow As Long, wn As String
wn = Workbooks(Workbooks.Count).Name
Dim wkb As Workbook
Set wkb = Workbooks(wn)
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a location containing the data file"
.Show
If .SelectedItems.Count = 0 Then
Exit Sub
Else
path = .SelectedItems(1) & "\"
End If
End With
filename = Dir(path & "*.xlsx")
Do While filename <> ""
Workbooks.Open filename:=path & filename
Dim wb As Workbook
Set wb = Workbooks(filename)
If wb.Sheets.Count = wkb.Sheets.Count Then
For i = 1 To ThisWorkbook.Sheets.Count
Set st = wb.Worksheets(i)
Set sht = wkb.Worksheets(i)
lr = st.Cells(st.Rows.Count, "c").End(xlUp).Row
lc = st.Cells(1, st.Columns.Count).End(xlToLeft).Column
LastRow = sht.Cells(sht.Rows.Count, "c").End(xlUp).Row + 1
st.Cells(2, 1).Resize(lr, lc).Copy
sht.Activate
wkb.Worksheets(i).Cells(LastRow, 1).Select
ActiveSheet.Paste
Next i
Workbooks(filename).Close
filename = Dir()
ElseIf wb.Sheets.Count <> wkb.Sheets.Count Then
Workbooks(filename).Close
MsgBox "The tab# are not match to your target file. Please check the templete you are using."
Exit Do
Workbooks(filename).Close
End If
Loop
End Sub
Sub hashmap()
Dim GI_dic As Object
Set GI_dic = CreateObject("Scripting.Dictionary") 'Task Code: [ Group Name, Need to Email to Third Party, Employee Name(Main Contact), Contact Information ]
Dim temp_list As Object
For i = 5 To 11
Set temp_list = CreateObject("Scripting.Dictionary")
temp_list.Add "# of Tasks", Cells(i, "c").Value
temp_list.Add "# of Errors", Cells(i, "d").Value
temp_list.Add "# of Answers", Cells(i, "e").Value
temp_list.Add "Error Ratio", Cells(i, "f").Value
temp_list.Add "Accuracy Ratio", Cells(i, "g").Value
GI_dic.Add Cells(i, "b").Value, temp_list
Set temp_list = Nothing
Next i
Dim tgt As Object
For r = 5 To 11
ee_Name = Cells(r, 2).Value
Set tgt = GI_dic(ee_Name)
'============ÅжÏÌõ¼þ£¨±ÈÈçÖ»Òª95%ÒÔÉϵģ©==============
If tgt("Accuracy Ratio") > 0.95 Then
k = tgt.keys
v = tgt.Items
For i = 0 To tgt.Count - 1
Debug.Print ee_Name, k(i), v(i)
Next i
End If
Next r
End Sub
Sub CreateFolderIfNotExists(folderPath As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Recursively create the folder if it does not exist
If Not fso.FolderExists(folderPath) Then
Call CreateParentFolder(fso.GetParentFolderName(folderPath))
fso.CreateFolder folderPath
End If
End Sub
Sub CreateParentFolder(parentFolderPath As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Check if the parent folder exists
If Not fso.FolderExists(parentFolderPath) Then
' Recursively create the parent folder
CreateParentFolder fso.GetParentFolderName(parentFolderPath)
' Create the current parent folder
fso.CreateFolder parentFolderPath
End If
End Sub
Sub Test()
Dim path As String
path = "C:\Your\Path\Here" ' Change to your desired path
Call CreateFolderIfNotExists(path)
End Sub
Sub MoveAndRenameFile(sourceFilePath As String, destinationFilePath As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
' Check if the source file exists
If fso.FileExists(sourceFilePath) Then
' Move and rename the file
fso.MoveFile Source:=sourceFilePath, Destination:=destinationFilePath
Else
MsgBox "File not found: " & sourceFilePath
End If
End Sub
Function ExtractDetailsFromFileName(fileName As String) As Variant
Dim regex As Object
Dim matches As Object
Dim accountName As String
Dim date1 As String
Dim date2 As String
Set regex = CreateObject("VBScript.RegExp")
' Regex pattern to match your file name formats
' Pattern explanation:
' - Captures any characters between "for " and the first hyphen as account name
' - Then captures two date patterns in d-m-yyyy format
regex.Pattern = "Report for (.+?)- (\d{1,2}-\d{1,2}-\d{4})-(\d{1,2}-\d{1,2}-\d{4})"
regex.Global = False
regex.IgnoreCase = True
Set matches = regex.Execute(fileName)
If matches.Count = 1 Then
accountName = matches(0).SubMatches(0)
date1 = matches(0).SubMatches(1)
date2 = matches(0).SubMatches(2)
ExtractDetailsFromFileName = Array(accountName, date1, date2)
Else
ExtractDetailsFromFileName = Array("Invalid format", "", "")
End If
End Function
Sub TestExtractDetails()
Dim fileName As String
Dim details As Variant
' Example file name
fileName = "Birthday Report for XYZ Corp- 1-2-2023- 3-4-2023.xlsx"
' Call the function
details = ExtractDetailsFromFileName(fileName)
' Print the results
If details(0) <> "Invalid format" Then
Debug.Print "Account Name: " & details(0)
Debug.Print "Date 1: " & details(1)
Debug.Print "Date 2: " & details(2)
Else
Debug.Print "File name format not recognized."
End If
End Sub
Sub ListAllFiles()
Dim FileSystem As Object
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Dim startFolder As String
Dim sheet As Worksheet
Dim rowNumber As Long
startFolder = "C:\Users\mason_wang\Desktop\JN\test pool\projects\Done - Project - AHDCC Mockup"
Set sheet = ThisWorkbook.Sheets("Sheet1")
rowNumber = 1
ListFilesRecursive startFolder, FileSystem, sheet, rowNumber
End Sub
Private Sub ListFilesRecursive(ByVal folderPath As String, ByRef FileSystem As Object, ByRef sheet As Worksheet, ByRef rowNumber As Long)
Dim folder As Object
Set folder = FileSystem.GetFolder(folderPath)
Dim subFolder As Object
Dim file As Object
For Each file In folder.Files
sheet.Cells(rowNumber, 1).Value = file.Path
sheet.Cells(rowNumber, 2).Value = file.Name
rowNumber = rowNumber + 1
Next file
For Each subFolder In folder.SubFolders
ListFilesRecursive subFolder.Path, FileSystem, sheet, rowNumber
Next subFolder
End Sub