0

here is a screenshot of my dataI have a set of measurements. Their related timestamps are in text format, like this: 12/23/2021 2:00:00 AM. My goal is to calculate a daily average of my measurements. I have this code but it stops in consolidate step. Does anyone know how to fix it:

Sub consolidate()
    Dim folderPath As String
    Dim filename As String
    Dim wkb As Workbook
    folderPath = "F:\analysis\12hourly\"
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
    filename = Dir(folderPath & "*.xls")
    Do While filename <> ""
      Application.ScreenUpdating = False
        Set wkb = Workbooks.Open(folderPath & filename)          'Open all files in directory
        wkb.Activate
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp                             'Delete first row
        Dim Lastrow As Integer
        Lastrow = Range("A" & Rows.Count).End(xlUp).Row
        Lastrow2 = Range("B" & Rows.Count).End(xlUp).Row
        Dim D, E
        D = Mid("A3:Lastrow", 1, 10)                             'Remove hour & minute
        Dim wkbr As Workbook
        Set wkbr = Workbooks.Add
        Dim rng As Range
        Set rng = wkrb.Sheets("Sheet1").Cells(1, 1)
        wkb.Activate
        Dim ConsolidateRangeArray As Variant                     'Daily average 
        ConsolidateRangeArray = Array(D, "B3:Lastrow2")
        rng.consolidate _
          Sources:=ConsolidateRangeArray, _
          Function:=xlAverage, TopRow:=False, LeftColumn:=True, CreateLinks:=False
        Dim wkbpath As String
        Dim wkbname As String
        wkb.Activate
        wkbpath = "F:\analysis\2daily\"                   'Save result in folder daily
        wkbname = ActiveWorkbook.Name
        ActiveWorkbook.SaveAs filename:= _
          wkbpath & wkbname & ".xlsx", FileFormat:=xlCSVUTF8 _
          , CreateBackup:=False
        ActiveWorkbook.Close SaveChanges:=False
        Application.DisplayAlerts = False
        filename = Dir
    Loop
  Application.ScreenUpdating = True

End Sub
Parastu
  • 1
  • 1
  • What are you trying to do with `D = Mid("A3:Lastrow", 1, 10)` ?? Because that does not do what you think it does. – braX Jan 03 '22 at 10:19
  • This also is not going to do what you want: `Array(D, "B3:Lastrow2")` – braX Jan 03 '22 at 10:25
  • I am new to VBA. I try to read each cell in range A3 to last non-empty row and delete the hour and keep date. – Parastu Jan 03 '22 at 10:42
  • You want to "remove hour & minute" or "remove date"? If you are keeping date then what are you trying to average? – Siddharth Rout Jan 03 '22 at 11:01
  • Also can you share a screenshot of how your data looks like? – Siddharth Rout Jan 03 '22 at 11:03
  • there are several measurements in each date that are stored with hour and minute. I need daily average. – Parastu Jan 03 '22 at 11:05
  • **1.** You have to use R1C1 notation when using `Range.Consolidate`. You may want to read up on [Range.Consolidate method (Excel)](https://learn.microsoft.com/en-us/office/vba/api/excel.range.consolidate) **2.** Avoid declaring the variables, objects inside the loop. Do it once outside. Contd... – Siddharth Rout Jan 03 '22 at 17:21
  • **3** To get the text before the space, you can use the formula `=LEFT(A3,(FIND(" ",A3,1)-1))`. Now you can either do this in a loop or in one go as explained [Here](https://stackoverflow.com/questions/19985895/convert-an-entire-range-to-uppercase-without-looping-through-all-the-cells). Something like `Range("A3:A" & LastRow).Value = Evaluate("index(LEFT(" & Range("A3:A" & LastRow).Address & ",(FIND("" ""," & Range("A3:A" & LastRow).Address & ",1)-1)),)")` **4.** Try and adapt the above suggestions and share the new code that you try and we will take it from there. – Siddharth Rout Jan 03 '22 at 17:22
  • Thanks for your kind and helpful comments. – Parastu Jan 04 '22 at 10:43

0 Answers0