我已经复制并粘贴了一些宏来生成一列随机图片,但是我不太了解它们来调试它们。我在Range(MergedAreas(i))上遇到运行时错误.PasteSSial行,但只有部分时间。
我正在创建一个名为Codenames:Pictures的游戏的Excel版本。如果你想尝试一下,这是file!
Sub DisplayRandomPics()
Dim MergedAreas As Variant
Dim MyPics() As String
Dim PicsLoc As String
Dim Temp1 As String
Dim Temp2 As String
Dim Pic As Picture
Dim Cnt As Long
Dim i As Long
Dim j As Long
PicsLoc = "A7:A284" 'Change the location of the pictures, accordingly
MergedAreas = Array("C2:C2", "C3:C3", "C4:C4", "C5:C5", "D2:D2", "D3:D3", "D4:D4", "D5:D5", "E2:E2", "E3:E3", "E4:E4", "E5:E5", "F2:F2", "F3:F3", "F4:F4", "F5:F5", "G2:G2", "G3:G3", "G4:G4", "G5:G5")
Cnt = 0
Randomize
For Each Pic In ActiveSheet.Pictures
If Union(Pic.TopLeftCell, Range(PicsLoc)).Address = Range(PicsLoc).Address Then
ReDim Preserve MyPics(0 To 1, 0 To Cnt)
MyPics(0, Cnt) = Pic.Name
MyPics(1, Cnt) = Rnd
Cnt = Cnt + 1
End If
Next Pic
If Cnt < 3 Then
MsgBox "The range " & PicsLoc & " must contain at least 3 pictures...", vbExclamation
Exit Sub
End If
Application.ScreenUpdating = False
Call DeleteRandomPics
For i = 0 To UBound(MyPics, 2) - 1
For j = i + 1 To UBound(MyPics, 2)
If MyPics(1, i) > MyPics(1, j) Then
Temp1 = MyPics(0, j)
Temp2 = MyPics(1, j)
MyPics(0, j) = MyPics(0, i)
MyPics(1, j) = MyPics(1, i)
MyPics(0, i) = Temp1
MyPics(1, i) = Temp2
End If
Next j
Next i
For i = 0 To 19
ActiveSheet.Pictures(MyPics(0, i)).Copy
Range(MergedAreas(i)).PasteSpecial
Next i
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Sub DeleteRandomPics()
Dim Pic As Picture
For Each Pic In ActiveSheet.Pictures
If Union(Pic.TopLeftCell, Range("A173:O173")).Address = Range("A173:O173").Address Then
Pic.Delete
End If
Next Pic
End Sub
你能尝试重写这样的代码:
For i = 0 To 19
ActiveSheet.Pictures(MyPics(0, i+1)).Copy
Range(MergedAreas(i)).PasteSpecial
Next i
看看它是否每次都有效?
从你在这里分配MyPics
数组的方式:
For j = i + 1 To UBound(MyPics, 2)
似乎j
不能是0
。