VBA Find Last Populated Cell In An Area Then Perform Tasks

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Good morning all,

I am after some help with VBA to perform the follow tasks


  1. Find the last populated cell within range C4 to C174
  2. Copy the last populated row from A to H
  3. Paste values @ A174 to H174
  4. Delete the original range copied
  5. Delete all rows based on C being blank in range C4 to C174

Any help you can provide would be greatly appreciated

Thanks in advance
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Dim cel As Range
If [C174] <> "" Then
    Set cel = [C174]
Else: Set cel = [C174].End(xlUp)
End If
With cel(1, -1).Resize(, 8)
    .Copy [A174]
    .ClearContents
End With
On Error Resume Next
[C4:C174].SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
 
Upvote 0
I've been beaten to it but i've written it so I'll post it!

Code:
Sub CopyPasteDelete()
    Dim lr As Long ' last used row
    
    lr = Cells(174, "C").End(xlUp).Row
    
    Range("A" & lr & ":H" & lr).Copy Range("A174")
    Rows(lr).Delete
    
    For i = 174 To 4 Step -1
        If Cells(i, "C") = "" Then Rows(i).Delete
    Next i
    
End Sub
 
Upvote 0
Works a treat thanks, the only issue I have is I am losing the formatting hence the reason I wanted to paste the copied values in cells A174 to H174 the remove all rows with blanks.
 
Upvote 0
Change how it is pasted:

Code:
Sub CopyPasteDelete()
    Dim lr As Long ' last used row
    
    lr = Cells(174, "C").End(xlUp).Row
    
    Range("A" & lr & ":H" & lr).Copy
[COLOR=#ff0000]    Range("A174").PasteSpecial xlPasteAll[/COLOR]
    Rows(lr).Delete
    
    For i = 174 To 4 Step -1
        If Cells(i, "C") = "" Then Rows(i).Delete
    Next i
    
End Sub
 
Upvote 0
I have changed it now and it works a treat thank you

Change how it is pasted:

Code:
Sub CopyPasteDelete()
    Dim lr As Long ' last used row
    
    lr = Cells(174, "C").End(xlUp).Row
    
    Range("A" & lr & ":H" & lr).Copy
[COLOR=#ff0000]    Range("A174").PasteSpecial xlPasteAll[/COLOR]
    Rows(lr).Delete
    
    For i = 174 To 4 Step
 
Upvote 0
This works great and is quite quick, the only issue I have is the reason for copying and the pasting values was so that I do not lose the formatting set-up

Can you help with this.


Code:
Dim cel As Range
If [C174] <> "" Then
    Set cel = [C174]
Else: Set cel = [C174].End(xlUp)
End If
With cel(1,
 
Upvote 0
For footoo's version, it's the same thing. Instead of copying and pasting in the same line of code, do it separate:

Code:
Dim cel As RangeIf [C174] <> "" Then
    Set cel = [C174]
Else: Set cel = [C174].End(xlUp)
End If
With cel(1, -1).Resize(, 8)
    .Copy
    [A174][COLOR=#FF0000].PasteSpecial xlPasteAll[/COLOR]
    .ClearContents
End With
On Error Resume Next
[C4:C174].SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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