+ Reply to Thread
Results 1 to 4 of 4

Simplification / correction of VBA code please

Hybrid View

ComboFab Simplification / correction... 06-24-2024, 07:27 AM
6StringJazzer Re: Simplification /... 06-24-2024, 08:00 AM
torachan Re: Simplification /... 06-24-2024, 08:05 AM
6StringJazzer Re: Simplification /... 06-24-2024, 09:33 AM
  1. #1
    Registered User
    Join Date
    06-24-2024
    Location
    Brussels
    MS-Off Ver
    Version 2405
    Posts
    1

    Simplification / correction of VBA code please

    Hello.

    I'm not a programming pro at all. I understand the basics... I needed to generate 2 QR-codes and have them placed in P37 and N50. In order, the code must :
    - delete the 2 QR-codes (images) present in P37 and N50 if they exist,
    - create and place the 2 new QR-codes.

    After a lot of searching and trial and error, I came up with this code:

    Sub qrcodedevis()
    
       Dim sd As Shape, rng As Range
       Set rng = Range("N50")
       For Each s In ActiveSheet.Shapes
          If Intersect(rng, s.TopLeftCell) Is Nothing Then
          Else
             s.Delete
          End If
       Next s
       Dim x As String
       x = Sheets("Devis HP").Range("U34")
       Range("U34").Select
       Selection.Copy
       Range("N50").Select
       ActiveSheet.Pictures.Insert(x).Select
       Dim t As Shape, rngg As Range
       Set rngg = Range("P37")
       For Each t In ActiveSheet.Shapes
          If Intersect(rngg, t.TopLeftCell) Is Nothing Then
          Else
             t.Delete
          End If
       Next t
       Dim y As String
       y = Sheets("Devis HP").Range("U36")
       Range("U36").Select
       Selection.Copy
       Range("P37").Select
       ActiveSheet.Pictures.Insert(y).Select
       Application.CutCopyMode = False
       Range("U9").Select
       
    End Sub
    Administrator's note: Welcome to the Forum! Please take the time to review our rules. There aren't many, and they are all important. Our guidelines recommend code tags. I have added them for you this time because you are a new member. I also added indentation for readability. --6StringJazzer

    Generally speaking, the code works, but there are 2 problems...
    1) From time to time, the code bugs and suggests debugging the following line:

    If Intersect(rng, s.TopLeftCell) Is Nothing Then
    If I enter the debug, change absolutely nothing and save the VBA code, when I run the macro again, it works. Strange because I didn't change anything.

    2) The code from which I drew inspiration had to delete several QR-codes from a range. The code therefore uses the "For each..." loop. However, not knowing how to code but understanding a little what was going on, I deleted the range concerned by the box I was interested in (P37) and then copied the code to rerun the operation for N50. It seems logical to me that the code isn't optimal since it uses 2 loops and each only deals with one element.


    In short, I'd like a kind soul to simplify my code and ensure that I no longer have any untimely bugs so that I can use it without any worries.


    I'm counting on your generosity.

    Thank you for your help.
    Last edited by 6StringJazzer; 06-24-2024 at 07:58 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,800

    Re: Simplification / correction of VBA code please

    The error could be related to your data. Please attach a file so we can actually run your code. There are other areas for improvement, but I'm not sure what is causing the error. We can take care of it all once we have the file.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: Simplification / correction of VBA code please

    initial comment - you are declaring 'sd' as shape however referencing 's' variable in the loop - I assume they are the same item ????
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,800

    Re: Simplification / correction of VBA code please

    Good point. In addition, s and t are undeclared. Although that does not necessarily cause an error, it is bug-prone. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Code Simplification
    By rakotonirinas in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-06-2021, 10:28 AM
  2. [SOLVED] Simplification of code for Chart Series control
    By mattydboom1 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-23-2020, 10:40 AM
  3. Autofilter loop + copy paste?
    By dunnobe in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2019, 04:20 AM
  4. [SOLVED] Code simplification (format and conditional formatting)
    By dunnobe in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-06-2018, 10:19 AM
  5. [SOLVED] Simplification for regex code to replace numbers in a comma delimited string
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2018, 06:30 AM
  6. [SOLVED] vba code correction
    By antonio32 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2017, 02:37 PM
  7. Simplification of macro code
    By PvanS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2009, 10:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1