有人能向我解释一下如何将网页链接(URL)转换为图像吗?
示例图片(URL 为http://cache.lego.com/media/bricks/5/1/4667591.jpg
)
我想要做的是制作一个我下载的零件清单来显示图像而不是上面的网络链接。
我在 J2 到 J1903 中所拥有的是:
http://cache.lego.com/media/bricks/5/1/4667591.jpg
http://cache.lego.com/media/bricks/5/1/4667521.jpg
...
我想要做的是让 Excel 将所有这些(共 10903 个)转换为图片(单元格大小为 81x81)。
有人可以一步一步解释我该如何做到这一点吗?
答案1
如果你有一组列中的链接J喜欢:
然后运行这个简短的 VBA 宏:
Sub InstallPictures()
Dim i As Long, v As String
For i = 2 To 1903
v = Cells(i, "J").Value
If v = "" Then Exit Sub
With ActiveSheet.Pictures
.Insert (v)
End With
Next i
End Sub
每个链接都将被打开,并且相关的图片将被放置在工作表上。
图片必须具有适当的尺寸和位置。
编辑#1:
宏的安装和使用非常容易:
- ALT-F11 打开 VBE 窗口
- ALT-I ALT-M 打开新模块
- 粘贴内容并关闭 VBE 窗口
如果您保存工作簿,宏将随之保存。如果您使用的是 2003 以后的 Excel 版本,则必须将文件保存为 .xlsm 而不是 .xlsx
要删除宏:
- 调出如上所示的 VBE 窗口
- 清除代码
- 关闭 VBE 窗口
要使用 Excel 中的宏:
- ALT-F8
- 选择宏
- 触摸运行
要了解有关宏的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
必须启用宏才能使其工作!
编辑#2:
为了避免因检索错误而停止,请使用此版本:
Sub InstallPictures()
Dim i As Long, v As String
On Error Resume Next
For i = 2 To 1903
v = Cells(i, "J").Value
If v = "" Then Exit Sub
With ActiveSheet.Pictures
.Insert (v)
End With
Next i
On Error GoTo 0
End Sub
答案2
这是我的修改:
- 用图片链接替换单元格(不是新列)
- 使图片与文档一起保存(而不是可能脆弱的链接)
- 使图像稍微小一些,以便它们有机会与细胞进行分类。
下面的代码:
Option Explicit
Dim rng As Range
Dim cell As Range
Dim Filename As String
Sub URLPictureInsert()
Dim theShape As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
' Set to the range of cells you want to change to pictures
Set rng = ActiveSheet.Range("A2:A600")
For Each cell In rng
Filename = cell
' Use Shapes instead so that we can force it to save with the document
Set theShape = ActiveSheet.Shapes.AddPicture( _
Filename:=Filename, linktofile:=msoFalse, _
savewithdocument:=msoCTrue, _
Left:=cell.Left, Top:=cell.Top, Width:=60, Height:=60)
If theShape Is Nothing Then GoTo isnill
With theShape
.LockAspectRatio = msoTrue
' Shape position and sizes stuck to cell shape
.Top = cell.Top + 1
.Left = cell.Left + 1
.Height = cell.Height - 2
.Width = cell.Width - 2
' Move with the cell (and size, though that is likely buggy)
.Placement = xlMoveAndSize
End With
' Get rid of the
cell.ClearContents
isnill:
Set theShape = Nothing
Range("A2").Select
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub
答案3
这个方法效果更好,因为图像最终位于其所属单元格的旁边。
Option Explicit
Dim rng As Range
Dim cell As Range
Dim Filename As String
Sub URLPictureInsert()
Dim theShape As Shape
Dim xRg As Range
Dim xCol As Long
On Error Resume Next
Application.ScreenUpdating = False
Set rng = ActiveSheet.Range("C1:C3000") ' <---- ADJUST THIS
For Each cell In rng
Filename = cell
If InStr(UCase(Filename), "JPG") > 0 Then '<--- ONLY USES JPG'S
ActiveSheet.Pictures.Insert(Filename).Select
Set theShape = Selection.ShapeRange.Item(1)
If theShape Is Nothing Then GoTo isnill
xCol = cell.Column + 1
Set xRg = Cells(cell.Row, xCol)
With theShape
.LockAspectRatio = msoFalse
.Width = 100
.Height = 100
.Top = xRg.Top + (xRg.Height - .Height) / 2
.Left = xRg.Left + (xRg.Width - .Width) / 2
End With
isnill:
Set theShape = Nothing
Range("A2").Select
End If
Next
Application.ScreenUpdating = True
Debug.Print "Done " & Now
End Sub