Excel r“”或“0”。 如果结束 接下来我 其他的 '字典为空,即未找到任何形状。 如果结束 以 端接头
如果您对Dictionary对象不太熟悉,您应该这样使用它: (注意“***”表示更改。)Excel r“”或“0”。 如果结束 接下来我 其他的 '字典为空,即未找到任何形状。 如果结束 以 端接头,excel,vba,cell,shapes,Excel,Vba,Cell,Shapes,如果您对Dictionary对象不太熟悉,您应该这样使用它: (注意“***”表示更改。) 子副本形状2() 作为对象的Dim dict*** Set dict=CreateObject(“Scripting.Dictionary”)'*** '允许大小写不敏感,例如'Picture=Picture'。 dict.CompareMode=vbTextCompare'*** '定义工作簿。 将wb设置为工作簿 将wb=ThisWorkbook设置为包含此代码的工作簿。 '定义源工作表。 Dim s
子副本形状2()
作为对象的Dim dict***
Set dict=CreateObject(“Scripting.Dictionary”)'***
'允许大小写不敏感,例如'Picture=Picture'。
dict.CompareMode=vbTextCompare'***
'定义工作簿。
将wb设置为工作簿
将wb=ThisWorkbook设置为包含此代码的工作簿。
'定义源工作表。
Dim src As工作表
设置src=wb.工作表(“图像”)
'将其形状名称写入字典。
将shp变暗为形状
对于src形状中的每个shp
dict(shp.Name)=空'***
下一个小水电
如果指令计数>0,则'***
'定义目标工作表。
将dst设置为工作表
设置dst=wb.工作表(“试验产品”)
'声明变量。
我想我会坚持多久
Dim NumStr作为字符串
将ShapeName设置为字符串
'在包含形状索引的单元格中循环并复制
'在源工作表的字典中找到的形状
'到目标工作表。
对于i=2到224
NumStr=CStr(dst.单元格(i,9).值)
如果NumStr“”和NumStr“0”,则
ShapeName=“Picture”&NumStr
如果dict.Exists(ShapeName),则'***
src.Shapes(ShapeName).Copy
dst.Paste Destination:=dst.Cells(i,10)
其他的
'具有此编号的形状(图片)不存在。
如果结束
其他的
“NumStr”是“”或“0”。
如果结束
接下来我
其他的
'字典为空,即未找到任何形状。
如果结束
端接头
在VBA中是&
。在VBA中是&
。这太棒了。我很快就会试试的。没想到这么详细的回答这么快。非常感谢你!我今天真的很绝望,花了很多时间寻找解决办法!万事如意。我只是不理解line.Item(shp.Name)=空..它将形状名称添加到字典中。字典包含一个值对,通常称为键
和值
:。项
是键
,。项(键)
是值
。该行将形状名称写入键
,将空
写入值
,因为我们不需要值
。了解这本词典。我添加了一个稍有改动的版本,使用起来更加友好。您应该在熟悉它的同时使用这种方法。谢谢!我对这个键有一个想法,但是添加新元素的方式让我很困惑,就像我期望的那样。。。此外,我认为VBA不支持字典,而且一开始也不这么认为,第二,我无法想象使用shape.name作为键,而不是元素。这太棒了。我很快就会试试的。没想到这么详细的回答这么快。非常感谢你!我今天真的很绝望,花了很多时间寻找解决办法!万事如意。我只是不理解line.Item(shp.Name)=空..它将形状名称添加到字典中。字典包含一个值对,通常称为键
和值
:。项
是键
,。项(键)
是值
。该行将形状名称写入键
,将空
写入值
,因为我们不需要值
。了解这本词典。我添加了一个稍有改动的版本,使用起来更加友好。您应该在熟悉它的同时使用这种方法。谢谢!我对这个键有一个想法,但是添加新元素的方式让我很困惑,就像我期望的那样。。。此外,我认为VBA不支持字典,而且一开始也不这么认为,第二,我无法想象使用shape.name作为键,而不是元素。
For i = 2 To 224
Sheets("Pilot products").Activate
a = CStr(ActiveSheet.Cells(i, 9).Value)
'a = CLng(a)
'MsgBox (VarType(a))
'Sheets("images").Activate
For Each sh In ActiveSheet.Shapes
If CStr(sh.Name) = CStr("Picture " + a) Then
sh("Picture " + a).Select
Selection.Copy
Sheets("Pilot products").Cells(i, 10).Select
ActiveSheet.Paste
Else: MsgBox CStr(a)
End If
Next sh
Next i
Option Explicit
Sub copyShapes()
With CreateObject("Scripting.Dictionary")
' Allow case-insensitivity e.g. 'Picture = PICturE'.
.CompareMode = vbTextCompare
' Define workbook.
Dim wb As Workbook
Set wb = ThisWorkbook ' The workbook containing this code.
' Define Source Worksheet.
Dim src As Worksheet
Set src = wb.Worksheets("Images")
' Write the names of its shapes to the Dictionary.
Dim shp As Shape
For Each shp In src.Shapes
.Item(shp.Name) = Empty
Next shp
' Check if any shapes were found.
If .Count > 0 Then
' Define Destination Worksheet.
Dim dst As Worksheet
Set dst = wb.Worksheets("Pilot products")
' Declare variables.
Dim i As Long
Dim ShapeIndex As String
Dim ShapeName As String
' Loop through cells containing the shape indexes and copy
' the found shapes in the Dictionary from Source Worksheet
' to Destination Worksheet.
For i = 2 To 224
ShapeIndex = CStr(dst.Cells(i, 9).Value)
If ShapeIndex <> "" And ShapeIndex <> "0" Then
ShapeName = "Picture " & ShapeIndex
If .Exists(ShapeName) Then
src.Shapes(ShapeName).Copy
dst.Paste Destination:=dst.Cells(i, 10)
Else
' A shape (picture) with this number doesn't exist.
End If
Else
' NumStr is either "" or "0".
End If
Next i
Else
' Dictionary is empty i.e. no shapes found.
End If
End With
End Sub
Sub copyShapes2()
Dim dict As Object '***
Set dict = CreateObject("Scripting.Dictionary") '***
' Allow case-insensitivity e.g. 'Picture = PICturE'.
dict.CompareMode = vbTextCompare '***
' Define workbook.
Dim wb As Workbook
Set wb = ThisWorkbook ' The workbook containing this code.
' Define Source Worksheet.
Dim src As Worksheet
Set src = wb.Worksheets("Images")
' Write its shape names to the Dictionary.
Dim shp As Shape
For Each shp In src.Shapes
dict(shp.Name) = Empty '***
Next shp
If dict.Count > 0 Then '***
' Define Destination Worksheet.
Dim dst As Worksheet
Set dst = wb.Worksheets("Pilot products")
' Declare variables.
Dim i As Long
Dim NumStr As String
Dim ShapeName As String
' Loop through cells containing the shape indexes and copy
' the found shapes in the Dictionary from Source Worksheet
' to Destination Worksheet.
For i = 2 To 224
NumStr = CStr(dst.Cells(i, 9).Value)
If NumStr <> "" And NumStr <> "0" Then
ShapeName = "Picture " & NumStr
If dict.Exists(ShapeName) Then '***
src.Shapes(ShapeName).Copy
dst.Paste Destination:=dst.Cells(i, 10)
Else
' A shape (picture) with this number doesn't exist.
End If
Else
' NumStr is either "" or "0".
End If
Next i
Else
' Dictionary is empty i.e. no shapes found.
End If
End Sub