Sub BoxCounting(Optional ByVal in_matrix As Variant, Optional ByVal out_matrix As Variant, Optional ByVal op_plota As Variant) Dim i As Integer, j As Integer, k As Integer Dim il As Integer, jl As Integer Dim power As Integer, expo As Integer Dim pts() As Single Dim width As Integer, length As Integer, height As Single, displace As Single Dim rmax As Single, rmin As Single Dim passo As Integer Dim r As Range, s As Range, o As Range Dim plota As Boolean If IsMissing(in_matrix) Then Set r = Application.InputBox(prompt:="Enter Input Matrix:", Type:=8) Set s = Application.InputBox(prompt:="Enter Output range:", Type:=8) plota = True Else Set r = in_matrix Set s = out_matrix plota = op_plota End If Application.Calculation = xlCalculationManual Application.ScreenUpdating = False width = r.Columns.Count length = r.Rows.Count 'acha o maximo e o minimo For i = 1 To length For j = 1 To width If r.Cells(i, j) <> "" Then If r.Cells(i, j).Interior.Color > rmax Then rmax = r.Cells(i, j).Interior.Color If r.Cells(i, j).Interior.Color < rmin Then rmin = r.Cells(i, j).Interior.Color End If Next j Next i power = WorksheetFunction.Max(width, length) - 1 For expo = 0 To 100 If power <= 2 ^ expo Then Exit For Next expo 'expo = Int(Log(max(width, length)) / Log(2)) power = 2 ^ expo ReDim nbox(power), pts(power, power) For i = 1 To length For j = 1 To width pts(i - 1, j - 1) = r.Cells(i, j).Interior.Color Next j Next i displace = (rmax - rmin) displace = displace + 1 / 2 / power 'faz o mei?o expo = 0 i = 1 Do expo = expo + 1 passo = (power / i) / 2 k = passo Do j = passo Do rmax = 0 For il = k - passo To k + passo For jl = j - passo To j + passo If pts(il, jl) > rmax Then rmax = pts(il, jl) Next jl Next il If (rmax - rmin) > 0 Then nbox(expo) = nbox(expo) + Int((rmax - rmin) / displace) + 1 j = j + power / i Loop While j < power k = k + power / i Loop While k < power displace = displace / 2 i = i * 2 Loop While i <= power For i = 1 To expo s.Cells(i, 1) = Log(1 / (power / (2 ^ (i - 1)))) / Log(10) s.Cells(i, 2) = Log(nbox(i)) / Log(10) Next i Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = False Application.Calculate End Sub