-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpersonal_macro_workbook.vba
11449 lines (10034 loc) · 508 KB
/
personal_macro_workbook.vba
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Option Explicit
Dim highpe() As Variant, ratio() As Variant, bediff() As Variant, strl() As Variant, backSlash As String, wbpath As String, numRun As Integer
Dim j As Integer, k As Integer, q As Integer, p As Integer, n As Integer, iRow As Integer, iCol As Integer, ns As Integer, fileNum As Integer
Dim startR As Integer, endR As Integer, g As Integer, Gnum As Integer, cae As Integer, ncomp As Integer, numXPSFactors As Integer, numAESFactors As Integer
Dim numMajorUnit As Integer, modex As Integer, para As Integer, graphexist As Integer, numData As Long, numChemFactors As Integer
Dim idebug As Integer, spacer As Integer, sftfit As Integer, sftfit2 As Integer, cmp As Integer, scanNum As Integer, numGrant As Integer, modePre As Integer
Dim wb As String, ver As String, TimeCheck As String, strAna As String, direc As String, ElemD As String, Results As String, testMacro As String, strBG0 As String
Dim strSheetDataName As String, strSheetGraphName As String, strSheetFitName As String, strSheetAnaName As String, strBG1 As String, strBG2 As String
Dim strMode As String, strLabel As String, strCasa As String, strAES As String, strchm As String, strErr As String, strErrX As String, ElemX As String, strBG3 As String
Dim sheetData As Worksheet, sheetGraph As Worksheet, sheetFit As Worksheet, sheetAna As Worksheet
Dim dataData As Range, dataKeData As Range, dataIntData As Range, dataBGraph As Range, dataKGraph, dataKeGraph As Range, dataBeGraph As Range
Dim pe As Single, wf As Single, char As Single, off As Single, multi As Single, windowSize As Single, windowRatio As Single
Dim startEk As Single, endEk As Single, startEb As Single, endEb As Single, stepEk As Single, dblMax As Single, dblMin As Single
Dim chkMax As Single, chkMin As Single, gamma As Single, lambda As Single, maxXPSFactor As Single, maxAESFactor As Single
Dim a0 As Single, a1 As Single, a2 As Single, fitLimit As Single, mfp As Single, peX As Single
Sub CLAM2()
ver = "8.50p" ' Version of this code.
If Application.OperatingSystem Like "*Mac*" Then
backSlash = "/"
Else
backSlash = "\"
End If
If backSlash = "/" Then ' location of directory for database for mac (Go from menu with option key, and click library)
direc = "Library" + backSlash + "Group Containers" + backSlash + "UBF8T346G9.Office" + backSlash + "MyExcelFolder" + backSlash + "Data" + backSlash + "hideki" + backSlash + "XPS" + backSlash
Else
' Windows
'direc = "C:" + backSlash + "Users" + backSlash + "Public" + backSlash + "Excel_XPS_macro" + backSlash + "Data" + backSlash + "hideki" + backSlash + "XPS" + backSlash ' this is for BOOTCAMP/Parallels on MacBookAir.
'direc = "E:" + backSlash + "Data" + backSlash + "Hideki" + backSlash + "XPS" + backSlash ' this is for Windows PC with HDD storage.
direc = "D:\Excel_XPS_macro\DATA\hideki\XPS\"
End If
windowSize = 1.3 ' 1 for large, 2 for small display, and so on. Larger number, smaller graph plot.
windowRatio = 4 / 3 ' window width / height, "2/1" for eyes or "4/3" for ppt
ElemD = "C,O" ' Default elements to be shown up in the element analysis.
TimeCheck = "0" ' "yes" to display the progress time, "No" only iteration results in fitting, numeric value to suppress any display.
' (mac Excel should be like "0" to be speedy)
a0 = -0.00044463 ' Undulator parameters for harmonics or
a1 = 1.0975 ' B vs gap equation
a2 = -0.02624 ' B = A0 + A1 * Exp(A2 * gap)
gamma = 1.2 ' An electron energy: GeV
lambda = 6 ' A magnetic period: cm
fitLimit = 500 ' Maximum fit range: eV
modePre = 1 ' Precision mode: 1 quite, 2 moderate, 3 accurate results to be obtained in solver mode 1 GRG Nonlinear
mfp = 0.6 ' Inelastic mean free path formula: E^(mfp), and mfp can be from 0.5 to 0.9.
para = 100 ' position of parameters in the graph sheet with higher version of 6.56.
' the limit of compared spectra depends on (para/3).
spacer = 4 ' spacer between data tables for each parameter in FitRatioAnalysis, but it should be more than 3
sftfit = 10
sftfit2 = 5
Call SheetNameAnalysis
If Len(strErr) > 0 Then Exit Sub
Call TargetDataAnalysis
End Sub
Sub SheetNameAnalysis()
Dim FSO As Object, dt As Integer, C1 As Variant, rng As Range, sh As String, flag As Boolean, strTest As String
If mid$(direc, Len(direc), 1) <> backSlash Then direc = direc & backSlash
direc = Replace(direc, "/", backSlash)
direc = Replace(direc, "*", "")
If backSlash = "/" Then
If StrComp(mid$(direc, 2, 5), "Users", 1) = 0 Then ' /Users/hidekinakajima/ as home folder
Else
direc = GetSpecialFolderPath_MacScript & direc
End If
'CreateFolderinMacOffice2016 ("MyExcelFolder" & backSlash & "Data" & backSlash & "hideki" & backSlash & "XPS" & backSlash)
If FileOrFolderExistsOnMac(direc & "UD.xlsx") = False Then
TimeCheck = MsgBox("Place the database files into the location: " & direc, 4, "No database files exist")
If TimeCheck = 6 Then
End If
End
End If
GoTo DeadInTheWater3
End If
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.DriveExists(mid$(direc, 1, 2)) = False Then
TimeCheck = MsgBox("Drive Not Found in " + mid$(direc, 1, 2) + " !" + vbCrLf + "Change a drive in direc", 0, "Database error")
End
End If
If FSO.FolderExists(direc) Then
If Len(Dir(direc + "UD.xlsx")) = 0 Then
Application.DisplayAlerts = False
Workbooks.Add
Call UDsamples
ActiveWorkbook.SaveAs Filename:=direc & "UD.xlsx", FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
Application.DisplayAlerts = True
End If
Else
If Workbooks.Count = 0 Then
TimeCheck = MsgBox("Database Not Found in " + direc + "!" + vbCrLf + "Would you like to continue and create directory?", 4, "Database error")
If TimeCheck = 6 Then
On Error GoTo DeadInTheWater1
C1 = Split(direc, backSlash)
For q = 1 To UBound(C1) - 1
C1(q) = C1(q - 1) & backSlash & C1(q)
'Debug.Print C1(q)
FSO.CreateFolder C1(q)
Next q
Workbooks.Add
Call UDsamples
ActiveWorkbook.SaveAs Filename:=direc & "UD.xlsx", FileFormat:=xlOpenXMLWorkbook
ActiveWorkbook.Close
Else
End 'Call GetOut
DeadInTheWater1:
MsgBox "A folder could not be created in the following path: " & direc & "." & vbCrLf & "Create directory manually and try again."
End
End If
Else
If Workbooks(1).Name = "PERSONAL.XLSB" Then GoTo DeadInTheWater2
If InStr(1, ActiveSheet.Name, "Fit_") > 0 Then
DeadInTheWater2:
TimeCheck = MsgBox("Database Not Found in " + direc + "!" + vbCrLf + "Would you like to continue?", 4, "Database error")
If TimeCheck = 6 Then
ElemX = ""
On Error GoTo DeadInTheWater1
C1 = Split(direc, backSlash)
For q = 1 To UBound(C1) - 1
C1(q) = C1(q - 1) & backSlash & C1(q)
FSO.CreateFolder C1(q)
Next q
Workbooks.Add
Call UDsamples
ActiveWorkbook.SaveAs Filename:=direc & "UD.xlsx", FileFormat:=51
ActiveWorkbook.Close
Else
End 'Call GetOut
End If
Else
GoTo DeadInTheWater1
End If
End If
End If
Set FSO = Nothing
DeadInTheWater3:
If StrComp(testMacro, "debug", 1) = 0 Then
TimeCheck = 0
End If
Call Initial
If testMacro <> "debug" Then
Application.ScreenUpdating = False
Application.EnableEvents = False
End If
Application.Calculation = xlCalculationAutomatic ' revised for Office 2010
graphexist = 0
sh = ActiveSheet.Name
wbpath = ActiveWorkbook.Path
If InStr(1, sh, "Graph_") > 0 Then
If InStr(1, sh, "Graph_Norm_") > 0 Then
strSheetDataName = "Norm_" & mid$(sh, 12, (Len(sh) - 11))
ElseIf InStr(1, sh, "Graph_Edge_") > 0 Then
strSheetDataName = "Edge_" & mid$(sh, 12, (Len(sh) - 11))
Else
strSheetDataName = mid$(sh, 7, (Len(sh) - 6))
End If
graphexist = 1 ' for trigger for Graph sheet
If IsEmpty(Cells(1, 2).Value) = False Then
If IsNumeric(Cells(1, 2).Value) = True Then
Else
Exit Sub
End If
End If
If IsEmpty(Cells(2, 2).Value) Then
Cells(2, 2).Value = 40
Else
If IsNumeric(Cells(2, 2).Value) = False Then
If StrComp(Cells(2, 2).Value, "MgKa", 1) = 0 Then
Cells(2, 2).Value = 1253.6
ElseIf StrComp(Cells(2, 2).Value, "AlKa", 1) = 0 Then
Cells(2, 2).Value = 1486.6
Else
Cells(2, 2).Value = 40
End If
Else
If Cells(2, 2).Value > 1500 And (Cells(10, 2).Value = "Ke" Or Cells(10, 2).Value = "Be") Then
Cells(2, 2).Value = 1500
End If
End If
End If
pe = Cells(2, 2).Value
highpe(0) = pe
' if it is error in this position, you forgot to delete the Macro1 at the beginning prior to Option Explicit.
If IsEmpty(Cells(2, 3).Value) Then
Cells(2, 3).Value = "eV"
Else
If StrComp(Cells(2, 3).Value, "eV", 1) <> 0 And StrComp(Cells(2, 1).Value, "PE", 1) = 0 Then
Call HigherOrderCheck ' Formula ";79;118.5;158 eV" in C2 cell
End If
End If
If Cells(2, 1).Value = "PE" Then
strl(1) = "Ke"
strl(2) = "Be"
strl(3) = "In"
If IsEmpty(Cells(3, 2).Value) Then
Cells(3, 2).Value = 4
Else
If IsNumeric(Cells(3, 2).Value) = False Then
Cells(3, 2).Value = 4
End If
End If
wf = Cells(3, 2).Value
If IsEmpty(Cells(4, 2).Value) Then
Cells(4, 2).Value = 0
Else
If IsNumeric(Cells(4, 2).Value) = False Then
Cells(4, 2).Value = 0
End If
End If
char = Cells(4, 2).Value
ElseIf Cells(2, 1).Value = "KE shifts" Or Cells(2, 1).Value = "PE shifts" Then ' AES mode for smoothing factor in derivative plot
If Cells(2, 1).Value = "KE shifts" Then
strl(1) = "Ke"
strl(3) = "De"
ElseIf Cells(2, 1).Value = "PE shifts" Then
strl(1) = "Pe"
strl(3) = "De"
End If
If IsEmpty(Cells(3, 2).Value) Then
Cells(3, 2).Value = 4
Else
If IsNumeric(Cells(3, 2).Value) = False Then
Cells(3, 2).Value = 4
ElseIf Cells(3, 2).Value < 1 Then
Cells(3, 2).Value = 4
End If
End If
wf = Cells(3, 2).Value
Else
wf = Cells(3, 2).Value
End If
If IsEmpty(Cells(9, 2).Value) Then
Cells(9, 2).Value = 0
Else
If IsNumeric(Cells(9, 2).Value) = False Then
Cells(9, 2).Value = 0
End If
End If
off = Cells(9, 2).Value
If IsEmpty(Cells(9, 3).Value) Then
Cells(9, 3).Value = 1
Else
If IsNumeric(Cells(9, 3).Value) = False Then
Cells(9, 3).Value = 1
End If
End If
multi = Cells(9, 3).Value
strAna = Cells(10, 3).Value
If Cells(40, para + 9).Value = "Ver." Then
Else
For q = 1 To 1000
If StrComp(Cells(40, q + 9).Value, "Ver.", 1) = 0 Then
Exit For
ElseIf q = 1000 Then
MsgBox "Graph sheet has no parameters to be refereced."
End
End If
Next
para = q
End If
If IsEmpty(Cells(41, para + 12).Value) Then
Cells(41, para + 12).Value = ((Cells(6, 2).Value - Cells(5, 2).Value) / Cells(7, 2).Value) + 1
End If
numData = Cells(41, para + 12).Value
If IsEmpty(Cells(45, para + 10).Value) Then
Cells(45, para + 10).Value = 0
End If
ncomp = Cells(45, para + 10).Value
If StrComp(Cells(51, para + 9).Value, "FALSE", 1) = 0 Then
Cells(51, para + 9).Value = "C,O"
Else
ElemD = Cells(51, para + 9).Value
End If
If StrComp(LCase(mid$(Cells(1, 1).Value, 1, 3)), "exp", 1) = 0 Then
strSheetAnaName = "Exp_" + strSheetDataName
strSheetGraphName = "Graph_" + strSheetDataName
Call ExportCmp("")
ActiveWorkbook.Save
If Len(strErr) > 0 Then Exit Sub
ElseIf StrComp(LCase(Cells(1, 1).Value), "norm", 1) = 0 Or StrComp(LCase(Cells(1, 1).Value), "edge", 1) = 0 Or StrComp(LCase(Cells(1, 1).Value), "extr", 1) = 0 Or StrComp(LCase(Cells(1, 1).Value), "diff", 1) = 0 Then
Call GetNormalize
If Len(strErr) > 0 Then Exit Sub
ElseIf StrComp(LCase(mid$(Cells(1, 1).Value, 1, 4)), "auto", 1) = 0 Or StrComp(LCase(mid$(Cells(1, 1).Value, 1, 6)), "offset", 1) = 0 Then
strSheetGraphName = "Graph_" + strSheetDataName
Call GetAutoScale
If StrComp(mid$(strErr, 1, 3), "err", 1) = 0 Then MsgBox ("Error in range: " & mid$(strErr, 4, Len(strErr) - 3))
ActiveWorkbook.Save
If Len(strErr) > 0 Then Exit Sub
ElseIf StrComp(LCase(mid$(Cells(1, 1).Value, 1, 3)), "leg", 1) = 0 Then
strSheetGraphName = "Graph_" + strSheetDataName
Results = vbNullString
Call CombineLegend
ActiveWorkbook.Save
End
ElseIf StrComp(LCase(Cells(1, 1).Value), "debug", 1) = 0 Then
Cells(1, 1).Value = "Grating"
testMacro = "debugGraph"
Call debugAll
End
ElseIf StrComp(LCase(Cells(1, 1).Value), "debugn", 1) = 0 Then
Cells(1, 1).Value = "Grating"
testMacro = "debugGraphn"
Call debugAll
End
End If
For k = 0 To CInt(para / 3)
If StrComp(Cells(1, (4 + (3 * k))).Value, "comp", 1) = 0 Then Exit For
Next
If k >= CInt(para / 3) Then
cmp = -1
Else
cmp = k ' position of comp if cmp < ncomp
End If ' "cmp" should not be used because it preserves the starting point of comp function!
g = 0
If StrComp(strAna, "ana", 1) = 0 And StrComp(TimeCheck, "yes", 1) = 0 Then TimeCheck = vbNullString
ElseIf InStr(1, sh, "Cmp_") > 0 Then
strSheetDataName = mid$(sh, 5, (Len(sh) - 4))
If StrComp(LCase(Cells(10, 3).Value), "chem", 1) = 0 Then
Cells(10, 3).Value = "In-BG"
strAna = "FitComp"
strSheetAnaName = "Cmp_" + strSheetDataName
strSheetGraphName = "Graph_" + strSheetDataName
Set sheetGraph = Worksheets(strSheetGraphName)
Set sheetAna = Worksheets(strSheetAnaName)
sheetGraph.Activate
numXPSFactors = Cells(43, para + 12).Value
numChemFactors = Cells(42, para + 12).Value
If IsEmpty(Cells(51, para + 10)) = False Then
sheetGraph.Range(Cells(40, para + 9), Cells((Cells(51, para + 10).End(xlDown).Row), para + 30)).Copy Destination:=sheetAna.Cells(40, para + 9)
End If
Set sheetGraph = Worksheets(strSheetAnaName)
sheetGraph.Activate
If Cells(43, para + 12).Value <> numXPSFactors Then Call PlotElem
If Cells(42, para + 12).Value <> numChemFactors Then Call PlotChem
strErr = "end"
If Len(strErr) > 0 Then Exit Sub
ElseIf StrComp(LCase(mid$(Cells(1, 1).Value, 1, 3)), "leg", 1) = 0 Then
strSheetGraphName = "Cmp_" + strSheetDataName
Results = vbNullString
Call CombineLegend
ActiveWorkbook.Save
End
ElseIf StrComp(LCase(mid$(Cells(1, 1).Value, 1, 4)), "auto", 1) = 0 Then
strSheetGraphName = "Cmp_" + strSheetDataName
ncomp = Cells(45, para + 10).Value
Call GetAutoScale
If Len(strErr) > 0 Then Exit Sub
Else
strSheetAnaName = "Exc_" + strSheetDataName
strSheetGraphName = "Cmp_" + strSheetDataName
ncomp = Range(Cells(10, 1), Cells(10, 1).End(xlToRight)).Columns.Count / 3
Call ExportCmp("")
ActiveWorkbook.Save
If Len(strErr) > 0 Then Exit Sub
End If
For k = 0 To CInt(para / 3)
If StrComp(Cells(1, (4 + (3 * k))).Value, "comp", 1) = 0 Then Exit For
Next
If k >= CInt(para / 3) Then
cmp = -1
Else
cmp = k ' position of comp if cmp < ncomp
End If ' "cmp" should not be used because it preserves the starting point of comp function!
ElseIf InStr(1, sh, "Fit_") > 0 Then
If InStr(1, sh, "Fit_BE") > 0 And IsEmpty(Cells(1, 101).Value) = False Then
strSheetDataName = Cells(1, 101).Value
Else
strSheetDataName = mid$(sh, 5, (Len(sh) - 4))
End If
wb = ActiveWorkbook.Name
If Not ExistSheet("Graph_" + strSheetDataName) Then
TimeCheck = MsgBox("Graph sheet " & "Graph_" + strSheetDataName & " is not found.", vbExclamation)
End
End If
If Workbooks(wb).Sheets("Graph_" + strSheetDataName).Cells(40, para + 9).Value = "Ver." Then
Else
For q = 1 To 1000
If StrComp(Workbooks(wb).Sheets("Graph_" + strSheetDataName).Cells(40, q + 9).Value, "Ver.", 1) = 0 Then Exit For
Next
para = q
End If
If LCase(Cells(1, 4).Value) = "ana" And Cells(1, 1).Value <> "Si" Then
Cells(1, 4).Value = "Name"
Set rng = [A:A]
numData = Application.CountA(rng) - 19
startEb = Cells(6, 101).Value
endEb = Cells(7, 101).Value
dblMax = Cells(3, 101).Value
dblMin = Cells(2, 101).Value
Application.Calculation = xlCalculationManual
Call FitAnalysis
Application.Calculation = xlCalculationAutomatic
Application.CutCopyMode = False
Cells(1, 1).Select
If Len(strErr) > 0 Then Exit Sub
ElseIf LCase(Cells(1, 4).Value) = "debug" Then
Cells(1, 4).Value = "Name"
testMacro = "debugFit"
Call debugAll
Application.CutCopyMode = False
End
ElseIf LCase(Cells(1, 4).Value) = "debuga" Then
Cells(1, 4).Value = "Name"
testMacro = "debugShift"
Call debugAll
Application.CutCopyMode = False
End
ElseIf LCase(Cells(1, 4).Value) = "debugf" Then
Cells(1, 4).Value = "Name"
testMacro = "debugPara"
Call debugAll
Application.CutCopyMode = False
End
ElseIf LCase(Cells(1, 4).Value) = "lmfit" Then
Cells(1, 4).Value = "Name"
Call ExportLmfit
Application.CutCopyMode = False
strErr = "exported"
If Len(strErr) > 0 Then Exit Sub
ElseIf mid$(LCase(Cells(1, 4).Value), 1, 3) = "exp" Then
If InStr(1, sh, "Fit_BE") > 0 Then
strSheetAnaName = "Exp_" & sh
strSheetFitName = sh
Else
strSheetAnaName = "Exp_Fit_" & strSheetDataName
strSheetFitName = "Fit_" & strSheetDataName
End If
Call ExportFit
Cells(1, 4).Value = "Name"
Application.CutCopyMode = False
ActiveWorkbook.Save
strErr = "exported"
If Len(strErr) > 0 Then Exit Sub
Else
If InStr(1, sh, "Fit_BE") > 0 Then
strMode = "Do fit range"
Else
strMode = "Do fit"
End If
Call FitCurve
If Len(strErr) > 0 Then Exit Sub
End If
ElseIf InStr(1, sh, "Ana_") > 0 Then
strSheetDataName = mid$(sh, 5, (Len(sh) - 4))
wb = ActiveWorkbook.Name
If StrComp(Cells(1, para).Value, "Parameters", 1) = 0 Then
Else
For q = 1 To 1000
If Cells(1, q).Value = "Parameters" Then
Exit For
ElseIf q = 1000 Then
MsgBox "Ana sheet has no parameters to be compared."
End
End If
Next
para = q
End If
Call FitRatioAnalysis
Application.CutCopyMode = False
End
ElseIf InStr(1, sh, "Exp_") > 0 Then
If InStr(1, sh, "Exp_Fit_") > 0 Then
ActiveSheet.Columns("B:C").EntireColumn.Delete
Do While IsEmpty(Cells(1, 3).Value) = False
ActiveSheet.Columns("C").EntireColumn.Delete
Loop
ActiveSheet.Cells(1, 2).Value = mid$(sh, 9, Len(sh) - 8) & "n"
End If
Call Convert2Txt("", "csv")
TimeCheck = MsgBox("Data were exported in the csv files.", vbExclamation)
End
ElseIf InStr(1, sh, "Norm_") > 0 Or InStr(1, sh, "Edge_") > 0 Or InStr(1, sh, "Diff_") > 0 Then
strSheetDataName = ActiveSheet.Name
Else
If InStr(ActiveWorkbook.Name, ".") < 1 Then
flag = Application.Dialogs(xlDialogSaveAs).Show
If flag = False Then
TimeCheck = MsgBox("Save the file with the extension: xlsx!", vbExclamation)
End
End If
End If
strTest = mid$(ActiveWorkbook.Name, 1, InStrRev(ActiveWorkbook.Name, ".") - 1)
strTest = mid$(strTest, 1, 19)
If Not ExistSheet(strTest) Then
If StrComp(mid$(Cells(1, 1).Value, 2, 4), "E/eV", 1) = 0 Then
ActiveSheet.Name = strTest ' follow the name of work book
strSheetDataName = strTest
Else
ActiveSheet.Name = mid$(sh, 1, 19) ' follow the name of work sheet
strSheetDataName = mid$(sh, 1, 19)
End If
Else
strSheetDataName = strTest
End If
strCasa = "User Defined" ' default database for XPS
strAES = "User Defined" ' default database for AES
strchm = "Tech DB" ' default database for Chem
End If
If Not ExistSheet(strSheetDataName) Then End
strSheetGraphName = "Graph_" + strSheetDataName
strSheetFitName = "Fit_" + strSheetDataName
Set sheetData = Worksheets(strSheetDataName)
Worksheets(strSheetDataName).Activate
wb = ActiveWorkbook.Name
wb = mid$(wb, 1, InStrRev(wb, ".") - 1) + ".xlsx"
Application.DisplayAlerts = False
If Len(ActiveWorkbook.Path) < 2 Then
Application.Dialogs(xlDialogSaveAs).Show
Else
On Error GoTo Error1
' If backSlash = "/" And numRun = 1 Then
' filePermissionCandidates = Array(wbpath, ActiveWorkbook.FullName, wbpath & backSlash & wb)
' fileAccessGranted = GrantAccessToMultipleFiles(filePermissionCandidates)
' End If
wbpath = ActiveWorkbook.Path
ActiveWorkbook.SaveAs Filename:=wbpath + backSlash + wb, FileFormat:=xlOpenXMLWorkbook
End If
Application.DisplayAlerts = True
Exit Sub
Error1:
Err.Clear
End Sub
Sub TargetDataAnalysis()
strMode = Cells(1, 1).Value
If InStr(strMode, "E/eV") > 0 Then ' Manually imported data analsysis
Do
If InStr(strMode, "'") > 0 Then ' remove "'" generated in Igor produced text
q = InStr(strMode, "'")
strMode = Left$(strMode, q - 1) + mid$(strMode, q + 1)
Else
Cells(1, 1).Value = strMode
Exit Do
End If
Loop
If InStr(Cells(1, 3).Value, "E/eV") > 0 Then
Call Convert2Txt("", "csv")
TimeCheck = MsgBox("Data were exported in the text files.", vbExclamation)
End
End If
If cmp >= 0 Then
Call GetCompare
ElseIf StrComp(strAna, "ana", 1) = 0 Then
Call FitCurve
ElseIf StrComp(strAna, "chem", 1) = 0 Then
Call PlotChem
ElseIf StrComp(strAna, "elem", 1) = 0 Then
Set sheetGraph = Worksheets(strSheetGraphName)
If LCase(sheetGraph.Cells(10, 1).Value) = "pe" Then
sheetGraph.Cells(10, 3).Value = "De" 'strl(3)
Else
sheetGraph.Cells(10, 3).Value = "In" 'strl(3)
End If
Call ElemXPS
Call PlotElem
Else
Call KeBL ' KE, BE, PE, GE, QE, AE, ME/eV data setup
If Len(strErr) > 0 Then Exit Sub
If StrComp(strMode, "GE/eV", 1) = 0 Then ' Grating scan with fixed gap
Call EngBL
Call descriptHidden1
Call GetOut
Else
Call PlotCLAM2
If Len(strErr) > 0 Then Exit Sub
Call ElemXPS
If Len(strErr) > 0 Then Exit Sub
Call PlotElem
If Len(strErr) > 0 Then Exit Sub
Call FitCurve
End If
End If
Else
strMode = mid$(Cells(2, 1).Value, 1, 5)
If StrComp(strMode, "CLAM2", 1) = 0 Or StrComp(strMode, "Photo", 1) = 0 Then
If cmp >= 0 Then
Call GetCompare
Else
Call FormatData
If Len(strErr) > 0 Then Exit Sub
Call PlotCLAM2
If Len(strErr) > 0 Then Exit Sub
Call ElemXPS
If Len(strErr) > 0 Then Exit Sub
Call PlotElem
If Len(strErr) > 0 Then Exit Sub
Call FitCurve
End If
Else
For j = 1 To 5 ' check vamas in the first 5 lines
'Debug.Print "vamas", j
If StrComp(mid$(Cells(j, 1).Value, 1, 5), "VAMAS", 1) = 0 Then
Call LoadVAMAS ' Load vamas format (multipak exported iso format)
Exit For
End If
Next
Call GetOut
End If
End If
End Sub
Sub LoadVAMAS()
Dim numNotes As Long, numRegions As Long, numVpara As Long, numVcoeff As Long, numBlocks As Long, list_file As String
Dim C1 As Variant, C2 As Variant, rng As Range, strPath As String, strTest As String, Fname As String, strInstr As String
Dim numCpara As Long, numTfunc As Single, numVscans As Long, numExp As Long, scanMode As String, list_tec As String
Dim expMode As String, numVadd As Long, strID As String, startRl As Long, numFut As Long, strTec As String, arrTec() As String
' limited less than 99 blocks, BE scale exporting, file name length < 7 character
list_file = ""
list_tec = "AES diff,AES dir,EDX,ELS,FABMS,FABMS energy spec,ISS,SIMS,SIMS energy spec,SNMS,SNMS energy spec,UPS,XPS,XRF"
arrTec = Split(list_tec, ",")
strPath = ActiveWorkbook.Path
strSheetDataName = ActiveSheet.Name
Set sheetData = Worksheets(strSheetDataName)
Set rng = sheetData.UsedRange
C1 = rng
strInstr = C1(3, 1)
numVpara = 5 ' initial note lines
numNotes = CInt(C1(numVpara + 1, 1)) ' additional note lines before NORM
expMode = C1(numVpara + numNotes + 2, 1)
scanMode = C1(numVpara + numNotes + 3, 1)
numVpara = numVpara + numNotes + 4
If expMode = "MAP" Or expMode = "MAPDP" Or expMode = "NORM" Or expMode = "SDP" Then
numVpara = numVpara + 1
End If
If expMode = "MAP" Or expMode = "MAPDP" Then
numVpara = numVpara + 3
End If
numExp = CInt(C1(numVpara, 1))
numVpara = numVpara + CInt(C1(numVpara, 1)) * 2 + 1
If CInt(C1(numVpara, 1)) > 0 Then 'number of entries in parameter
numVpara = numVpara + CInt(C1(numVpara, 1)) + 1
Else
numVpara = numVpara + 1
End If
If CInt(C1(numVpara, 1)) > 0 Then 'number of manually entered items in block
numVpara = numVpara + CInt(C1(numVpara, 1)) + 1
Else
numVpara = numVpara + 1
End If
numFut = CInt(C1(numVpara + 1, 1)) ' number of future upgrade block entries
If CInt(C1(numVpara, 1)) > 0 Then ' number of future upgrade experiment entries
numVpara = numVpara + CInt(C1(numVpara, 1)) + 2
Else
numVpara = numVpara + 2
End If
numBlocks = CInt(C1(numVpara, 1))
startRl = numVpara + 1
Debug.Print "numBlocks", numBlocks ' this is starting block
For p = 1 To numBlocks
Debug.Print "block", p, "startRl", startRl
' If StrComp(mid$(strInstr, 1, 14), "PHI VersaProbe", 1) = 0 Then ' if ElemD (atom and transition) is multiple, strID should be used.
' strID = Str(p)
' strID = Replace(strID, " ", "")
' Else
' strID = C1(startRl + 1, 1) + "_" + C1(startRl, 1)
' strID = Replace(strID, "/", "")
' strID = Replace(strID, " ", "")
' strID = Replace(strID, ":", "")
' End If
strID = Application.WorksheetFunction.Trim(strID)
numVcoeff = CInt(C1(startRl + 9, 1)) ' cell position of block
numVcoeff = startRl + 9 + numVcoeff + 1 ' position "XPS" for strTec
j = 0
For k = 0 To 100
If k = 100 Then
Debug.Print "technique is not found"
End
ElseIf IsEmpty(C1(numVcoeff + k, 1)) = False Then
strTec = C1(numVcoeff + k, 1)
For q = 0 To UBound(arrTec)
If StrComp(arrTec(q), strTec, 1) = 0 Then
numVcoeff = numVcoeff + k
j = 1
Debug.Print "technique is found"
Exit For
End If
Next q
End If
If j > 0 Then Exit For
Next k
If expMode = "MAP" Or expMode = "MAPDP" Then numVcoeff = numVcoeff + 2
If expMode = "SDP" Or expMode = "MAPDP" Or expMode = "MAPSVDP" Or expMode = "SDPSV" Then
numVcoeff = numVcoeff + 3
ElseIf mid$(strTec, 1, 5) = "FABMS" Or strTec = "ISS" Or mid$(strTec, 1, 4) = "SIMS" Or mid$(strTec, 1, 4) = "SNMS" Then
numVcoeff = numVcoeff + 3
End If
pe = CSng(C1(numVcoeff + numExp + 2, 1))
Debug.Print pe, "pe", numVcoeff + numExp + 2
numVcoeff = numVcoeff + numExp + 2
If expMode = "MAP" Or expMode = "MAPDP" Or expMode = "MAPSV" Or expMode = "MAPSVDP" Or expMode = "SEM" Then numVcoeff = numVcoeff + 2
If expMode = "SEM" Or expMode = "MAPSV" Or expMode = "MAPSVDP" Then numVcoeff = numVcoeff + 6
If strTec = "AES diff" Then numVcoeff = numVcoeff + 1
wf = CSng(C1(numVcoeff + 9, 1))
If wf > 100 Or wf < -100 Then wf = 0 ' wf = "1e+037"
Debug.Print wf, "wf", numVcoeff + 9
' If scanMode = "REGULAR" then
If LCase(C1(numVcoeff + 18, 1)) = "binding energy" Then
strMode = "BE/eV"
ElseIf LCase(C1(numVcoeff + 18, 1)) = "kinetic energy" Then
strMode = "KE/eV"
ElseIf LCase(C1(numVcoeff + 18, 1)) = "photon energy" Then
strMode = "PE/eV"
Else
strMode = "EE/eV"
End If
Debug.Print strMode, "strMode", numVcoeff + 18
ElemD = C1(numVcoeff + 15, 1) & C1(numVcoeff + 16, 1)
startEk = CSng(C1(numVcoeff + 20, 1))
stepEk = CSng(C1(numVcoeff + 21, 1))
numCpara = CInt(C1(numVcoeff + 22, 1))
Debug.Print CInt(C1(numVcoeff + 22, 1)), "numCpara", numVcoeff + 22
numVcoeff = numVcoeff + 22
numTfunc = CSng(C1(numVcoeff + numCpara * 2 + 2, 1))
numVscans = CInt(C1(numVcoeff + numCpara * 2 + 3, 1))
Debug.Print CSng(C1(numVcoeff + numCpara * 2 + 2, 1)), "numTfunc", numVcoeff + numCpara * 2 + 2
Debug.Print CInt(C1(numVcoeff + numCpara * 2 + 3, 1)), "numVscans", numVcoeff + numCpara * 2 + 3
numVcoeff = numVcoeff + numCpara * 2 + 2
If expMode = "SDP" Or expMode = "MAPDP" Or expMode = "MAPSVDP" Or expMode = "SDPSV" Then
If strTec = "AES diff" Or strTec = "AES dir" Or strTec = "EDX" Or strTec = "ELS" Or strTec = "UPS" Or strTec = "XPS" Or strTec = "XRF" Then
numVcoeff = numVcoeff + 7
End If
End If
numVadd = CInt(C1(numVcoeff + 6, 1))
numVcoeff = numVcoeff + 6 + numVadd * 3 + 1 + numFut
numData = CInt(C1(numVcoeff, 1)) / numCpara
Debug.Print CInt(C1(numVcoeff, 1)), "numDataRaw", numVcoeff
numVcoeff = numVcoeff + (numCpara * 2) + 1
Debug.Print "startEk", startEk, "stepEk", stepEk, "numData", numData, ElemD
If StrComp(mid$(strSheetDataName, 1, 6), "Vamas_", 1) = 0 Then strSheetDataName = mid$(strSheetDataName, 7, Len(strSheetDataName) - 6)
If Len(strSheetDataName) > 11 Then strSheetDataName = mid$(strSheetDataName, 1, 11)
If Len(ElemD) > 5 Then ElemD = mid$(ElemD, 1, 5)
' If Len(strID) > 7 Then strID = mid$(strID, 1, 7)
' strSheetAnaName = strSheetDataName + "_" + strID + "_" + ElemD
strSheetAnaName = strSheetDataName + "_" + ElemD
If p = 0 Then sheetData.Name = "Vamas_" + strSheetDataName
If ExistSheet(strSheetAnaName) Then
Application.DisplayAlerts = False
Worksheets(strSheetAnaName).Delete
Application.DisplayAlerts = True
End If
Worksheets.Add().Name = strSheetAnaName
Set sheetAna = Worksheets(strSheetAnaName)
sheetAna.Activate
C2 = sheetAna.Range(Cells(1, 1), Cells(1 + numData, 2))
For j = 0 To numData - 1
If strTec = "XPS" Or strTec = "UPS" Then
If strMode = "KE/eV" Then ' convert BE/eV
C2(2 + j, 1) = WorksheetFunction.Round(pe - wf - (startEk + j * stepEk), 3)
Else ' BE/eV case as it is
C2(2 + j, 1) = WorksheetFunction.Round(startEk + j * stepEk, 3)
End If
Else
C2(2 + j, 1) = WorksheetFunction.Round(startEk + j * stepEk, 3)
End If
C2(2 + j, 2) = C1(numVcoeff + j * numCpara, 1) / numTfunc / numVscans
Next
sheetAna.Range(Cells(1, 1), Cells(1 + numData, 2)) = C2
If strTec = "XPS" Or strTec = "UPS" Then
Cells(1, 1).Value = "BE/eV"
Cells(1, 2).Value = "PE: " & pe & " eV"
Else
Cells(1, 1).Value = strMode
Cells(1, 2).Value = "EE: " & pe & " eV"
End If
Fname = strPath + backSlash + strSheetAnaName & ".txt"
list_file = list_file & strSheetAnaName & ".txt" & vbCrLf
fileNum = FreeFile(0)
Open Fname For Output As #fileNum
For j = 1 To 1 + numData
strTest = sheetAna.Cells(j, 1) & vbTab & sheetAna.Cells(j, 2)
Print #fileNum, strTest
strTest = vbNullString
Next j
Close #fileNum
Application.DisplayAlerts = False
sheetAna.Delete
Application.DisplayAlerts = True
startRl = numVcoeff + numData * numCpara
Next p
If testMacro <> "debug" Then MsgBox "Export " & numBlocks & " text files." & vbCrLf & list_file, vbInformation
sheetData.Activate
End Sub
Sub PlotCLAM2()
Dim C1 As Variant, C2 As Variant, C3 As Variant, C4 As Variant, imax As Integer, sig As Integer, SourceRangeColor1 As Long, SourceRangeColor2 As Long, strTest As String
sig = 1
imax = numData + 10
If ExistSheet(strSheetGraphName) Then
Application.DisplayAlerts = False
Worksheets(strSheetGraphName).Delete
Application.DisplayAlerts = True
End If
If ExistSheet(strSheetFitName) Then
Application.DisplayAlerts = False
Worksheets(strSheetFitName).Delete
Application.DisplayAlerts = True
End If
If Len(strSheetGraphName) > 30 And StrComp(mid$(strSheetGraphName, 1, 11), "Graph_Norm_") = 0 Then
MsgBox "File name """ & mid$(strSheetGraphName, 12, Len(strSheetGraphName) - 14) & """ should be <= 16", vbOKOnly, "Error in length of filename: " & Len(mid$(strSheetGraphName, 12, Len(strSheetGraphName) - 14))
End
End If
Worksheets.Add().Name = strSheetGraphName
Set sheetGraph = Worksheets(strSheetGraphName)
sheetGraph.Activate
Set dataBGraph = Range(Cells(11, 2), Cells(11, 2).Offset(numData - 1, 1))
Set dataKGraph = Union(Range(Cells(11, 1), Cells(11, 1).Offset(numData - 1, 0)), Range(Cells(11, 3), Cells(11, 3).Offset(numData - 1, 0)))
Set dataKeGraph = Range(Cells(11, 1), Cells(11, 1).Offset(numData - 1, 0))
Set dataBeGraph = dataKeGraph.Offset(, 1)
dataKeGraph.Value = dataKeData.Value
C1 = dataKeData ' C first column
C2 = dataIntData ' U second column
C3 = dataKeGraph.Offset(, 2) ' dataIntGraph ' A
If StrComp(strMode, "AE/eV", 1) = 0 Or StrComp(strMode, "PE/eV", 1) = 0 Then
C3 = Differ(wf, C1, C2)
Range(Cells(11, 2), Cells((numData + 10), 2)) = C2
ElseIf InStr(strMode, "E/eV") > 0 Then
If StrComp(Cells(1, 3).Value, "Ip", 1) = 0 Or StrComp(Cells(1, 3).Value, "Ie", 1) = 0 Then
C4 = dataKeData.Offset(, 2)
Else
C4 = dataKeData.Offset(, para + 30) ' Empty Ip
End If
For n = 1 To numData
If IsEmpty(C4(n, 1)) Then
C4(n, 1) = 1
Else
If IsNumeric(C3(n, 1)) = False Then
C4(n, 1) = 1
Else
If C4(n, 1) <= 0 Then
C4(n, 1) = 1
End If
End If
End If
C3(n, 1) = (C2(n, 1) / C4(n, 1))
Next
Else
If WorksheetFunction.Average(C2) < 0 Then sig = -1
For n = 1 To numData
If IsNumeric(C2(n, 1)) = False Then Exit For
C3(n, 1) = C2(n, 1) * sig * 1
Next
End If
Range(Cells(11, 3), Cells((numData + 10), 3)) = C3
If StrComp(strMode, "BE/eV", 1) <> 0 Then
If Cells(11, 1).Value > Cells(12, 1).Value Then
Range(Cells(11, 1), Cells((numData + 10), 3)).Sort key1:=Cells(11, 1), order1:=xlAscending
End If
End If
Call descriptGraph
Call scalecheck
If strMode = "ME/eV" Then Call SheetCheckGenerator ' Check Sheet for "ME/eV"
If numMajorUnit > 0 Then
If startEk > 0 Then
startEk = Application.Floor(startEk, numMajorUnit)
Else
startEk = Application.Ceiling(startEk, (-1 * numMajorUnit))
End If
If endEk > 0 Then
endEk = Application.Ceiling(endEk, numMajorUnit)
Else
endEk = Application.Floor(endEk, (-1 * numMajorUnit))
End If
End If
Charts.Add
ActiveChart.ChartType = xlXYScatterLinesNoMarkers 'xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=dataBGraph, PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:=strSheetGraphName
ActiveChart.SeriesCollection(1).Name = ActiveWorkbook.Name '"BE graph"
ActiveChart.ChartTitle.Delete
With ActiveChart.Axes(xlCategory, xlPrimary)
If StrComp(strl(1), "Pe", 1) = 0 Or StrComp(strl(3), "De", 1) = 0 Or StrComp(strl(1), "Po", 1) = 0 Then
.MinimumScale = startEb
.MaximumScale = endEb
.Crosses = xlMinimum
Else
.MinimumScale = endEb
.MaximumScale = startEb
.ReversePlotOrder = True
.Crosses = xlMaximum
End If
.HasTitle = True
.AxisTitle.Text = strl(0)
End With
SourceRangeColor1 = ActiveChart.SeriesCollection(1).Border.Color