Solved

find last row  in a column and clear contents.

Posted on 2011-02-28
11
219 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 225 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 25 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Accepted Solution

by:
SiddharthRout earned 225 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 41

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now