使用与当前打开的 CSV 相同的名称保存 CSV
Save CSV with same name as current open CSV
我有一个宏,它在 CSV 上运行个人工作簿中的单独循环宏(无需打开它),应该使用相同的名称重新保存文档。
我在尝试保存时遇到此错误。
Run-time error '1004':
You cannot save this workbook with the same name as another open workbook or add-in. Choose a different name or close the other workbook or add-in before saving.
我需要这个,因为有一个工具正在寻找这个文档名称。
这是打开csv并运行循环宏的宏(不打开Excel)。
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("F:\Folder\specific name.csv", 0, True)
xlapp.workbooks.open("C:\Users\name\AppData\Roaming\Microsoft\Excel\XLSTART \Personal.xlsb")
xlApp.Run "Personal.xlsb!LoopColumnC "
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
LoopColumnC 的宏是它试图保存的地方。
Sub LoopColumnC()
'
' LoopColumnC Macro
'
'
'This is to disregard any popups.
Application.DisplayAlerts = False
'This is the actual loop. Works just fine.
Dim lastRow As Long
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To lastRow
If Cells(i, 3).Value = 1 Then Cells(i, 3).Value = Cells(i - 1, 3).Value
Next i
'Here is where it errors out because the file it is trying to save as is already opened in the background.
ThisWorkbook.SaveAs "F:\Folder\specific name.csv",
Application.DisplayAlerts = True
End Sub
您应该修改您的代码以将 worksheet 对象直接传递给被调用的宏 - 不要依赖 Activesheet 等
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
'if you want to save with the same name then you can't open as read-only...
Set xlBook = xlApp.Workbooks.Open("F:\Folder\specific name.csv", 0, True)
xlapp.workbooks.open("C:\Users\name\AppData\Roaming\Microsoft\Excel\XLSTART \Personal.xlsb")
xlApp.Run "Personal.xlsb!LoopColumnC", xlBook.Worksheets(1) '<< passes the sheet
xlBook.Close '<<
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
您的处理宏接收 sheet 对象:
'gets a worksheet object from the caller
Sub LoopColumnC(ws As Worksheet)
Dim lastRow As Long
'operate on the provided worksheet
lastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 3).Value = 1 Then
ws.Cells(i, 3).Value = ws.Cells(i - 1, 3).Value
End If
Next i
Application.DisplayAlerts = False
ws.Parent.SaveAs "F:\Folder\specific name.csv" 'ws.Parent = workbook
Application.DisplayAlerts = True
End Sub
我有一个宏,它在 CSV 上运行个人工作簿中的单独循环宏(无需打开它),应该使用相同的名称重新保存文档。
我在尝试保存时遇到此错误。
Run-time error '1004':
You cannot save this workbook with the same name as another open workbook or add-in. Choose a different name or close the other workbook or add-in before saving.
我需要这个,因为有一个工具正在寻找这个文档名称。
这是打开csv并运行循环宏的宏(不打开Excel)。
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("F:\Folder\specific name.csv", 0, True)
xlapp.workbooks.open("C:\Users\name\AppData\Roaming\Microsoft\Excel\XLSTART \Personal.xlsb")
xlApp.Run "Personal.xlsb!LoopColumnC "
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
LoopColumnC 的宏是它试图保存的地方。
Sub LoopColumnC()
'
' LoopColumnC Macro
'
'
'This is to disregard any popups.
Application.DisplayAlerts = False
'This is the actual loop. Works just fine.
Dim lastRow As Long
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To lastRow
If Cells(i, 3).Value = 1 Then Cells(i, 3).Value = Cells(i - 1, 3).Value
Next i
'Here is where it errors out because the file it is trying to save as is already opened in the background.
ThisWorkbook.SaveAs "F:\Folder\specific name.csv",
Application.DisplayAlerts = True
End Sub
您应该修改您的代码以将 worksheet 对象直接传递给被调用的宏 - 不要依赖 Activesheet 等
Sub ExcelMacroExample()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
'if you want to save with the same name then you can't open as read-only...
Set xlBook = xlApp.Workbooks.Open("F:\Folder\specific name.csv", 0, True)
xlapp.workbooks.open("C:\Users\name\AppData\Roaming\Microsoft\Excel\XLSTART \Personal.xlsb")
xlApp.Run "Personal.xlsb!LoopColumnC", xlBook.Worksheets(1) '<< passes the sheet
xlBook.Close '<<
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
您的处理宏接收 sheet 对象:
'gets a worksheet object from the caller
Sub LoopColumnC(ws As Worksheet)
Dim lastRow As Long
'operate on the provided worksheet
lastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row
For i = 2 To lastRow
If ws.Cells(i, 3).Value = 1 Then
ws.Cells(i, 3).Value = ws.Cells(i - 1, 3).Value
End If
Next i
Application.DisplayAlerts = False
ws.Parent.SaveAs "F:\Folder\specific name.csv" 'ws.Parent = workbook
Application.DisplayAlerts = True
End Sub