Macro that Adds +1.value to the Cell Above It.

Poker Joe

New Member
Joined
Feb 12, 2015
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
Attempting to write a simple macro that finds the LastRow in Column "CO". In the row below the LastRow, I want to add 1 to the value of the cell above it looping through all the cells in the LastRow to the last column (last cell of the LastRow). I may add more numbers to the LastRow in the future. Once I loop through last cell of the LastRow, I want it to stop looping.

The macro I have below stops at the first cell of the blank row below the last row. I'm stuck.

I'm sure there is a simpler macro that will accomplish my task than the monstrosity I wrote below.

Any suggestions would be appreciated.


LatestNC5_3.10.23.xlsm
COCPCQCRCSCTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDJDKDLDMDNDODPDQDRDSDTDUDVDWDXDYDZEAEBECEDEE
1374
13756111312134311420500062102271317221214961231131341014208518
137671214231454215216111732133824182313151071341232342115219619
1377
1378
SBH (2)





VBA Code:
Sub SBHTest()

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, "CO").End(xlUp).Row ' find the last row of values in column CO
    
    Dim firstBlankRow As Long
    firstBlankRow = lastRow + 1 ' get the first blank row below the last row of values in column CO
    
    Cells(firstBlankRow, "CO").Value = lastRow + 1 ' leave a value only in the first cell of the blank row without the formula
    
    Dim lastCol As Long
    lastCol = Cells(lastRow, Columns.Count).End(xlToLeft).Column ' find the last column of values in the last row
    
    Dim i As Long, j As Long
    For i = lastRow To firstBlankRow Step -1 ' loop through the rows from the last row down to the first blank row below the last row of values in column CO
        For j = 93 To lastCol ' loop through the columns from column CO up to the last column of values in the last row
           [B] If i = firstBlankRow And j = 93 Then[/B]
                ElseIf i = lastRow And j > lastCol Then
                ' do nothing after the last value in the last row
            Else
                Cells(firstBlankRow, j).Value = Cells(lastRow, j).Value + 1 ' leave the value of each cell in the LastRow + 1
            End If
        Next j
    Next i
    
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,
You could test
VBA Code:
Range("yourdestinationRange").FormulaR1C1 = "=R[-1]C+1"
Range("yourdestinationRange").Value = Range("yourdestinationRange").Value
 
Upvote 0
Hey James,

Thanks. I tried that before I attempted a macro but it leaves the formula in the cell and I just want the value. Plus, I want to learn how to write loop macros, For i, For Each, etc. I'll get it figured out eventually.
 
Upvote 0
Thanks. I tried that before I attempted a macro but it leaves the formula in the cell
Leaves the formula in what cell? the code @James006 posted converts the formula he put in the destination cell to values in the 2nd line.

It is not a good example to use for learning loops as you would normally just do something like....

VBA Code:
Sub SBHTest()

    Dim lastRow As Long, firstBlankRow As Long, lastCol As Long
   
    lastRow = Cells(Rows.Count, "CO").End(xlUp).Row ' find the last row of values in column CO
    firstBlankRow = lastRow + 1                  ' get the first blank row below the last row of values in column CO
    lastCol = Cells(lastRow, Columns.Count).End(xlToLeft).Column ' find the last column of values in the last row
   
    With Cells(firstBlankRow, "CO").Resize(, lastCol - Range("CO:CO").Column + 1)
        .FormulaR1C1 = "=R[-1]C+1"
        .Value = .Value
    End With

End Sub
 
Upvote 1
Solution
Leaves the formula in what cell? the code @James006 posted converts the formula he put in the destination cell to values in the 2nd line.

It is not a good example to use for learning loops as you would normally just do something like....

VBA Code:
Sub SBHTest()

    Dim lastRow As Long, firstBlankRow As Long, lastCol As Long
  
    lastRow = Cells(Rows.Count, "CO").End(xlUp).Row ' find the last row of values in column CO
    firstBlankRow = lastRow + 1                  ' get the first blank row below the last row of values in column CO
    lastCol = Cells(lastRow, Columns.Count).End(xlToLeft).Column ' find the last column of values in the last row
  
    With Cells(firstBlankRow, "CO").Resize(, lastCol - Range("CO:CO").Column + 1)
        .FormulaR1C1 = "=R[-1]C+1"
        .Value = .Value
    End With

End Sub
Ah! That worked perfectly.
Here is where I made my error believing James' suggestion left a formula in the cell. I was stepping through (F8) the code and when I saw (side by side view) values appear on the blank line below the last row, I clicked on one of them pausing the stepping through but not stepping out of the step through. It showed the formula in the Formula Bar, so, instantly I thought they would stay after the macro completed. When I allowed it to run unto completion, the formulas disappear and the values are all that's in the cell.

Thanks James and Mark. I'm still learning and have a long road ahead. Your help and patience is greatly appreciated.


 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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