Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

find last row  in a column and clear contents.

Posted on 2011-02-28
11
Medium Priority
?
264 Views
Last Modified: 2012-05-11
experts help,

I'm looking for excel VBA code to find the last row in column "b" and clear the contents of each row after the the last row.
0
Comment
Question by:allwork32
11 Comments
 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 900 total points
ID: 35003184
Try this

Sub Sample()
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row + 1
    Sheets("Sheet1").Range("B" & LastRow & ":B" & Rows.Count).ClearContents
End Sub

Open in new window


Sid
0
 
LVL 10

Assisted Solution

by:Makrini
Makrini earned 100 total points
ID: 35003192
Sub tester()
    Dim lastrow As Long
    lastrow = Range("B1000000").End(xlUp).Row
    Rows(lastrow + 1 & ":1000000").ClearContents
End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35003201
Makrini: That won't work if the user has rows filled up till say B100001. Also the code will not work in Excel 2003 ;)

You should always avoid hard-coding rows :)

Sid
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Makrini
ID: 35003204
agreed - mine was a quick dirty solution, didn't see you had replied till I hit send
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35003206
That's ok :) No harm done :)

Sid
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 900 total points
ID: 35003216
Modified Code.

Sub tester()
    Dim lastrow As Long
    lastrow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
    Sheets("Sheet1").Rows(lastrow & ":" & Rows.Count).ClearContents
End Sub

Open in new window


Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35003218
@allwork32: If you want to clear all rows then use the code in ID: 35003216 (Based on Markini's code) and if you want to clear all rows in Col B then use the code in ID: 35003184

Sid
      
0
 
LVL 10

Expert Comment

by:Makrini
ID: 35003245
:)  - good work
0
 
LVL 1

Author Comment

by:allwork32
ID: 35003330
thank you everyone, i'll check it out when I return to work in the morning.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 35003759
@Sid - would not the following be more correct?

    Sheets("Sheet1").Rows(lastrow+1 & ":" & Rows.Count).ClearContents

otherwise, lose that last row which has data?

dave
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35003770
Yes. I missed that in the 2nd code. I did incorporate that in the 1st one though :)

Sid
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question