Solved

Macro not working.  Need last used row in spreadsheet.

Posted on 2011-02-10
2
333 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

735 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