Sub continuing beyond end of used range

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Can someone help me understand why the code below keeps adding one extra line of data (000000000) to the bottom of my dataset every time it's run and what I need to do to stop it?

Thanks, y'all!

Code:
  With ActiveSheet
    .UsedRange  ' ~ Reset used range
    Set rngUsed = rng_ActualUsed()  ' ~~ determine actual used range in each sheet
    Set rngX = Range("A1:" & Cells(1, rngUsed.Columns.Count).Address)  ' ~~ Set for range of header row | cells function gives the last cell in row 1
    
    For Each cell In rngX
      With cell
        Select Case True
        
        Case .value Like "SSN"
          For Each cellx In Intersect(cell.EntireColumn, rngUsed).Offset(1, 0)  ' ~~ confines column of corresponding header
            cellx = WorksheetFunction.Substitute(cellx, "-", vbNullString)  ' remove dashes
            cellx.NumberFormat = "@" ' Format as text -- REQUIRED!!
            cellx = Application.Text(cellx, "000000000")  ' format as text but retain leading zeros
          Next cellx
        End Select
      End With
    Next cell
  End With
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Range.Offset moves your Range down by 1 row, it does not shrink it. So A1:A10 would become A2:A11. You need to do something like the following to both move and shrink your range:

Code:
Range.Offset(1,0).Resize(Range.rows.count-1, 0)
 
Upvote 0
LockeGarmin,

Thanks so much!! I hadn't thought to apply both simultaneously (I guess I hadn't ever seen it done). I had tried reducing the Offset value, which fixed the addition of a new last row issue but then gorked up the header.

With some tinkering, I found that the code should read:
Code:
Range.Offset(1,0).Resize(Range.rows.count-1)
Otherwise, the number of columns in the range get set to zero (thanks to Bill Jelen and MSDN).
 
Last edited:
Upvote 0
Oops! You're right. I was following the pattern in Offset and forgot about the ramifications of using 0 in resize. :) On another note you can actually just use .Offset(1) instead of .Offset(1,0) to representing moving 1 row down if you wanted. Glad I could help!
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top