Solved

Macro not working.  Need last used row in spreadsheet.

Posted on 2011-02-10
2
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

737 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