Can you help me debug this code?

saftawy1

Board Regular
Joined
Oct 12, 2021
Messages
65
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Dim lastRow As Long, i as long
lastRow = ThisWorkbook.Sheets("report").Range("b1000000").End(xlUp).Row
lastRow = lastRow - 1
i = lastRow
ThisWorkbook.Sheets("report").Range("c&i:p&i").Formula = "=SUBTOTAL(109,$c$2:i)"

What is meant by the code is to add a sum to specific columns starting from the last row containing data




test-total column.xlsb
BCDEFGHIJKLMNOP
1p1p2p3p4p5p6p7p8p9p10p11p12p13p14p15
2111111111111222
3111111111111222
4111111111111222
5111111111111222
6111111111111222
7111111111111222
8total
report
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B7:P7Expression=$AT7="هديه"textNO
B7:P7Expression=$AT7="مقعد مجانى"textNO
B7:P7Expression=$AT7="مجمد"textNO
B7:P7Expression=$AT7="comp"textNO
B7:P7Expression=$AT7="pc"textNO
B7:P7Expression=$AT7="cxl"textNO
B7:P7Expression=$AT7="cash"textNO
B6:P6Expression=$AT6="هديه"textNO
B6:P6Expression=$AT6="مقعد مجانى"textNO
B6:P6Expression=$AT6="مجمد"textNO
B6:P6Expression=$AT6="comp"textNO
B6:P6Expression=$AT6="pc"textNO
B6:P6Expression=$AT6="cxl"textNO
B6:P6Expression=$AT6="cash"textNO
B5:P5Expression=$AT5="هديه"textNO
B5:P5Expression=$AT5="مقعد مجانى"textNO
B5:P5Expression=$AT5="مجمد"textNO
B5:P5Expression=$AT5="comp"textNO
B5:P5Expression=$AT5="pc"textNO
B5:P5Expression=$AT5="cxl"textNO
B5:P5Expression=$AT5="cash"textNO
B4:P4Expression=$AT4="هديه"textNO
B4:P4Expression=$AT4="مقعد مجانى"textNO
B4:P4Expression=$AT4="مجمد"textNO
B4:P4Expression=$AT4="comp"textNO
B4:P4Expression=$AT4="pc"textNO
B4:P4Expression=$AT4="cxl"textNO
B4:P4Expression=$AT4="cash"textNO
B3:P3Expression=$AT3="هديه"textNO
B3:P3Expression=$AT3="مقعد مجانى"textNO
B3:P3Expression=$AT3="مجمد"textNO
B3:P3Expression=$AT3="comp"textNO
B3:P3Expression=$AT3="pc"textNO
B3:P3Expression=$AT3="cxl"textNO
B3:P3Expression=$AT3="cash"textNO
B2:P2Expression=$AT2="هديه"textNO
B2:P2Expression=$AT2="مقعد مجانى"textNO
B2:P2Expression=$AT2="مجمد"textNO
B2:P2Expression=$AT2="comp"textNO
B2:P2Expression=$AT2="pc"textNO
B2:P2Expression=$AT2="cxl"textNO
B2:P2Expression=$AT2="cash"textNO
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
See if this is what you had in mind:
VBA Code:
Sub SubTotalColumn()

    Dim lastRow As Long, lastRowData As Long
    'lastRow = ThisWorkbook.Sheets("report").Range("b1000000").End(xlUp).Row
    lastRow = ThisWorkbook.Sheets("report").Range("b" & Rows.Count).End(xlUp).Row
    lastRowData = lastRow - 1
    
    With ThisWorkbook.Sheets("report")
        .Range(.Cells(lastRow, "C"), .Cells(lastRow, "P")).Formula = "=SUBTOTAL(109,C$2:C$" & lastRowData & ")"
    End With

End Sub
 
Upvote 0
Solution
See if this is what you had in mind:
VBA Code:
Sub SubTotalColumn()

    Dim lastRow As Long, lastRowData As Long
    'lastRow = ThisWorkbook.Sheets("report").Range("b1000000").End(xlUp).Row
    lastRow = ThisWorkbook.Sheets("report").Range("b" & Rows.Count).End(xlUp).Row
    lastRowData = lastRow - 1
   
    With ThisWorkbook.Sheets("report")
        .Range(.Cells(lastRow, "C"), .Cells(lastRow, "P")).Formula = "=SUBTOTAL(109,C$2:C$" & lastRowData & ")"
    End With

End Sub
Thank you very much

Alex Blakenburg

its excelent code
 
Upvote 0

Forum statistics

Threads
1,224,299
Messages
6,177,754
Members
452,798
Latest member
mlance617

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