|
-
Aug 28th, 2012, 09:39 PM
#1
Thread Starter
New Member
I am trying to create 50-100 instances of this code
The basic/core code is as follows:
{
On Error Resume Next
Set Shape = ActiveSheet.Shapes("Picture 1")
On Error GoTo 0
If Shape Is Nothing Then
Exit Sub
End If
If Range("Q4").Value < 0.025 Then
ActiveSheet.Shapes("Picture 1").Select
Selection.Cut
End If
}
I am trying to create 50-100 instances of this code where each instance changes a bit. For example, the 2nd code instance must be like this:
{
On Error Resume Next
Set Shape = ActiveSheet.Shapes("Picture 2")
On Error GoTo 0
If Shape Is Nothing Then
Exit Sub
End If
If Range("Q5").Value < 0.025 Then
ActiveSheet.Shapes("Picture 2").Select
Selection.Cut
End If
}
... and the 3rd:
{
On Error Resume Next
Set Shape = ActiveSheet.Shapes("Picture 3")
On Error GoTo 0
If Shape Is Nothing Then
Exit Sub
End If
If Range("Q6").Value < 0.025 Then
ActiveSheet.Shapes("Picture 3").Select
Selection.Cut
End If
}
As you see:
Q4 corresponds to Picture 1
Q5 corresponds to Picture 2
Q6 corresponds to Picture 3
...
Q50 corresponds to Picture 47
...
Q103 corresponds to Picture 100
I am at a loss for writing a more elegant code rather than repeating this for 50-100 times! Could you please show me how to do it?
Thanks!
-
Aug 28th, 2012, 11:07 PM
#2
Re: I am trying to create 50-100 instances of this code
Welcome to the Forums 
You need to create a new Sub and pass whatever is changing as parameter (it seems you will need to use 2 String parameters), then you can call your Sub as many times as you want. You could also create a loop to avoid calling the sub line by line multiple times, for each iteration you would create 2 Strings variables in code then sending them as parameters and it looks like simply concatenating the corresponding number to each variable (creating the corresponding control name) for each iteration should be enough for you.
-
Aug 29th, 2012, 05:15 AM
#3
Re: I am trying to create 50-100 instances of this code
Maybe you could loop it like this
Code:
Dim i As Integer
Dim strRange As String
Dim strPicture As String
For i = 1 To 100
strPicture = "Picture " & i
Set Shape = ActiveSheet.Shapes(strPicture)
If Shape Is Nothing Then
Exit Sub
End If
strRange = "Q" & (i + 3)
If Range(strRange).Value < 0.025 Then
ActiveSheet.Shapes(strPicture).Select
Selection.Cut
End If
Next i
-
Aug 29th, 2012, 07:25 PM
#4
Thread Starter
New Member
-
Aug 30th, 2012, 12:52 PM
#5
Frenzied Member
Re: I am trying to create 50-100 instances of this code
for 50 instance try the following way .
Code:
Dim i As Integer
Dim strRange As String
Dim strPicture As String
For i = 1 To 50
strPicture = "Picture " & i
Set Shape = ActiveSheet.Shapes(strPicture)
If Shape Is Nothing Then
Exit Sub
End If
strRange = "Q" & (i + 3)
If Range(strRange).Value < 0.025 Then
ActiveSheet.Shapes(strPicture).Select
Selection.Cut
End If
Next i
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|