Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 30.91 KB | None | 0 0
  1. Sub AudioDataAnalysis()
  2.  
  3.     Application.ScreenUpdating = False
  4.     Set startCell = ActiveCell
  5.      
  6.     'Convert delimited data to columns
  7.     Columns("A:A").Select
  8.     Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, Comma:=True
  9.        
  10.     MSG1 = MsgBox("Delete first and last measurements?", vbYesNo)
  11.     If MSG1 = vbYes Then
  12.          TrimFirstandLast
  13.     End If
  14.      
  15.     Dim TimeRng As Range
  16.     Dim searchstring As String
  17.     Dim headers As Range
  18.     Dim counter As Integer
  19.    
  20.     searchstring = "Time"
  21.     Set TimeRng = ActiveSheet.Range("A1:ZZ10000").Find(searchstring, lookat:=xlPart)
  22.     If TimeRng Is Nothing Then
  23.         MsgBox "'" & searchstring & "' was not found."
  24.         Exit Sub
  25.     End If
  26.      
  27.     If Not TimeRng Is Nothing Then
  28.         Set XL2 = ActiveSheet.Range("A1:ZZ10000").Find("XL2", lookat:=xlPart)
  29.    
  30.         If XL2 Is Nothing Then
  31.     End If
  32.    
  33.     'This is a hack
  34.         If Not XL2 Is Nothing Then
  35.             Set TimeRng = ActiveSheet.Range("C23")
  36.         End If
  37.     End If
  38.        
  39.    
  40.     'get data height------------------------
  41.     Dim DataHeight As Integer
  42.     DataHeight = 0
  43.    
  44.     Dim week As Integer
  45.     Dim weekd As String
  46.     Dim dd As String
  47.    
  48.    
  49.     'go down to first value
  50.     If Not XL2 Is Nothing Then
  51.         TimeRng.Offset(2, -1).Activate
  52.         week = Weekday(ActiveCell)
  53.         weekd = WeekdayName(week - 1, 2)
  54.         dd = CDate(ActiveCell.value)
  55.         TimeRng.Offset(-1, 1).Activate
  56.     Else
  57.         TimeRng.Offset(1, 0).Activate
  58.         week = Weekday(ActiveCell)
  59.         weekd = WeekdayName(week - 1, 2)
  60.         dd = CDate(ActiveCell.value)
  61.     End If
  62.        
  63.     Do While Not ActiveCell.value = ""
  64.         ActiveCell.Offset(1, 0).Activate
  65.         DataHeight = DataHeight + 1
  66.     Loop
  67.    
  68.    
  69.     '----------------------------------------
  70.    
  71.    
  72.     Set headers = ActiveSheet.Range(TimeRng.Address)
  73.        
  74.     'ActiveWindow.FreezePanes = False
  75.     TimeRng.Activate
  76.     Dim TimeChartRng As Range
  77.     'Get width of header row
  78.     counter = 0
  79.     Do While Not ActiveCell.value = ""
  80.         ActiveCell.Offset(0, 1).Activate
  81.         counter = counter + 1
  82.     Loop
  83.     counter = counter - 1
  84.     TimeRng.Activate
  85.        
  86.     'go down to first value
  87.     If Not XL2 Is Nothing Then
  88.         TimeRng.Offset(2, 0).Activate
  89.     Else
  90.         TimeRng.Offset(1, 0).Activate
  91.     End If
  92.     'freeze panes
  93.     'ActiveWindow.FreezePanes = True
  94.    
  95.     ' --------------JR-------------------------
  96.     Dim k As Integer
  97.     k = 0
  98.     Dim bin() As Integer
  99.     ReDim Preserve bin(0)
  100.     bin(0) = 3
  101.     Dim DayOrNight As String
  102.     Dim dayCount As Integer
  103.     dayCount = 1
  104.     Dim whichday() As Integer
  105.     ' ----------------------------------------------
  106.    
  107.     'this loop creates an array which assigns a number to each new day/night cycle
  108.     Do While Not ActiveCell.value = ""
  109.         ActiveCell.NumberFormat = "hh:mm"
  110.         If TimeValue(ActiveCell.Text) >= TimeValue("07:00") And TimeValue(ActiveCell.Text) < TimeValue("23:00") Then
  111.             'Color index 42 - light blue
  112.             Range(ActiveCell, Cells(ActiveCell.row, ActiveCell.Column + counter)).Interior.Color = RGB(115, 220, 255)
  113.            
  114.             ' --------------JR-------------------------
  115.             If k = 0 Then
  116.                 DayOrNight = "Day"
  117.             End If
  118.             If bin(UBound(bin)) = 2 Then
  119.                 dayCount = dayCount + 1
  120.             End If
  121.             ReDim Preserve bin(k)
  122.             bin(k) = 1
  123.             k = k + 1
  124.             ReDim Preserve whichday(ActiveCell.row)
  125.             whichday(ActiveCell.row) = dayCount
  126.             '--------------------------------------------
  127.            
  128.         Else
  129.             'color index 41 - darker blue
  130.             Range(ActiveCell, Cells(ActiveCell.row, ActiveCell.Column + counter)).Interior.Color = RGB(115, 133, 255)
  131.            
  132.             ' --------------JR-------------------------
  133.             If k = 0 Then
  134.                 DayOrNight = "Night"
  135.             End If
  136.             If bin(UBound(bin)) = 1 Then
  137.                 dayCount = dayCount + 1
  138.             End If
  139.             ReDim Preserve bin(k)
  140.             bin(k) = 2
  141.             k = k + 1
  142.             ReDim Preserve whichday(ActiveCell.row)
  143.             whichday(ActiveCell.row) = dayCount
  144.                 '------------------------------------------------
  145.         End If
  146.            
  147.         If TimeChartRng Is Nothing Then
  148.             Set TimeChartRng = ActiveCell
  149.         Else
  150.             Set TimeChartRng = Union(TimeChartRng, ActiveCell)
  151.         End If
  152.         ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Select
  153.     Loop
  154.  
  155.     'Getting LAeqs ----------------------
  156.     TimeRng.Activate
  157.          
  158.     'JR----------------------
  159.     Dim LAeqRng() As Variant
  160.     ReDim LAeqRng(dayCount, 63) 'Needs fixing to take variable not 63
  161.    
  162.     Dim LA90Rng() As Variant
  163.     ReDim LA90Rng(dayCount, 63)
  164.    
  165.     Dim LAMRng() As Variant
  166.     ReDim LAMRng(dayCount, 31)
  167.     'JR-----------------------
  168.          
  169.     Dim DayLAeqRng As Range
  170.     Dim NightLAeqRng As Range
  171.     Dim LAeqDay As Double
  172.     Dim LAeqChartRng As Range
  173.    
  174.     Dim Dayl90minRng As Range
  175.     Dim Nightl90minRng As Range
  176.     Dim l90minCol As Range
  177.     Dim Nightl90minResult As Double
  178.     Dim Dayl90minResult As Double
  179.     Dim LA90ChartRng As Range
  180.    
  181.     Dim NightLAmaxRng As Range
  182.     Dim LAmaxCol As Range
  183.     Dim NightLAmaxResult As String
  184.     Dim LAmaxChartRng As Range
  185.          
  186.     'Set LAeqCol
  187.     ActiveCell.Offset(0, counter).Activate
  188.     Set LAeqCol = ActiveSheet.Range("A1:ZZ10000").Find("LAeq", lookat:=xlPart)
  189.    
  190.     'l90minCol
  191.     If Not XL2 Is Nothing Then
  192.         Set l90minCol = ActiveSheet.Range("A1:ZZ10000").Find("LAF90.0%", lookat:=xlPart)
  193.     Else
  194.         Set l90minCol = ActiveSheet.Range("A1:ZZ10000").Find("LA90", lookat:=xlPart)
  195.     End If
  196.    
  197.     'LAmax col
  198.     If Not XL2 Is Nothing Then
  199.         Set LAmaxCol = ActiveSheet.Range("A1:ZZ10000").Find("LAFmax", lookat:=xlPart)
  200.     Else
  201.         Set LAmaxCol = ActiveSheet.Range("A1:ZZ10000").Find("LAmax", lookat:=xlPart)
  202.     End If
  203.    
  204.     Dim coldif As Integer
  205.     If Not l90minCol Is Nothing Then
  206.         coldif = l90minCol.Column - LAeqCol.Column
  207.     End If
  208.    
  209.     Dim coldif2 As Integer
  210.     If Not LAmaxCol Is Nothing Then
  211.         coldif2 = LAmaxCol.Column - LAeqCol.Column
  212.     End If
  213.    
  214.     If Not LAeqCol Is Nothing Then
  215.         LAeqCol.Activate
  216.         If Not XL2 Is Nothing Then
  217.             ActiveCell.Offset(2, 0).Activate
  218.         Else
  219.             ActiveCell.Offset(1, 0).Activate
  220.         End If
  221.     End If 'delete if broken
  222.        
  223.     'JR----------
  224.     Dim z As Integer
  225.     Dim day As Integer
  226.     z = 0
  227.     'JR------------
  228.        
  229.     Do While Not ActiveCell.value = ""
  230.          
  231.         'JR------------------
  232.         day = whichday(ActiveCell.row)
  233.         If Not day = dday Then
  234.             z = 0
  235.         End If
  236.         LAeqRng(day, z) = ActiveCell
  237.         If TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) >= TimeValue("07:00") And TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) < TimeValue("23:00") Then
  238.             Else
  239.                 LAMRng(day, z) = ActiveCell.Offset(0, coldif2)
  240.         End If
  241.         LA90Rng(day, z) = ActiveCell.Offset(0, coldif)
  242.         z = z + 1
  243.         dday = day
  244.         'JR---------------------------------
  245.          
  246.         'day----------------
  247.         If TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) >= TimeValue("07:00") And TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) < TimeValue("23:00") Then
  248.             If DayLAeqRng Is Nothing Then
  249.                 Set DayLAeqRng = ActiveCell
  250.             Else
  251.                 Set DayLAeqRng = Union(DayLAeqRng, ActiveCell)
  252.             End If
  253.                
  254.         'night---------------
  255.         Else
  256.             If NightLAeqRng Is Nothing Then
  257.                 Set NightLAeqRng = ActiveCell
  258.             Else
  259.               Set NightLAeqRng = Union(NightLAeqRng, ActiveCell)
  260.             End If
  261.         End If
  262.                
  263.         'generate LAeq chart range
  264.         If LAeqChartRng Is Nothing Then
  265.             Set LAeqChartRng = ActiveCell
  266.         Else
  267.             Set LAeqChartRng = Union(LAeqChartRng, ActiveCell)
  268.         End If
  269.         ActiveCell.Offset(1, 0).Activate
  270.     Loop
  271.  
  272.     'JR-------------------------------------------------------------
  273.     Dim m As Integer
  274.     Dim n As Integer
  275.     Dim hours() As Double
  276.     ReDim hours(dayCount)
  277.     Dim h As Double
  278.     h = 0#
  279.     L = WorksheetFunction.Max(LA90Rng)
  280.    
  281.     'Calculate how many measurements in each day (needed to calculate mean)
  282.     For m = 1 To dayCount
  283.         For n = 0 To 63
  284.             If Not LAeqRng(m, n) = "" Then
  285.                 h = h + 1#
  286.             End If
  287.         Next n
  288.         hours(m) = h
  289.         h = 0#
  290.     Next m
  291.    
  292.     'find lowest LA90 in each night
  293.     Dim l90m() As Double
  294.     ReDim l90m(dayCount)
  295.    
  296.     For m = 1 To dayCount
  297.         For n = 0 To 31
  298.             If LA90Rng(m, n) > 0 And LA90Rng(m, n) < L Then
  299.                 L = LA90Rng(m, n)
  300.             End If
  301.         Next n
  302.         l90m(m) = L
  303.         L = WorksheetFunction.Max(LA90Rng)
  304.     Next m
  305.                
  306.     'find 10th Lmax each night
  307.     Dim LAM() As Double
  308.     ReDim LAM(31)
  309.     Dim LAM2() As Double
  310.     ReDim LAM2(dayCount)
  311.     Dim h2 As Integer
  312.    
  313.    
  314.     For m = 1 To dayCount
  315.         For n = 0 To 31
  316.             LAM(n) = LAMRng(m, n)
  317.         Next n
  318.         If hours(m) = 32 Then
  319.             LAM2(m) = WorksheetFunction.Large(LAM, 10)
  320.         ElseIf hours(m) < 3 Then
  321.             LAM2(m) = WorksheetFunction.Max(LAM)
  322.         Else
  323.             h2 = hours(m) * (10# / 32#)
  324.             LAM2(m) = WorksheetFunction.Large(LAM, h2)
  325.         End If
  326.            
  327.     Next m
  328.            
  329.     'log -> arithmetic
  330.     For m = 1 To dayCount
  331.         For n = 0 To 63
  332.             LAeqRng(m, n) = 10# ^ ((LAeqRng(m, n)) / 10#)
  333.         Next n
  334.     Next m
  335.    
  336.     Dim LAeqdays() As Double
  337.     ReDim LAeqdays(1 To dayCount)
  338.    
  339.     'arithmetic sum
  340.     For m = 1 To dayCount
  341.         For n = 0 To 63
  342.             If Not LAeqRng(m, n) = 1 Then
  343.                 LAeqdays(m) = LAeqdays(m) + LAeqRng(m, n)
  344.             End If
  345.         Next n
  346.         LAeqdays(m) = LAeqdays(m) / hours(m)
  347.     Next m
  348.    
  349.     'arithmetic -> log
  350.     For m = 1 To dayCount
  351.         LAeqdays(m) = 10# * Log10(LAeqdays(m))
  352.     Next m
  353.     'JR----------------------------------------------------
  354.    
  355.          
  356.          
  357.     If Not DayLAeqRng Is Nothing Then
  358.         dayLogAvgResult = LOGAVERAGE(DayLAeqRng)
  359.     End If
  360.     If Not NightLAeqRng Is Nothing Then
  361.         nightLogAvgResult = LOGAVERAGE(NightLAeqRng)
  362.     End If
  363.        
  364.        
  365.     'Getting Night lMAx------------
  366.    
  367.     If Not LAmaxCol Is Nothing Then
  368.         LAmaxCol.Activate
  369.         If Not XL2 Is Nothing Then
  370.             ActiveCell.Offset(2, 0).Activate
  371.         Else
  372.             ActiveCell.Offset(1, 0).Activate
  373.         End If
  374.            
  375.         If Not LAmaxCol Is Nothing Then
  376.             Do While Not IsEmpty(ActiveCell.value)
  377.                 If TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) >= TimeValue("07:00") And TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) < TimeValue("23:00") Then
  378.                     Else
  379.                     If NightLAmaxRng Is Nothing Then
  380.                         Set NightLAmaxRng = ActiveCell
  381.                     Else
  382.                         Set NightLAmaxRng = Union(NightLAmaxRng, ActiveCell)
  383.                     End If
  384.                 End If
  385.                
  386.                 'generate LAeq chart range
  387.                 If LAmaxChartRng Is Nothing Then
  388.                     Set LAmaxChartRng = ActiveCell
  389.                 Else
  390.                     Set LAmaxChartRng = Union(LAmaxChartRng, ActiveCell)
  391.                 End If
  392.                 ActiveCell.Offset(1, 0).Activate
  393.             Loop
  394.             If Not NightLAmaxRng Is Nothing Then
  395.                 If WorksheetFunction.count(NightLAmaxRng) < 10 Then
  396.                     largeAmount = WorksheetFunction.count(NightLAmaxRng) / (100 / 32)
  397.                     NightLAmaxResult = WorksheetFunction.Large(NightLAmaxRng, largeAmount)
  398.                 Else
  399.                     NightLAmaxResult = WorksheetFunction.Large(NightLAmaxRng, 10)
  400.                 End If
  401.             End If
  402.         End If
  403.     End If
  404.        
  405.     'Getting Night l90min------------
  406.     If Not l90minCol Is Nothing Then
  407.         l90minCol.Activate
  408.         If Not XL2 Is Nothing Then
  409.             ActiveCell.Offset(2, 0).Activate
  410.         Else
  411.             ActiveCell.Offset(1, 0).Activate
  412.         End If
  413.            
  414.         If Not l90minCol Is Nothing Then
  415.             Do While Not ActiveCell.value = ""
  416.                 If TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) >= TimeValue("07:00") And TimeValue(Cells(ActiveCell.row, TimeRng.Column).Text) < TimeValue("23:00") Then
  417.                         If Dayl90minRng Is Nothing Then
  418.                             Set Dayl90minRng = ActiveCell
  419.                         Else
  420.                             Set Dayl90minRng = Union(Dayl90minRng, ActiveCell)
  421.                         End If
  422.                     Else
  423.                         If Nightl90minRng Is Nothing Then
  424.                             Set Nightl90minRng = ActiveCell
  425.                         Else
  426.                             Set Nightl90minRng = Union(Nightl90minRng, ActiveCell)
  427.                         End If
  428.                End If
  429.    
  430.                 'generate LAeq chart range
  431.                
  432.                 If LA90ChartRng Is Nothing Then
  433.                     Set LA90ChartRng = ActiveCell
  434.                 Else
  435.                     Set LA90ChartRng = Union(LA90ChartRng, ActiveCell)
  436.                 End If
  437.                    
  438.                 ActiveCell.Offset(1, 0).Activate
  439.             Loop
  440.             If Not Nightl90minRng Is Nothing Then
  441.                 Nightl90minResult = WorksheetFunction.Min(Nightl90minRng)
  442.                 Dayl90minResult = WorksheetFunction.Min(Dayl90minRng)
  443.             End If
  444.         End If
  445.     End If
  446.        
  447.         'write results------------------
  448.        
  449.          
  450.     If Not XL2 Is Nothing Then
  451.         TimeRng.Offset(4, counter + 2).Activate
  452.     Else
  453.         TimeRng.Offset(3, counter + 2).Activate
  454.     End If
  455.          
  456.     Columns(ActiveCell.Column).AutoFit
  457.          
  458.     ActiveCell.value = "Date: "
  459.     ActiveCell.Borders(xlEdgeBottom).weight = xlThin
  460.     ActiveCell.Offset(0, 1).Activate
  461.                    
  462.     ActiveCell.value = "LAeq   (Day) "
  463.     ActiveCell.Borders(xlEdgeBottom).weight = xlThin
  464.     ActiveCell.Offset(0, 1).Activate
  465.          
  466.     ActiveCell.value = "LAeq   (Night) "
  467.     ActiveCell.Borders(xlEdgeBottom).weight = xlThin
  468.     ActiveCell.Offset(0, 1).Activate
  469.                  
  470.     ActiveCell.value = "LA90min   (Day) "
  471.     ActiveCell.Borders(xlEdgeBottom).weight = xlThin
  472.     ActiveCell.Offset(0, 1).Activate
  473.    
  474.     ActiveCell.value = "LA90min   (Night) "
  475.     ActiveCell.Borders(xlEdgeBottom).weight = xlThin
  476.     ActiveCell.Offset(0, 1).Activate
  477.                  
  478.     ActiveCell.value = "10th LAmax   (Night) "
  479.     ActiveCell.Borders(xlEdgeBottom).weight = xlThin
  480.     ActiveCell.Offset(1, -5).Activate
  481.    
  482. Dim week2 As Integer
  483.          
  484.     'JR-------------------
  485.     Dim mdate() As Variant
  486.     ReDim mdate(dayCount)
  487.    
  488.    
  489.     For m = 1 To dayCount
  490.         mdate(m) = DateAdd("d", (m - 1), dd)
  491.     Next m
  492.        
  493.    
  494.     If DayOrNight = "Day" And dayCount Mod 2 = 0 Then
  495.         GoTo line1
  496.     ElseIf DayOrNight = "Day" And dayCount Mod 2 = 1 Then
  497.         GoTo line2
  498.     ElseIf DayOrNight = "Night" And dayCount Mod 2 = 0 Then
  499.         GoTo line3
  500.     ElseIf DayOrNight = "Night" And dayCount Mod 2 = 1 Then
  501.         GoTo Line4
  502.     Else
  503.         MsgBox "Error 327846"
  504.         Exit Sub
  505.     End If
  506.          
  507. line1:
  508.     For m = 1 To dayCount / 2
  509.         week2 = (week + (m - 1)) - 1
  510.         If week2 > 7 Then
  511.             week2 = week2 - 7
  512.         End If
  513.         ActiveCell.value = WeekdayName(week2, 2) & ", " & Left(mdate(m), 10)
  514.         ActiveCell.Offset(0, 1).Activate
  515.         'LAeq
  516.         ActiveCell.value = LAeqdays(2 * m - 1)
  517.         ActiveCell.NumberFormat = "0.0"
  518.         ActiveCell.Offset(0, 1).Activate
  519.         ActiveCell.value = LAeqdays(2 * m)
  520.         ActiveCell.NumberFormat = "0.0"
  521.         ActiveCell.Offset(0, 1).Activate
  522.         'LA90
  523.         If Not l90minCol Is Nothing Then
  524.             ActiveCell.value = l90m(2 * m - 1)
  525.         Else
  526.             ActiveCell.value = "no data"
  527.             ActiveCell.HorizontalAlignment = xlRight
  528.         End If
  529.         ActiveCell.Offset(0, 1).Activate
  530.         If Not l90minCol Is Nothing Then
  531.             ActiveCell.value = l90m(2 * m)
  532.         Else
  533.             ActiveCell.value = "no data"
  534.             ActiveCell.HorizontalAlignment = xlRight
  535.         End If
  536.         ActiveCell.Offset(0, 1).Activate
  537.         'LAmax
  538.         If Not LAmaxCol Is Nothing Then
  539.             ActiveCell.value = LAM2(2 * m)
  540.         Else
  541.             ActiveCell.value = "no data"
  542.             ActiveCell.HorizontalAlignment = xlRight
  543.         End If
  544.         ActiveCell.Offset(1, -5).Activate
  545.     Next m
  546.     GoTo lastline
  547.            
  548. line2:
  549.     For m = 1 To (dayCount + 1) / 2
  550.         week2 = (week + (m - 1)) - 1
  551.         If week2 > 7 Then
  552.             week2 = week2 - 7
  553.         End If
  554.         ActiveCell.value = WeekdayName(week2, 2) & ", " & Left(mdate(m), 10)
  555.         ActiveCell.Offset(0, 1).Activate
  556.         'LAeq
  557.         ActiveCell.value = LAeqdays(2 * m - 1)
  558.         ActiveCell.NumberFormat = "0.0"
  559.         ActiveCell.Offset(0, 1).Activate
  560.         If m = (dayCount + 1) / 2 Then
  561.             ActiveCell.value = "-"
  562.             ActiveCell.HorizontalAlignment = xlRight
  563.         Else
  564.             ActiveCell.value = LAeqdays(2 * m)
  565.         End If
  566.         ActiveCell.NumberFormat = "0.0"
  567.        
  568.         'LAMin day
  569.         ActiveCell.Offset(0, 1).Activate
  570.         If Not l90minCol Is Nothing Then
  571.             ActiveCell.value = l90m(2 * m - 1)
  572.         Else
  573.             ActiveCell.value = "no data"
  574.             ActiveCell.HorizontalAlignment = xlRight
  575.         End If
  576.        
  577.         'LAmin night
  578.         ActiveCell.Offset(0, 1).Activate
  579.         If Not l90minCol Is Nothing Then
  580.             If m = (dayCount + 1) / 2 Then
  581.                 ActiveCell.value = "-"
  582.                 ActiveCell.HorizontalAlignment = xlRight
  583.             Else
  584.                 ActiveCell.value = l90m(2 * m)
  585.             End If
  586.         Else
  587.             ActiveCell.value = "no data"
  588.             ActiveCell.HorizontalAlignment = xlRight
  589.         End If
  590.        
  591.         '10th LAmax
  592.         ActiveCell.Offset(0, 1).Activate
  593.         If Not LAmaxCol Is Nothing Then
  594.             If m = (dayCount + 1) / 2 Then
  595.                 ActiveCell.value = "-"
  596.                 ActiveCell.HorizontalAlignment = xlRight
  597.             Else
  598.                 ActiveCell.value = LAM2(2 * m)
  599.             End If
  600.         Else
  601.             ActiveCell.value = "no data"
  602.             ActiveCell.HorizontalAlignment = xlRight
  603.         End If
  604.         ActiveCell.Offset(1, -5).Activate
  605.     Next m
  606.     GoTo lastline
  607.    
  608. line3:
  609.     For m = 1 To ((dayCount / 2) + 1)
  610.         week2 = (week + (m - 1)) - 1
  611.         If week2 > 7 Then
  612.             week2 = week2 - 7
  613.         End If
  614.         ActiveCell.value = WeekdayName(week2, 2) & ", " & Left(mdate(m), 10)
  615.         ActiveCell.Offset(0, 1).Activate
  616.        
  617.         'LAeq
  618.         If m = 1 Then
  619.             ActiveCell.value = "-"
  620.             ActiveCell.HorizontalAlignment = xlRight
  621.         Else
  622.             ActiveCell.value = LAeqdays(2 * m - 2)
  623.         End If
  624.         ActiveCell.NumberFormat = "0.0"
  625.         ActiveCell.Offset(0, 1).Activate
  626.         If m = (dayCount / 2) + 1 Then
  627.             ActiveCell.value = "-"
  628.             ActiveCell.HorizontalAlignment = xlRight
  629.         Else
  630.             ActiveCell.value = LAeqdays(2 * m - 1)
  631.             ActiveCell.NumberFormat = "0.0"
  632.         End If
  633.         ActiveCell.Offset(0, 1).Activate
  634.        
  635.         'LA90 day
  636.         If Not l90minCol Is Nothing Then
  637.             If m = 1 Then
  638.                 ActiveCell.value = "-"
  639.                 ActiveCell.HorizontalAlignment = xlRight
  640.             Else
  641.                 ActiveCell.value = l90m(2 * m)
  642.             End If
  643.         Else
  644.             ActiveCell.value = "no data"
  645.             ActiveCell.HorizontalAlignment = xlRight
  646.         End If
  647.         ActiveCell.Offset(0, 1).Activate
  648.        
  649.         'LA90 night
  650.         If Not l90minCol Is Nothing Then
  651.             If m = (dayCount / 2) + 1 Then
  652.                 ActiveCell.value = "-"
  653.                 ActiveCell.HorizontalAlignment = xlRight
  654.             Else
  655.                 ActiveCell.value = l90m((2 * m) - 1)
  656.             End If
  657.         Else
  658.             ActiveCell.value = "no data"
  659.             ActiveCell.HorizontalAlignment = xlRight
  660.         End If
  661.         ActiveCell.Offset(0, 1).Activate
  662.        
  663.         '10th LAmax
  664.         If Not LAmaxCol Is Nothing Then
  665.             If m = (dayCount / 2) + 1 Then
  666.                 ActiveCell.value = "-"
  667.                 ActiveCell.HorizontalAlignment = xlRight
  668.             Else
  669.                 ActiveCell.value = LAM2((2 * m) - 1)
  670.             End If
  671.         Else
  672.             ActiveCell.value = "no data"
  673.             ActiveCell.HorizontalAlignment = xlRight
  674.         End If
  675.         ActiveCell.Offset(1, -5).Activate
  676.     Next m
  677.     GoTo lastline
  678.    
  679. Line4:
  680.     For m = 1 To (dayCount + 1) / 2
  681.         week2 = (week + (m - 1)) - 1
  682.         If week2 > 7 Then
  683.             week2 = week2 - 7
  684.         End If
  685.         ActiveCell.value = WeekdayName(week2, 2) & ", " & Left(mdate(m), 10)
  686.         ActiveCell.Offset(0, 1).Activate
  687.        
  688.         'LAeq
  689.         If m = 1 Then
  690.             ActiveCell.value = "-"
  691.             ActiveCell.HorizontalAlignment = xlRight
  692.         Else
  693.             ActiveCell.value = LAeqdays(2 * m - 2)
  694.         End If
  695.         ActiveCell.NumberFormat = "0.0"
  696.         ActiveCell.Offset(0, 1).Activate
  697.         ActiveCell.value = LAeqdays(2 * m - 1)
  698.         ActiveCell.NumberFormat = "0.0"
  699.         ActiveCell.Offset(0, 1).Activate
  700.        
  701.         'LA90 day
  702.         If Not l90minCol Is Nothing Then
  703.             If m = 1 Then
  704.                 ActiveCell.value = "-"
  705.                 ActiveCell.HorizontalAlignment = xlRight
  706.             Else
  707.                 ActiveCell.value = l90m(2 * m)
  708.             End If
  709.         Else
  710.             ActiveCell.value = "no data"
  711.             ActiveCell.HorizontalAlignment = xlRight
  712.         End If
  713.         ActiveCell.Offset(0, 1).Activate
  714.                
  715.         'LA90 night
  716.         If Not l90minCol Is Nothing Then
  717.             ActiveCell.value = l90m((2 * m) - 1)
  718.         Else
  719.             ActiveCell.value = "no data"
  720.             ActiveCell.HorizontalAlignment = xlRight
  721.         End If
  722.         ActiveCell.Offset(0, 1).Activate
  723.        
  724.         'LAmax
  725.         If Not LAmaxCol Is Nothing Then
  726.             ActiveCell.value = LAM2((2 * m) - 1)
  727.             ActiveCell.Offset(0, 1).Activate
  728.         Else
  729.             ActiveCell.value = "no data"
  730.             ActiveCell.HorizontalAlignment = xlRight
  731.         End If
  732.        
  733.         ActiveCell.Offset(1, -5).Activate
  734.     Next m
  735.     GoTo lastline
  736.  
  737. lastline:
  738.    
  739.     ActiveCell.Offset(1, 0).Activate
  740.     ActiveCell.value = "Entire measurement period:"
  741.     ActiveCell.Offset(0, 1).Activate
  742.     ActiveCell.value = dayLogAvgResult
  743.     ActiveCell.NumberFormat = "0.0"
  744.     ActiveCell.Offset(0, 1).Activate
  745.     ActiveCell.value = nightLogAvgResult
  746.     ActiveCell.NumberFormat = "0.0"
  747.     ActiveCell.Offset(0, 1).Activate
  748.     ActiveCell.value = Dayl90minResult
  749.     ActiveCell.NumberFormat = "0.0"
  750.     ActiveCell.Offset(0, 1).Activate
  751.     ActiveCell.value = Nightl90minResult
  752.     ActiveCell.NumberFormat = "0.0"
  753.      ActiveCell.Offset(0, 1).Activate
  754.     ActiveCell.value = NightLAmaxResult
  755.     ActiveCell.Offset(0, -5).Activate
  756.  
  757.     Columns(ActiveCell.Column).AutoFit
  758.     ActiveCell.Offset(0, 1).Activate
  759.     Columns(ActiveCell.Column).AutoFit
  760.     ActiveCell.Offset(0, 1).Activate
  761.     Columns(ActiveCell.Column).AutoFit
  762.     ActiveCell.Offset(0, 1).Activate
  763.     Columns(ActiveCell.Column).AutoFit
  764.     ActiveCell.Offset(0, 1).Activate
  765.     Columns(ActiveCell.Column).AutoFit
  766.     ActiveCell.Offset(0, 1).Activate
  767.     Columns(ActiveCell.Column).AutoFit
  768.          
  769.     'End If
  770.          
  771.          'Create Chart-------------------------------------------
  772.            
  773.     Dim rng As Range
  774.     Dim cht As Object
  775.        
  776.     'Create a chart
  777.              
  778.     Set cht = ActiveSheet.Shapes.AddChart
  779.     cht.Chart.ChartType = xlLine
  780.    
  781.     Do While cht.Chart.SeriesCollection.count > 0
  782.         cht.Chart.SeriesCollection(1).Delete
  783.     Loop
  784.    
  785.     cht.Chart.HasTitle = True
  786.     cht.Chart.ChartTitle.Characters.Text = ""
  787.     cht.Chart.HasTitle = False
  788.    
  789.     Dim xTitle As String
  790.     Dim yTitle As String
  791.    
  792.     yTitle = "Sound Pressure Level - dB re 2 x 10-5 Pa"
  793.     xTitle = "Measurement Start Time"
  794.    
  795.     'X axis name
  796.     cht.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
  797.     cht.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = xTitle
  798.     cht.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 10
  799.     cht.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Bold = False
  800.  
  801.     'y-axis name
  802.     cht.Chart.Axes(xlValue, xlPrimary).HasTitle = True
  803.     cht.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = yTitle
  804.     cht.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters(start:=36, length:=2).Font.Superscript = True
  805.     cht.Chart.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Size = 10
  806.     cht.Chart.Axes(xlValue, xlPrimary).AxisTitle.Format.TextFrame2.TextRange.Font.Bold = False
  807.  
  808.     'Give chart some data
  809.     cht.Chart.Parent.Width = 542
  810.     cht.Chart.Parent.Height = 330
  811.    
  812.     Dim chartposition As Range
  813.    
  814.     If Not XL2 Is Nothing Then
  815.         TimeRng.Offset(-20, counter + 2).Activate
  816.         cht.Chart.Parent.Left = ActiveCell.Left
  817.         cht.Chart.Parent.Top = ActiveCell.Top
  818.        
  819.     Else
  820.         TimeRng.Offset(dayCount + 5, counter + 2).Activate
  821.         cht.Chart.Parent.Left = ActiveCell.Left
  822.         cht.Chart.Parent.Top = ActiveCell.Top
  823.     End If
  824.          
  825.     TimeRng.Activate
  826.  
  827.     Dim c As Integer
  828.     c = 1
  829.                            
  830.     If Not LAmaxCol Is Nothing Then
  831.         cht.Chart.SeriesCollection.NewSeries
  832.         cht.Chart.SeriesCollection(c).Select
  833.         With Selection
  834.             .Name = "LAmax"
  835.             .Values = LAmaxChartRng
  836.             .XValues = TimeChartRng
  837.             .Format.Line.Visible = msoFalse
  838.             .MarkerSize = 3
  839.             .MarkerStyle = xlMarkerStyleX
  840.             .MarkerBackgroundColorIndex = xlColorIndexNone
  841.             .MarkerForegroundColorIndex = 56
  842.             .Format.Line.weight = 0.25
  843.            
  844.         End With
  845.         c = c + 1
  846.     End If
  847.      
  848.         cht.Chart.SeriesCollection.NewSeries
  849.         cht.Chart.SeriesCollection(c).Name = "LAeq"
  850.         cht.Chart.SeriesCollection(c).XValues = TimeChartRng
  851.         cht.Chart.SeriesCollection(c).Values = LAeqChartRng
  852.         cht.Chart.SeriesCollection(c).Border.Color = RGB(31, 73, 125)
  853.         cht.Chart.SeriesCollection(c).MarkerStyle = xlMarkerStyleNone
  854.         cht.Chart.SeriesCollection(c).Format.Line.weight = 1.5
  855.         c = c + 1
  856.                
  857.     If Not l90minCol Is Nothing Then
  858.         cht.Chart.SeriesCollection.NewSeries
  859.         cht.Chart.SeriesCollection(c).Name = "LA90"
  860.         cht.Chart.SeriesCollection(c).XValues = TimeChartRng
  861.         cht.Chart.SeriesCollection(c).Values = LA90ChartRng
  862.         cht.Chart.SeriesCollection(c).Border.Color = RGB(149, 179, 215)
  863.         cht.Chart.SeriesCollection(c).MarkerStyle = xlMarkerStyleNone
  864.         cht.Chart.SeriesCollection(c).Format.Line.weight = 1.5
  865.         c = c + 1
  866.     End If
  867.      
  868.     ActiveChart.Axes(xlCategory).MajorTickMark = xlCross
  869.     ActiveChart.Axes(xlValue).MajorTickMark = xlNone
  870.        
  871.        
  872.     Dim spacing As Integer
  873.     spacing = CInt(((DataHeight / 96#) / 1.1)) * 4
  874.     ActiveChart.Axes(xlCategory).TickLabelSpacing = spacing
  875.     ActiveChart.Axes(xlCategory).TickLabels.Orientation = xlUpward
  876.        
  877.     'add vertical gridlines
  878.     ActiveChart.Axes(xlValue).MajorGridlines.Select
  879.     With Selection.Format.Line
  880.         .Visible = msoTrue
  881.         .ForeColor.ObjectThemeColor = msoThemeColorBackground1
  882.         .ForeColor.TintAndShade = 0
  883.         .ForeColor.Brightness = -0.25
  884.         .Transparency = 0.5
  885.     End With
  886.    
  887.     ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
  888.     ActiveChart.Axes(xlCategory).TickMarkSpacing = spacing
  889.     ActiveChart.Axes(xlCategory).AxisBetweenCategories = False
  890.     ActiveChart.Axes(xlCategory).MajorGridlines.Select
  891.     With Selection.Format.Line
  892.         .Visible = msoTrue
  893.         .ForeColor.ObjectThemeColor = msoThemeColorBackground1
  894.         .ForeColor.TintAndShade = 0
  895.         .ForeColor.Brightness = -0.25
  896.         .Transparency = 0.5
  897.     End With
  898.    
  899.     'Dim Name As String
  900.     'Name = ActiveWorkbook.Name
  901.     'ActiveWorkbook.SaveAs Filename:=Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook
  902.          
  903.     'End If
  904.    startCell.Activate
  905.    Application.ScreenUpdating = True
  906.    
  907. End Sub
  908. Private Sub TrimFirstandLast()
  909.    
  910.    
  911.    
  912.     Dim add1 As String
  913.     Dim add2 As String
  914.     Dim add3 As String
  915.    
  916.     Set TimeRng = ActiveSheet.Range("A1:ZZ10000").Find("Time", lookat:=xlPart)
  917.     If TimeRng Is Nothing Then
  918.         MsgBox "'" & searchstring & "' was not found."
  919.         Exit Sub
  920.     End If
  921.    
  922.     Set XL2 = ActiveSheet.Range("A1:ZZ10000").Find("XL2", lookat:=xlPart)
  923.    
  924.     'This is a hack
  925.     If Not XL2 Is Nothing Then
  926.         Set TimeRng = ActiveSheet.Range("C23")
  927.     End If
  928.        
  929.     If Not XL2 Is Nothing Then
  930.         TimeRng.Offset(2, -1).Activate
  931.         add1 = ActiveCell.Address
  932.         ActiveCell.Offset(1, 0).Activate
  933.     Else
  934.         TimeRng.Offset(1, -1).Activate
  935.         add1 = ActiveCell.Address
  936.         ActiveCell.Offset(1, 0).Activate
  937.     End If
  938.    
  939.     add2 = ActiveCell.Address
  940.    
  941.     Do While Not ActiveCell = ""
  942.         ActiveCell.Offset(1, 0).Activate
  943.     Loop
  944.     ActiveCell.Offset(-1, 0).Activate
  945.    
  946.     Do While Not ActiveCell = ""
  947.         ActiveCell.Offset(0, 1).Activate
  948.     Loop
  949.     ActiveCell.Offset(0, -1).Activate
  950.    
  951.     add3 = ActiveCell.Address
  952.    
  953.     Range(add2 & ":" & add3).Cut Range(add1)
  954.    
  955.     If Not XL2 Is Nothing Then
  956.         TimeRng.Offset(2, -1).Activate
  957.     Else
  958.         TimeRng.Offset(1, -1).Activate
  959.     End If
  960.    
  961.     Do While Not ActiveCell = ""
  962.         ActiveCell.Offset(1, 0).Activate
  963.     Loop
  964.     ActiveCell.Offset(-1, 0).Activate
  965.    
  966.     add1 = ActiveCell.Address
  967.    
  968.     Do While Not ActiveCell = ""
  969.         ActiveCell.Offset(0, 1).Activate
  970.     Loop
  971.     ActiveCell.Offset(0, -1).Activate
  972.    
  973.     add2 = ActiveCell.Address
  974.    
  975.     Range(add1 & ":" & add2).Clear
  976.        
  977. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement