Hi Guys,
I am getting, Run-time Error 5 "Invalid Procedure Call or Argument" at the this part of the code.
"o = Left(Cells(a, b).Value, (InStr(1, Cells(a, b).Value, "-") - 1))" . I am not able to figure out the problem.
Option Explicit
Sub bucket_range()
Dim wb As Workbook
Dim wmain As Worksheet
Dim wrough As Worksheet
Dim wpreinvpiv As Worksheet
Dim i As Long
Dim result As Variant
Dim lastrow As Long
Dim j As Long
Dim k As Long
Dim l As Long
Dim pivot_rng As Range
Dim m As Long
Dim n As Long
Dim o As String
Dim p As Long
Dim a As Long
Dim b As Long
Dim c As Long
Set wb = Application.ActiveWorkbook
Set wmain = wb.Sheets("main")
Set wrough = wb.Sheets("main_rough")
Set wpreinvpiv = wb.Sheets("Inv Prev Day Pivot")
wmain.Activate
Range(Range("D4"), Range("d4").End(xlToRight)).Select
Selection.ClearContents
wpreinvpiv.Activate
m = Range("A" & Rows.Count).End(xlUp).Row
Set pivot_rng = wpreinvpiv.Range(Cells(2, 1), Cells(m - 1, 1))
l = Application.Max(pivot_rng)
wmain.Activate
i = ActiveSheet.Cells(2, 3).Value
j = 0
k = 4
Do Until k > (Int(l / i) + 4)
If k <= (Int(l / i) + 3) And (i * (j + 1)) <= l Then
Cells(4, k).Value = i * j & "-" & i * (j + 1)
Else: Cells(4, k).Value = i * j & "-" & "above"
End If
k = k + 1
j = j + 1
Loop
wmain.Activate
a = 4
b = 4
c = Range(Range("d4"), Range("d4").End(xlToRight)).Count
Do Until b > 3 + c
o = Left(Cells(a, b).Value, (InStr(1, Cells(a, b).Value, "-") - 1))
p = Right(Cells(a, b).Value, Len(Cells(a, b)) - InStr(1, Cells(a, b), "-"))
wpreinvpiv.Activate
wmain.Cells(a + 1, b).Value = Application.SumIfs(Range(Cells(2, 2), Cells(m - 1, 2)), Range(Cells(2, 1), Cells(m - 1, 1)), ">=" & o, Range(Cells(2, 1), Cells(m - 1, 1)), "<" & p)
b = b + 1
Loop
End Sub
Bookmarks