copy cell paste to last row when last row and last column are ever changing

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I'm sure everyone is tired of my posts ?

Working on a column that is ever changing is really testing me.

Back to this -

Copy of Area 84 IND Sept-October.xlsm
BCDEFGH
1
2600336A3600336PL600336SSIFU600336SSIVD600336VIQS601036FTotal
385730623
4110100
5220100
6420000
7120101
8130000
9100111
10043112
11020000
12006001
13001021
14000101
15000200
report
Cell Formulas
RangeFormula
B3:G15B3=COUNTIFS(data!$H:$H,$A3,data!$A:$A,B$2)
H3H3=SUM($B3:F3)
Named Ranges
NameRefers ToCells
data!_FilterDatabase=data!$A$3:$AI$3B3:G15


This is my code
VBA Code:
    Dim Lrw As Long, lcol As Long
    Dim rng As Range
    
    Set rng = Range("A2")
    
    Lrw = Cells(Rows.Count, "B").End(xlUp).Row
    lcol = Cells(2, Columns.Count).End(xlToLeft).Column
    
    Range("B3", Cells(Lrw, lcol)).FillRight

    rng.Offset(0, lcol).Value = "Total"
    
    rng.Offset(1, lcol - 1).FormulaR1C1 = "=sum(rc2:rc[-1])"

What is the best way to copy the last cell on row 3 and copy it all the way down to the last row? Does that even make sense?

Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Possibly add these lines to your VBA
VBA Code:
Dim mCol As Long
Dim mRow As Long
'====Get last row number - offset by 1 down.
mRow = Cells(1048576, 2).End(xlUp).Offset(1, 0).Row
'====Get Furthest right column number
mCol = Cells(3, 2).End(xlToRight).Column
'====Take value from Row3 furthest column Right and move it down to last row same column.
Cells(mRow, mCol).Value = Cells(3, mCol).Value
 
Upvote 0
Possibly add these lines to your VBA
VBA Code:
Dim mCol As Long
Dim mRow As Long
'====Get last row number - offset by 1 down.
mRow = Cells(1048576, 2).End(xlUp).Offset(1, 0).Row
'====Get Furthest right column number
mCol = Cells(3, 2).End(xlToRight).Column
'====Take value from Row3 furthest column Right and move it down to last row same column.
Cells(mRow, mCol).Value = Cells(3, mCol).Value
Oddly, that copied the value in H3 and pasted it in H16.
Copy of Area 84 IND Sept-October.xlsm
BCDEFGH
1
2600336A3600336PL600336SSIFU600336SSIVD600336VIQS601036FTotal
385730629
4110100
5220100
6420000
7120101
8130000
9100111
10043112
11020000
12006001
13001021
14000101
15000200
1629
report
Cell Formulas
RangeFormula
B3:G15B3=COUNTIFS(data!$H:$H,$A3,data!$A:$A,B$2)
H3H3=SUM($B3:G3)
Named Ranges
NameRefers ToCells
data!_FilterDatabase=data!$A$3:$AI$3B3:G15


I think, and forgive me I'm new, that a loop would be good somehow.

Using this
VBA Code:
    Dim lcol As Long
    Dim rng As Range
    
    Set rng = Range("A2")
    
    lcol = Cells(2, Columns.Count).End(xlToLeft).Column
    
    rng.Offset(1, lcol).FormulaR1C1 = "=sum(rc2:rc[-1])"

something like if rng.Offset(i, lcol).FormulaR1C1 = "=sum(rc" & i +1 & ":rc[-1])"

I can't figure out the part where it would look in rng.Offset(i, lcol -1).
 
Upvote 0
I came up with something that almost gets me to the finish line.

VBA Code:
    Dim LR As Long
    
    Dim lcol As Long
    Dim rng As Range
    
    Set rng = Range("A2")
    
    lcol = Cells(2, Columns.Count).End(xlToLeft).Column
    
    rng.Offset(1, lcol - 1).FormulaR1C1 = "=sum(rc2:rc[-1])"
    
    'LR = Cells(Rows.Count, 1).End(xlUp).Row

    For i = 1 To 100
        If rng.Offset(i, lcol - 2).Value > -1 Then
        rng.Offset(i, lcol - 1).FormulaR1C1 = "=sum(rc2:rc[-1])"
        End If
    Next i

The snag is I want to stop the loop at the last row. I have 1 to 100 to test it. I need help on how to replace the 100 with finding the last row.

I tried another way using If rng.Offset(i, lcol - 2).Value = "" Then End If, I thought that could stop the loop.
 
Upvote 0
I don't know if there was a better way, but this worked

VBA Code:
    Dim lcol As Long
    Dim rng As Range
    Dim lrow As Long
    
    Set rng = Range("A2")
    
    lcol = Cells(2, Columns.Count).End(xlToLeft).Column
    lrow = Cells(Rows.Count, "A").End(xlUp).Row
    
    For i = 1 To lrow - 2
        If rng.Offset(i, lcol - 2).Value > -1 Then
        rng.Offset(i, lcol - 1).FormulaR1C1 = "=sum(rc2:rc[-1])"
        End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,214,825
Messages
6,121,787
Members
449,049
Latest member
greyangel23

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