Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel AddPicture需要为文件路径引用单元格并调整图像大小_Excel_Vba - Fatal编程技术网

Excel AddPicture需要为文件路径引用单元格并调整图像大小

Excel AddPicture需要为文件路径引用单元格并调整图像大小,excel,vba,Excel,Vba,我有一个addpicture VBA,它使用固定的文件路径运行文件,但我需要它引用由特定单元格中的公式生成的文件路径。还需要能够调整图像大小,以适应单元格列宽,但保持纵横比。我可以用PictureInsert功能完成所有这些,但是当文档被其他方使用时,图像不可见 这是我的addpicture代码: Sub URLAddPicture() Set pic = ActiveSheet.Shapes.AddPicture("\\frb-fs01\DF\SHOEPICS\1. SHOE PHOT

我有一个addpicture VBA,它使用固定的文件路径运行文件,但我需要它引用由特定单元格中的公式生成的文件路径。还需要能够调整图像大小,以适应单元格列宽,但保持纵横比。我可以用PictureInsert功能完成所有这些,但是当文档被其他方使用时,图像不可见

这是我的addpicture代码:

Sub URLAddPicture()
    Set pic = ActiveSheet.Shapes.AddPicture("\\frb-fs01\DF\SHOEPICS\1. SHOE PHOTOS\spring summer 2020\BULK SAMPLES\DISCOVERY\AADLIA-SUBLACKEURO LEATHER.JPG", _
    linktofile:=msoFalse, savewithdocument:=msoCTrue, Left:=0, Top:=0, Width:=-1, Height:=-1)
End Sub
和图片插入代码:

Sub URLPictureInsert()
    Dim Pshp As Shape
    Dim xRg As Range
    Dim xCol As Long

    On Error Resume Next

    Application.ScreenUpdating = False

    Set rng = ActiveSheet.Range("A113")
    For Each cell In rng
        filenam = cell
        ActiveSheet.Pictures.Insert(filenam).Select

        Set Pshp = Selection.ShapeRange.Item(1)
        If Pshp Is Nothing Then GoTo lab

        xCol = cell.Column
        Set xRg = Cells(cell.Row, xCol)
        With Selection
            .ShapeRange.LockAspectRatio = msoTrue
            If (.Height \ .Width) <= (rng.Height \ rng.Width) Then
                .Width = rng.Width - 1
                .Left = rng.Left + 1
                .Top = rng.Top + ((rng.Height - Selection.Height) / 2)
            Else
                .Top = rng.Top + 1
                .Height = rng.Height - 1
                .Left = rng.Left + ((rng.Width - Selection.Width) / 2)
            End If

            .Placement = xlMoveAndSize
            .PrintObject = True
        End With
lab:
        Set Pshp = Nothing
        Range("A113").Select
    Next

    Application.ScreenUpdating = True
End Sub
Sub-URLPictureInsert()
形状模糊的Pshp
Dim xRg As范围
Dim xCol尽可能长
出错时继续下一步
Application.ScreenUpdating=False
设置rng=ActiveSheet.Range(“A113”)
对于rng中的每个单元
filenam=单元
ActiveSheet.Pictures.Insert(filenam)。选择
设置Pshp=Selection.shaperage.Item(1)
如果Pshp算不上什么,那就转到实验室
xCol=cell.Column
设置xRg=单元格(cell.Row,xCol)
有选择
.shaperage.LockAspectRatio=msoTrue

如果(.Height\.Width)如果图片位于硬盘(磁盘)的正确位置,并且
rng
正确,则此代码应该可以工作。 另外,如果
rng
是单个单元格,则无需循环,但我保留了它,以便以后在您将其变大时使用

Option Explicit

Sub URLPictureInsert()
Dim Pshp As Shape
Dim Cell As Range
Dim Rng As Range
Dim Filenam$

Application.ScreenUpdating = False

Set Rng = ActiveSheet.Range("A113")

For Each Cell In Rng

    Filenam = Cell.Value2

    On Error Resume Next 'in case filename doesn't exist
    Set Pshp = ActiveSheet.Pictures.Insert(Filenam).ShapeRange(1)
    On Error GoTo 0

    If Not Pshp Is Nothing Then

          With Pshp

              .LockAspectRatio = msoTrue

              If (.Height \ .Width) <= (Rng.Height \ Rng.Width) Then
                  .Width = Rng.Width - 1
                  .Left = Rng.Left + 1
                  .Top = Rng.Top + ((Rng.Height - .Height) / 2)
              Else
                  .Top = Rng.Top + 1
                  .Height = Rng.Height - 1
                  .Left = Rng.Left + ((Rng.Width - .Width) / 2)
              End If

              .Placement = xlMoveAndSize

        End With 'Pshp

    End If 'not Pshp is nothing

    Set Pshp = Nothing

Next Cell

Application.ScreenUpdating = True
End Sub
选项显式
子URLPictureInsert()
形状模糊的Pshp
暗淡单元格作为范围
变暗Rng As范围
迪姆·菲尔南$
Application.ScreenUpdating=False
设置Rng=ActiveSheet.Range(“A113”)
对于Rng中的每个单元
Filenam=Cell.Value2
如果文件名不存在,则在出现错误时“继续下一步”
设置Pshp=ActiveSheet.Pictures.Insert(Filenam.shaperage)(1)
错误转到0
如果不是Pshp,那么什么都不是
使用Pshp
.LockAspectRatio=msoTrue

如果(.Height\.Width)首先代码无法工作,因为
如果Pshp为空,则
没有
结束If
。此外,您还必须在错误恢复下一步时删除
,因为这一行隐藏所有错误消息,直到
结束子项
,因此,如果您没有看到错误,则无法修复它们,如果您没有修复它们,则代码显然无法正常工作。解决这个问题,然后回答你的问题,并告诉你得到了哪个错误和在哪里。你可能会从阅读中受益。清除所有
。使用此链接的技术选择
选择
语句。然后更新问题中的代码。•此外,我建议始终激活
Option Explicit
:在VBA编辑器中,转到工具›选项›并正确声明所有变量ᴇʜ的建议,如何避免@DaveExcel Meeeh,是的,我看起来不太好,我的坏。谢谢你的指点。但这实际上是一个经典的例子,为什么使用
GoTo
或单行
If
语句是如此糟糕的做法和糟糕的编码风格:因为它不会花很长时间导致错误我的建议是将
GoTo
替换为多行
If Not
satement
If Not Pshp Nothing,然后在
lab:
End If
结尾的
?其他各方怎么看不到这幅图?是因为其他计算机上的A113中不存在文件名吗?