There must be a way to shorten this!!

m_in_spain

Board Regular
Joined
Sep 28, 2018
Messages
55
I am sure there is a way to shorten this into a loop..but each time I try it doesn't not move down the BY column, just clears the first name. There are many cells that will already be blank with no name, hence the error resume next.
As ever help is appreciated. (sorry there are 100 lines of this!! :biggrin::biggrin:

Code:
Sub deletenames()Application.DisplayAlerts = False
Sheets("calcs").Select
On Error Resume Next
   Range("BY3").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY4").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY5").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY6").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY7").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY8").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY9").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY10").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY11").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY12").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY13").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY14").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY15").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY16").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY17").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY18").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY19").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY20").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY21").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY22").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY23").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY24").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY25").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY26").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY27").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY28").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY29").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY30").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY31").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY32").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY33").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY34").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY35").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY36").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY37").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY38").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY39").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY40").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY41").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY42").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY43").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY44").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY45").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY46").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY48").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY48").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY49").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY50").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY51").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY52").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY53").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY54").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY55").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY56").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY57").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY58").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY59").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY60").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY61").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY62").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY63").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY64").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY65").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY66").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY67").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY68").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY69").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY70").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY71").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY72").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY73").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY74").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY75").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY76").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY77").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY78").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY79").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY80").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY81").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY82").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY83").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY84").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY85").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY86").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY87").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY88").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY89").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY90").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY91").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY92").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY93").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY94").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY95").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY96").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY97").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY98").Select
   'On Error Resume Next
   Selection.Name.Delete
   Range("BY99").Select
   'On Error Resume Next
   Selection.Name.Delete
   On Error Resume Next
   Range("BY100").Select
   Application.DisplayAlerts = False
   End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,121
What about:
Code:
Sub deletenames()
    Range("BY3:BY100").ClearContents
End Sub
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,690
Office Version
  1. 2010
Platform
  1. Windows
Give this code a try...
Code:
Sub DeleteNames()
  Dim Cell As Range
  On Error Resume Next
  For Each Cell In Range("BY3:BY100")
    Cell.Name.Delete
  Next
  On Error GoTo 0
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,012
Office Version
  1. 365
Platform
  1. Windows
Try
Code:
Sub delnames()
   Dim i As Long
   For i = 3 To 100
      On Error Resume Next
      Range("BY" & i).Name.Delete
      On Error GoTo 0
   Next i
End Sub
I'm obviously too slow typing today :(
 
Last edited:

m_in_spain

Board Regular
Joined
Sep 28, 2018
Messages
55

ADVERTISEMENT

What about:
Code:
Sub deletenames()
    Range("BY3:BY100").ClearContents
End Sub

this cleared the actual content of the cell rather than the NAME, but thanks for answering, the next answer solved my problem this time, I already feel my next question coming up..stand by sir!!
 

m_in_spain

Board Regular
Joined
Sep 28, 2018
Messages
55
Give this code a try...
Code:
Sub DeleteNames()
  Dim Cell As Range
  On Error Resume Next
  For Each Cell In Range("BY3:BY100")
    Cell.Name.Delete
  Next
  On Error GoTo 0
End Sub

Thank you sir, works like a dream. One day i will get my hear around all this...stand by for the next question!!
 

m_in_spain

Board Regular
Joined
Sep 28, 2018
Messages
55
Try
Code:
Sub delnames()
   Dim i As Long
   For i = 3 To 100
      On Error Resume Next
      Range("BY" & i).Name.Delete
      On Error GoTo 0
   Next i
End Sub
I'm obviously too slow typing today :(

Slow or not, thank you for replying, different ideas show me how flexible this vba is, i am sloooooooly learning...stand by for my next question!!
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,690
Office Version
  1. 2010
Platform
  1. Windows
Try
Code:
Sub delnames()
   Dim i As Long
   For i = 3 To 100
      On Error Resume Next
      Range("BY" & i).Name.Delete
      On Error GoTo 0
   Next i
End Sub
I'm obviously too slow typing today :(
Your code is structured differently than mine so, not really. I would point out, though, that On Error Resume Next remains active until the code ends (which I do not like to rely on) or is physically cancelled, so you can set it before the loop starts and cancel it after the loop ends instead of repeatedly setting and cancelling inside the loop.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,737
Messages
5,597,826
Members
414,180
Latest member
Sir Khaya

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
Top