Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Macro not working.  Need last used row in spreadsheet.

Posted on 2011-02-10
2
Medium Priority
?
337 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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Six Sigma Control Plans
Introduction to Processes

715 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