更新现有的xlsx文件以反映xlsm文件中的更改。

huangapple go评论67阅读模式
英文:

Updating the existing xlsx file from changes in xlsm file

问题

I am using a macro-enabled workbook (xlsm) file to capture and record values. However, due to limitations with Power Automate for xlsm files, I have created a duplicate file in xlsx format. Currently, my approach involves repeatedly deleting and recreating the xlsx file with the same name. Unfortunately, this process generates a unique ID in Sharepoint each time the file is created, leading to referencing errors in my Power Automate flow.

Sub createXlsx()

    ' Path for the file to be placed at
    Dim path As String
    path = "C:\Users\user\Testing\"

    ' The name of the new file
    Dim newFile As String
    newFile = "Test.xlsx"

    Dim name As String
    name = path & newFile

    ' Check if the file already exists
    If Dir(path & newFile) <> "" Then
        Kill (path & newFile)
    End If

    Application.DisplayAlerts = False

    ' Create a new workbook
    Dim newWorkbook As Workbook
    Set newWorkbook = Workbooks.Add

    ' Copy the used range from the original worksheet to the new workbook
    Sheet1.UsedRange.Copy newWorkbook.Sheets(1).Range("A1")

    ' Save the new workbook as .xlsx format
    newWorkbook.SaveCopyAs path & newFile
    newWorkbook.Close SaveChanges:=False

    Application.DisplayAlerts = True

End Sub

I am seeking assistance to update the existing file instead of using the "kill" method to delete and create a new file. This approach would help me resolve the problem in Power Automate, as I am unsure how to obtain the newly created file each time and include it in the "List Row in a Table" action within my automated flow.

英文:

I am using a macro-enabled workbook (xlsm) file to capture and record values. However, due to limitations with Power Automate for xlsm files, I have created a duplicate file in xlsx format. Currently, my approach involves repeatedly deleting and recreating the xlsx file with the same name. Unfortunately, this process generates a unique ID in Sharepoint each time the file is created, leading to referencing errors in my Power Automate flow.

Sub createXlsx()

    &#39; Path for the file to be placed at
    Dim path As String
    path = &quot;C:\Users\user\Testing\&quot;
    
    &#39; The name of the new file
    Dim newFile As String
    newFile = &quot;Test.xlsx&quot;

    Dim name As String
    name = path &amp; newFile

    &#39; Check if the file already exists
    If Dir(path &amp; newFile) &lt;&gt; &quot;&quot; Then
        Kill (path &amp; newFile)
    End If

    Application.DisplayAlerts = False

    &#39; Create a new workbook
    Dim newWorkbook As Workbook
    Set newWorkbook = Workbooks.Add

    &#39; Copy the used range from the original worksheet to the new workbook
    Sheet1.UsedRange.Copy newWorkbook.Sheets(1).Range(&quot;A1&quot;)

    &#39; Save the new workbook as .xlsx format
    newWorkbook.SaveCopyAs path &amp; newFile
    newWorkbook.Close SaveChanges:=False

    Application.DisplayAlerts = True

End Sub

I am seeking assistance to update the existing file instead of using the "kill" method to delete and create a new file. This approach would help me resolve the problem in Power Automate, as I am unsure how to obtain the newly created file each time and include it in the "List Row in a Table" action within my automated flow.

答案1

得分: 1

使用Workbook.Open来打开文件 查看Microsoft Learn : Workbook.Open

你可以像这样将整个工作表复制到另一个工作簿中

ThisWorkbook.Sheets(1).Copy After:=Workbooks(NewWorkbook).Sheets(1)

英文:

Use Workbook.Open to open the file See Microsoft Learn : Workbook.Open

You can copy the entire sheet over to the other workbook like this

ThisWorkbook.Sheets(1).Copy After:=Workbooks(NewWorkbook).Sheets(1)

答案2

得分: 1

你可以更新工作簿而不是创建一个新的:

Sub updateXlsx()

    ' 文件路径
    Dim path As String
    path = "C:\Users\user\Testing\"
    
    ' 新文件的名称
    Dim targetFile As String
    targetFile = "Test.xlsx"

    Dim fullpathTargetFile As String
    fullpathTargetFile = path & targetFile

    ' 禁用警告对话框

    ' 打开工作簿
    Dim wbTarget As Workbook
    Set wbTarget = Workbooks.Open(fullpathTargetFile)

    Dim wsTarget As Worksheet
    Set wsTarget = wbTarget.Worksheets(1)

    ' 删除当前内容
    wsTarget.UsedRange.Delete xlShiftUp

    ' 将原工作表的使用范围复制到目标工作簿
    sheet1.UsedRange.Copy wsTarget.Range("A1")
    wbTarget.Close SaveChanges:=True

    ' 启用警告对话框

End Sub
英文:

You can update the workbook instead of creating a new one:

Sub updateXlsx()

    &#39; Path for the file to be placed at
    Dim path As String
    path = &quot;C:\Users\user\Testing\&quot;
    
    &#39; The name of the new file
    Dim targetFile As String
    targetFile = &quot;Test.xlsx&quot;

    Dim fullpathTargetFile As String
    fullpathTargetFile = path &amp; targetFile

    &#39;Application.DisplayAlerts = False

    &#39; open the workbook
    Dim wbTarget As Workbook
    Set wbTarget = Workbooks.Open(fullpathTargetFile)

    Dim wsTarget As Worksheet
    Set wsTarget = wbTarget.Worksheets(1)

    &#39;delete current content
    wsTarget.UsedRange.Delete xlShiftUp

    &#39; Copy the used range from the original worksheet to the target workbook    
    sheet1.UsedRange.Copy wsTarget.Range(&quot;A1&quot;)
    wbTarget.Close SaveChanges:=True

    &#39;Application.DisplayAlerts = True

End Sub

huangapple
  • 本文由 发表于 2023年5月26日 15:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定