英文:
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()
' 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.
答案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()
' Path for the file to be placed at
Dim path As String
path = "C:\Users\user\Testing\"
' The name of the new file
Dim targetFile As String
targetFile = "Test.xlsx"
Dim fullpathTargetFile As String
fullpathTargetFile = path & targetFile
'Application.DisplayAlerts = False
' open the workbook
Dim wbTarget As Workbook
Set wbTarget = Workbooks.Open(fullpathTargetFile)
Dim wsTarget As Worksheet
Set wsTarget = wbTarget.Worksheets(1)
'delete current content
wsTarget.UsedRange.Delete xlShiftUp
' Copy the used range from the original worksheet to the target workbook
sheet1.UsedRange.Copy wsTarget.Range("A1")
wbTarget.Close SaveChanges:=True
'Application.DisplayAlerts = True
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论