【问题标题】:How to make an Excel macro run when the file is updated?如何在文件更新时运行 Excel 宏?
【发布时间】:2019-06-26 13:48:32
【问题描述】:

我有一个 PowerApp,它可以更新 OneDrive 中托管的 Excel 文件中的单元格。 Excel 文件包含一个在 PowerApp 更改 Excel 文件时应该运行的宏。但是,它不会那样做。如果我手动更新单元格,宏就可以正常工作。当文件被 PowerApps 更新时,它只是没有被激活。

当 PowerApp 更改文件时,我可以使用不同的功能吗?

如果不可能,我可以使用 Flow 来激活宏吗?

这是适用于手动更改的当前脚本,但不适用于自动 PowerApps 更改。

Private Sub Worksheet_Change(ByVal Target As Range)
    Call InsertImageTest
End Sub

这是我想使用上面的代码触发的宏。

Sub InsertImageTest()

' This macro inserts an image from a set location to a set cell.

Dim ws As Worksheet
Dim imagePath As String
Dim cell As String
Dim posText As String
Dim imgLeft As Double
Dim imgTop As Double
Dim rngX As Range
Dim activeSheetName As String

' Customizable variables
imagePath = ActiveWorkbook.Path & Range("$B$2").Value
posText = "Signature"
activeSheetName = "Data" ' Set to "Data" by default, but will change to the Active sheets name, if the active sheet is not called "Data"

' For i = 1 To Sheets.Count
'    If CStr(Sheets(i).Name) Is CStr(activeSheetName) Then
'        Debug.Print "Code can be executed! Data tab was found"
'    End If
' Next i

cell = "$A$1"

Set ws = ActiveSheet
Set rngX = Worksheets(activeSheetName).Range("A1:Z1000").Find(posText, lookat:=xlPart)
If Not rngX Is Nothing Then
    cell = rngX.Address
    Debug.Print cell
    Debug.Print rngX.Address & " cheating"
    Worksheets(activeSheetName).Range(cell).Value = ""

    Debug.Print rngX.Address & " real"

    imgLeft = Range(cell).Left
    imgTop = Range(cell).Top

    ' Width & Height = -1 means keep original size
    ws.Shapes.AddPicture _
    Filename:=imagePath, _
    LinkToFile:=msoFalse, _
    SaveWithDocument:=msoTrue, _
    Left:=imgLeft, _
    Top:=imgTop, _
    Width:=-1, _
    Height:=-1
End If

' The code beaneath will resize the cell to fit the picture
For Each Picture In ActiveSheet.DrawingObjects
PictureTop = Picture.Top
PictureLeft = Picture.Left
PictureHeight = Picture.Height
PictureWidth = Picture.Width
For N = 2 To 256
If Columns(N).Left > PictureLeft Then
PictureColumn = N - 1
Exit For
End If
Next N
For N = 2 To 65536
If Rows(N).Top > PictureTop Then
PictureRow = N - 1
Exit For
End If
Next N
Rows(PictureRow).RowHeight = PictureHeight
Columns(PictureColumn).ColumnWidth = PictureWidth * (54.29 / 288)
Picture.Top = Cells(PictureRow, PictureColumn).Top
Picture.Left = Cells(PictureRow, PictureColumn).Left
Next Picture

End Sub

【问题讨论】:

  • 尝试Worksheet_Calculate 事件处理程序。
  • @Dean 不幸的是没有工作。我只是在更新一个字符串单元格值,所以不调用Calculate。
  • PowerApp 打开文件并修改它?
  • 不清楚 PowerApps 如何操作单元格中的数据。使用 PowerApps 操作工作簿时是否打开
  • @DavidZemens PowerApps 通过 API 编辑工作簿,因此在操作时不会打开。

标签: excel vba powerapps


【解决方案1】:

很遗憾,服务器通过 API 打开 Excel,而 Excel 不会以这种方式触发宏。似乎流有the same。我会考虑在 PowerApps 中实现宏功能逻辑。自定义应该触发宏的列的编辑形式,取决于宏应该做什么。如果宏尝试更改另一列的值,可能会解锁数据卡。

【讨论】:

  • 如何在PowerApps中实现宏功能逻辑?该应用程序当前正在编辑表单。这将更新 Excel 中包含所有文件位置的表。所有单元格都应解锁。宏的工作是使用表格中的文件位置将原始图像放置在单元格中。我已经添加了宏代码以进行澄清。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-09
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多