Run code in last column

jocker_boy

Board Regular
Joined
Feb 5, 2015
Messages
83
Hello,

I'm trying to run the code in last column.
Part of the code is perfect. Until this line it works good: Range(Cells(6, lc), Cells(lr, lc)).FormulaR1C1 = "=R[0]C[-1]*" & Application.ConvertFormula("$J6", xlA1, xlR1C1)

Then in second part of the code i would like to replace the range "EE" with "lc" variable. But i don't know how.

Thanks for the help.
Gonçalo

VBA Code:
Sub test()

'Populate Auxiliar Columns
    Dim lr As Long
    Dim lc As Long
  
'Group Columns
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=8

'   Find last row in column C with data
    lr = Cells(Rows.Count, "C").End(xlUp).row
    
'   Find last column in row 5 with data
    lc = Cells(5, columns.Count).End(xlToLeft).Column

'Ungroup Columns
    ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1

    Range(Cells(6, lc + 3), Cells(6, lc + 3)).Select
    Range(Cells(6, lc), Cells(lr, lc)).FormulaR1C1 = "=R[0]C[-1]*" & Application.ConvertFormula("$J6", xlA1, xlR1C1)


Dim r As Long, r2 As Long, last_row As Long
Dim next_row As Long, current_len As Long, test_len As Long
Dim Rng As String

With ActiveSheet

last_row = .Cells(Rows.Count, 1).End(xlUp).row

For r = 6 To lr
    next_row = r + 1

    If .Range("B" & next_row) > .Range("B" & r) Then
       current_len = .Range("B" & r)
      
       'create range
       For r2 = r + 1 To last_row
            test_len = .Range("B" & r2)
            If current_len >= test_len Then
                Rng = "EE" & r + 1 & ":" & "EE" & r2 - 1
                Exit For
            End If
        Next
    
        .Range("EE" & r).Formula = "=SUBTOTAL(9," & Rng & ")"
    End If
    
Next

End With

End Sub
 
By the way, I changed the "solution" post.
You want to mark the reply that answered the original question asked, not any follow-up questions.
So I marked the original one you had posted as the solution.
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top