0

I am looking to copy the last cell with value in a column (it is a formula that returns a value). The only problem is that after the last value in a specific column, there are more formulas that return a blank value.

When I use this code :

LastRow = Cells(Rows.Count, "C").End(xlUp).Row
Cells(LastRow, "C").Copy

it copies the last cell that contains a formula so it copies a blank cell.. I want it to copy the last cell that shows a value.

Thanks for the help!

Mikku
  • 6,538
  • 3
  • 15
  • 38
TristB
  • 17
  • 6
  • To clarify, the cell has a formula in it, but the formula is returning a blank. You want to then keep going up rows (so from row 20, to 19 to 18 etc) until the formula returns a non blank value. Then you want to copy **that** formula down? – BruceWayne Aug 02 '19 at 15:18
  • Yes exactly! I want to copy the last cell of the column that returns a value – TristB Aug 02 '19 at 15:23
  • Thinking about this -- Is the formula the same in all rows? Or to be super clear, do you want to copy the *value* and not the *formula* down 30 rows? – BruceWayne Aug 02 '19 at 15:25
  • The formula is the same in all the rows of a column, but different in each column. I want to copy the formula not the value. Would this work? : SpecialCells(Type:=xlCellTypeConstants, Value:=xlTextValues) – TristB Aug 02 '19 at 15:30

3 Answers3

1

I think this is what you'd be looking for (also incorporating code from your previous question):

For col = 1 To lastCol
    If .Cells(1, col).Value = "F" Then
        'lastRow = .Cells(.Rows.Count, col).End(xlUp).Row
        lastRow = .Columns(col).Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows, LookIn:=xlValues).Row
        .Range(.Cells(lastRow, col), .Cells(lastRow + copyRowAmt, col)).Formula = _
            .Cells(lastRow, col).Formula
    End If
Next col
BruceWayne
  • 22,923
  • 15
  • 65
  • 110
0

You can use the SpecialCells feature of Range Class in this case. A bit tricky but will do the Job efficiently.

Try:

LastRow = Cells(Rows.Count, "C").End(xlUp).row
LastRow = Split(Range("C1:C" & LastRow).SpecialCells(xlCellTypeConstants).Address, "$")(4)
Cells(LastRow, "C").Copy

Updated:

For c = 1 To 20

    LastRow = Cells(Rows.Count, c).End(xlUp).row
    LastRow = Split(Range(Cells(1, c), Cells(LastRow, c)).SpecialCells(xlCellTypeConstants).Address, "$")(4)
    Cells(LastRow, c).Copy

Next c
Mikku
  • 6,538
  • 3
  • 15
  • 38
0

@Mikku - Thank you for the answer! The only thing now is that I am looping through columns in a worksheet. My code really is :

For c = 1 To 20
    LastRow = Cells(Rows.Count, c).End(xlUp).Row
    LastRow = Split(Range(c & LastRow).SpecialCells(xlCellTypeConstants).Address, "$")(4)
    Cells(LastRow, c).Copy

Next c

So how would I insert your code in my loop?

Thanks!

TristB
  • 17
  • 6