Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 231467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:56:07+00:00 2026-05-11T19:56:07+00:00

I am having a bit of trouble with errors occurring in a loop in

  • 0

I am having a bit of trouble with errors occurring in a loop in VBA. First, here is the code I am using

dl = 20
For dnme = 1 To 3
Select Case dnme
Case 1
drnme = kt + " 90"
nme = "door90"
drnme1 = nme
Case 2
drnme = kt + " dec"
nme = "door70" 'decorative glazed'
Case 3
drnme = kt + " gl"
nme = "door80" 'plain glazed'
End Select

On Error GoTo ErrorHandler
Set sh = Worksheets("kitchen doors").Shapes(drnme) 'This line here is where the problem is'
sh.Copy
ActiveSheet.Paste
    Selection.ShapeRange.Name = nme
    Selection.ShapeRange.Top = 50
    Selection.ShapeRange.Left = dl
    Selection.ShapeRange.Width = 150
    Selection.ShapeRange.Height = 220
25
dl = dl + 160
Next dnme

 Exit Sub
ErrorHandler:

GoTo 25

The problem is that when it tries to access the shape, the shape doesn’t always exist. The first time through the loop, this is fine. It goes to the ErrorHandler and everything works good. The second time it goes through and can’t find the shape, it comes up with the “End/Debug” error box. I can’t work out why it doesn’t just go straight to the ErrorHandler. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:56:07+00:00Added an answer on May 11, 2026 at 7:56 pm

    First of all you have a for loop with only 3 iterations, and you have a switch case for three!!. why can’t you move your common code to a new function and call it thrice?

    And more over each error has a unique number (incase of VBA errors like Subscript out of range etc, or a description if its a generic number like 1004, and other office errors). You need to check the error number, then decide how to proceed, if to skip the part or work around.

    Please go through this code..I have moved your comon code to a new function, and in that function we will be resizing the shape.
    If the shape is missing then we will just return false, and move to next shape.

    'i am assuming you have defined drnme, nme as strings and d1 as integer
    'if not please do so
    Dim drnme As String, nme As String, d1 As Integer
    
    dl = 20
    
    drnme = kt + " 90"
    nme = "door90"
    If ResizeShape(drnme, nme, d1) Then
        d1 = d1 + 160
    End If
    'Just call 
    'ResizeShape(drnme, nme, d1)
    'd1 = d1 + 160
    'If you don't care if the shape exists or not to increase d1
    'in that case whether the function returns true or false d1 will be increased
    
    drnme = kt + " dec"
    nme = "door70" 'decorative glazed'
    If ResizeShape(drnme, nme, d1) Then
        d1 = d1 + 160
    End If
    
    drnme = kt + " gl"
    nme = "door80" 'plain glazed'
    If ResizeShape(drnme, nme, d1) Then
        d1 = d1 + 160
    End If
    
    ActiveSheet.Shapes("Txtdoors").Select
    Selection.Characters.Text = kt & ":   " & kttxt
    Worksheets("kts close").Protect Password:="UPS"
    
    
    End Sub
    
    'resizes the shape passed in.
    'if the shape does not exists then returns false.
    'in that case you can skip incrementing d1 by 160
    
    Public Function ResizeShape(drnme As String, nme As String, d1 As Integer) As Integer
    On Error GoTo ErrorHandler
    Dim sh As Shape
    Set sh = Worksheets("kitchen doors").Shapes(drnme)
    sh.Copy
    ActiveSheet.Paste
    Selection.ShapeRange.Name = nme
    Selection.ShapeRange.Top = 50
    Selection.ShapeRange.Left = dl
    Selection.ShapeRange.Width = 150
    Selection.ShapeRange.Height = 220
    Exit Function
    ErrorHandler:
    'Err -2147024809 will be raised if the shape does not exists
    'then just return false
    'for the other errors you can examine the number and go back to next line or the same line
    'by using Resume Next or Resume
    'not GOTO!!
    If Err.Number = -2147024809 Or Err.Description = "The item with the specified name wasn't found." Then
        ResizeShape = False
        Exit Function
    End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 218k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The UITableViewController class has a property tableView to access the… May 12, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer At least in Delphi 2009 (can't test in version 2010… May 12, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer ORDER BY CASE WHEN (ClosingDate < GetDate()) THEN 1 ELSE… May 12, 2026 at 11:39 pm

Related Questions

One of the pages in our web application polls the server approximately every 10
I am having a bit of trouble using strtok with strcmp. //Handles the header
I'm trying to add FileInfo details to my xml file by recursively scanning and
I have been asked to modify an Excel sheet with some arcaic programming. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.