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.
Bookmarks