VBA Excel 形状

2024-01-01

我使用了一个小子程序将图片插入到我的工作表中

ActiveSheet.Pictures.Insert(URL).Select

这适用于 Excel 2003 (Windows),但不再适用于 Excel 2011 (Mac)。

因此我修改了我的子程序 (就像提议的http://www.launchexcel.com/google-maps-excel-demo/ http://www.launchexcel.com/google-maps-excel-demo/), 但子程序停止于

theShape.Fill.UserPicture URL 

与错误消息

“-2147024894 (80070002) 对象填充格式的用户图片方法”

矩形是绿色的!

Sub Q1()

Dim wks As Worksheet
Dim URL As String
Dim i As Long
Dim lastRow As Long
Dim theShape As Shape
Dim pasteCell As Range

' Used Worksheet
Set wks = Worksheets("Blatt1")

' Delete already existing shapes
For Each theShape In wks.Shapes
        theShape.Delete
Next theShape

' Check all existing rows in Column K
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
For i = 2 To lastRow

' the URLs are already computed and stored in column K
URL = wks.Range("K" & i).Value

' try to put the images in column L
Set pasteCell = wks.Range("L" & i)
pasteCell.Select

' Create a Shape for putting the Image into
' ActiveSheet.Pictures.Insert(URL).Select is deprecated and does not work any more!!!
Set theShape = wks.Shapes.AddShape(msoShapeRectangle, pasteCell.Left, pasteCell.Top, 200, 200)

' fill the shape with the image after greening
theShape.Fill.BackColor.RGB = RGB(0, 255, 0)
theShape.Fill.UserPicture URL

Next i

End Sub

有什么建议或提示吗?也许我像蝙蝠一样瞎了......


您是否尝试过将形状设置为 URL 的语法:

Sub Picadder()

Dim Pic As Shape

Set Pic = ActiveSheet.Shapes.AddPicture("http://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png", msoFalse, msoTrue, 0, 0, 100, 100)

End Sub

当这段代码适应您的工作时,可能看起来像这样:

Sub Q1()

Dim wks As Worksheet
Dim URL As String
Dim i As Long
Dim lastRow As Long
Dim theShape As Shape
Dim pasteCell As Range

' Used Worksheet
Set wks = Worksheets("Blatt1")

' Delete already existing shapes
For Each theShape In wks.Shapes
        theShape.Delete
Next theShape

' Check all existing rows in Column K
lastRow = Cells(Rows.Count, "K").End(xlUp).Row
For i = 2 To lastRow

' the URLs are already computed and stored in column K
URL = wks.Range("K" & i).Value

' try to put the images in column L
Set pasteCell = wks.Range("L" & i)
pasteCell.Select

' Create a Shape for putting the Image into
' ActiveSheet.Pictures.Insert(URL).Select is deprecated and does not work any more!!!
Set theShape = wks.Shapes.AddPicture(URL, pasteCell.Left, pasteCell.Top, 200, 200)

' Set shape image backcolor.
theShape.Fill.BackColor.RGB = RGB(0, 255, 0)

Next i

End Sub

您的网址需要正确格式化 - 我必须在初始片段的网址上使用引号才能使其有效运行,但这可能是一个解决方案。

对于 Mac-Excel 2011,有一个讨论的解决方法迈克尔·麦克劳克林 http://blog.mclaughlinsoftware.com/2011/03/08/add-image-comment-vba/在他的博客上。显然,在 Mac-Excel 2011 中将图像与单元格关联起来并不容易(如果有的话)。此外,研究表明,将图像插入 Excel 工作簿的问题已被多次询问。迄今为止的研究似乎还没有通过图像方法轻易解决这个问题。因此,变通办法可能是最好的解决方案。

该代码片段是从 Michael 的博客中非常密切地改编和移植的,如下所示:

Function InsertImageCommentAsWorkAround(title As String, cellAddress As Range)

    ' Define variables used in the comment.
    Dim ImageCommentContainer As comment

  ' Clear any existing comments before adding new ones.
  Application.ActiveCell.ClearComments

  ' Define the comment as a local variable and assign the file name from the _
  ' _ cellAddress as an input parameter to the comment of a cell at its cellAddress.

  ' Add a comment.
  Set ImageCommentContainer = Application.ActiveCell.AddComment

  ' With the comment, set parameters.
  With ImageCommentContainer
    .Text Text:=""

        'With the shape overlaying the comment, set parameters.
        With .Shape
          .Fill.UserPicture (cellAddress.Value)
          .ScaleHeight 3#, msoFalse, msoScaleFormTopLeft
          .ScaleWidth 2.4, msoFalse, msoScaleFromTopLeft
        End With

  End With

  InsertImageCommentAsWorkAround = title

End Function

我建议将注释集调整到您的循环中,并使用它来将图像设置到位,使用循环中的形状格式设置由改编代码生成的注释形状的格式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VBA Excel 形状 的相关文章

  • Range.End() 困惑

    我有一个关于 VBA 中 Range End 属性的一般性问题 我已经阅读了有关该房产的信息here http msdn microsoft com en us library bb221181 aspx 但我还是很困惑 例子 With w
  • Swift 5 MacOS 图像调整大小内存问题

    我是使用 Swift 进行 Mac OS 应用程序开发的新手 但我尝试制作简单的 ImageResizer 应用程序 我必须调整 50k 图像的大小 10个小时后 内存已增加到近120GB 我以为 Swift 也有垃圾收集器 为什么它可以增
  • Mac OS X 中 Bash 脚本中的 SFTP 命令

    我需要使用 SFTP 和 SSH 从 Mac 主机将文本文件传输到远程 PC freeSSH 这两个连接在本地网络中 那么有没有办法从 Bash 脚本内部运行 SFTP 命令 使用提供的用户名和密码 我已经尝试过一些脚本expect 但我没
  • 非相邻单元格作为数组函数的输入(MIN 和 ISBLANK)

    提出了这个问题 由于具体问题的答案是一个拼写错误 因此被删除 https stackoverflow com questions 59289065 excel non adjecent cells as input to array fun
  • 将所有工作簿工作表复制到新工作簿 VBA

    我正在使用此代码将工作簿中的每张工作表复制到新工作簿中 它工作正常 但它颠倒了工作表的顺序 是否有办法阻止它这样做 Sub copy copies all the sheets of the open workbook to a new o
  • 如何从 MacOS X Dock 启动脚本?

    我知道我可以将应用程序固定到扩展坞并从那里启动它们 但是 有没有办法将不是 MacOS 意义上的 应用程序 的程序 例如 bash 脚本 固定到扩展坞上 您可以将任何文件拖到 Dock 的右侧栏 垃圾箱和文件夹所在的位置 然后单击它来执行它
  • 强力查询历年产品利润对比

    我有一个数据集 其中包含公司 产品 利润和年份 公司每年都会销售少量产品并获得利润 公司没有必要在明年销售相同的产品 他们可能会省略以前的产品并添加新的少量产品 我只想对两年的产品进行逐个比较 如下所示 我的数据集是 Company Pro
  • 从受密码保护的 Excel 文件到 pandas DataFrame

    我可以使用以下命令打开受密码保护的 Excel 文件 import sys import win32com client xlApp win32com client Dispatch Excel Application print Exce
  • 你将如何开始自动化我的工作? - 第2部分

    后续这个问题 https stackoverflow com questions 2796128 how would you start automating my job 在经历了第一波进货 9 小时的复制 粘贴 后 我现在相信我已经满足
  • 如何设置Python的USER_SITE;我需要吗?

    我在 OS X 10 10 只需使用 pip 维护 上安装了 Python 我的站点包位于 Library Python 2 7 site packages 苹果的封装在 System Library Frameworks Python f
  • 为什么我无法在 Mac 12.0.1 (Monterey) 上使用 pip 安装 OpenCV? [复制]

    这个问题在这里已经有答案了 当我尝试使用 python pip 安装 OpenCV 时 它显示了以下内容 Remainder of file ignored Requirement already satisfied pip in Libr
  • 运行时错误“1004”:无法获取 WorksheetFunction 类的 Combin 属性

    我在 Excel 2013 的工作簿中有 VBA 函数 可以根据泊松分布计算 p 值 当 的时候events下面代码中的变量超过 1029 我得到运行时错误 1004 无法获取 WorksheetFunction 类的 Combin 属性
  • 如何将 HTML 表格导出为 .xlsx 文件

    我有一个关于导出的问题HTML表格 as an xlsx文件 我做了一些工作 现在我可以将其导出为xls 但我需要将其导出为xlsx 这是我的 jsFiddle https jsfiddle net 272406sv 1 https jsf
  • 使用 MemoryStream 创建 Open XML 电子表格时的 Excel 和“不可读内容”

    使用 Open XML SDK v2 0 创建 Excel 电子表格时 我们的 Excel 输出最初可以成功运行几个月 最近Excel 所有版本 开始抱怨 Excel在 zot xlsx 中发现不可读的内容 是否要恢复此工作簿的内容 我们正
  • Excel 宏与 Javascript

    我希望使用 Javascript 中的宏而不是默认的 VBA 来操作 Excel 电子表格 我可以使用以下 VBA 代码执行 javascript 代码 javascript to execute Dim b As String b fun
  • 基于多个动态过滤条件过滤Excel范围

    我想过滤数据集 考虑几个可以根据可能值列表动态更改的过滤条件 我有过滤条件team其具有以下值 Team A Team B ALL 其中 ALL 是代表所有团队的通配符 对于第二个标准release 如果我想在过滤器中包含多个版本 则值之间
  • 如何生成Mac应用程序的安装包?

    如何为 OS X 二进制文件以及一些配置和脚本文件创建单个安装程序包 最终文件夹应如下所示 任何帮助 将不胜感激 谢谢 如果您希望将各种东西放置在不同的位置 安装程序在这里 文档在那里 支持文件在这里等等 它们也非常适合提供安装体验的可配置
  • MS Access 中的舍入

    VBA Access 中舍入的最佳方法是什么 我目前的方法是利用Excel方法 Excel WorksheetFunction Round 但我正在寻找一种不依赖Excel的方法 请注意 VBA Round 函数使用 Banker 舍入 将
  • java实现excel价格、收益率函数[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 为什么我的 PyGame 应用程序根本不运行?

    我有一个简单的 Pygame 程序 usr bin env python import pygame from pygame locals import pygame init win pygame display set mode 400

随机推荐