Solved

Macro not working.  Need last used row in spreadsheet.

Posted on 2011-02-10
2
330 Views
Last Modified: 2012-05-11
I've been using this macro to fix the leading zero on the zip codes stored in column M.  Now that I've needed to move to Excel '07 due to having files that no longer fit in the older version of Excel I need to update this macro to use the ACTUAL last row instead of a hard-coded number (65536).  However, I've tried several different methods to find the last row and the macro refuses to work.

Among other things I've attempted:
Dim LR as long
LR = ActiveSheet.UsedRange.Rows.Count
set rg = Range(Cells(2, col), Cells(LR, col).end(xlUp))

or

Dim LR as long
LR = Cells.SpecialCells(xlCellTypeLastCell).Row
set rg = Range(Cells(2, col), Cells(LR, col).end(xlUp))

or

set rg = Range(Cells(2, col), Cells(ActiveSheet.UsedRange.Rows.Count, col).end(xlUp))

or

set rg = Range(Cells(2, col), Cells(Cells.SpecialCells(xlCellTypeLastCell).Row, col).end(xlUp))

Nothing seems to work aside from typing in a hard-coded number.
Sub ZipCoder3()
Dim rg As Range
Dim col As Long
Application.ScreenUpdating = False
col = Columns("M").Column
Columns(col + 1).Insert
Set rg = Range(Cells(2, col), Cells(65536, col).End(xlUp))
rg.NumberFormat = "00000"
rg.Offset(0, 1).FormulaR1C1 = "=LEFT(RC[-1],5)"
rg.Formula = rg.Offset(0, 1).Value
rg.Offset(0, 1).EntireColumn.Delete
Application.ScreenUpdating = True
End Sub

Open in new window

0
Comment
Question by:Rossamino
2 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 250 total points
ID: 34863907
Try substituting Rows.Count for 65536 in line 7.
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34864271
Rossamino,

Try this format:

With Sheets("Sheet1")
    Set rg = .Range(.Cells(2, "M"), .Cells(.rows.count, "M").End(xlUp))
End With

Note the leading periods. Also it's important to spefify the worksheet on which this is to work, otherwise it will apply to whatever worksheet you are currently on. Change With Sheets("Sheet1") to whatever is correct for your workbook.

Patrick
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

14 Experts available now in Live!

Get 1:1 Help Now