1

我正在尝试打开工作簿并从中删除工作表,但它运行代码没有错误,工作表仍然存在......我可以修改它,因为我将公式更改为另一张工作表上的值。首先 - 是的,我知道“i”变量设置为进行 1 次迭代。不知何故,现在当我打开工作簿时,它说它被我锁定了——我什至不知道该怎么做。

那么……我该如何解锁呢?当我转到文件-->信息-->权限时,它说'任何人都可以复制、更改和修改此工作簿的任何部分......我也可以手动删除工作表......

这是代码:

Sub Change()

Dim wb As Excel.Workbook
Set wb = ThisWorkbook

Dim ws As Excel.Worksheet
Set ws = wb.Sheets("FileSearch Results")

Dim rng As Range
Set rng = ws.UsedRange

Dim cPaths As Integer
cPaths = rng.Column

Dim i As Integer
i = rng.Row

Dim oExcel As Excel.Application
Set oExcel = New Excel.Application

Dim oWB As Workbook

Dim komm As Excel.Worksheet
Dim sh1 As Excel.Worksheet

Do While i < 2
    Dim pth As String
    pth = ws.Cells(i, cPaths)
    Set oWB = oExcel.Workbooks.Open(pth)

    Set sh1 = oWB.Worksheets("Sheet1")
    With sh1.UsedRange
        .Value = .Value
    End With

    Set komm = oWB.Worksheets("Kommentar")
    Application.DisplayAlerts = False
    komm.Delete
    Application.DisplayAlerts = True



    oWB.Close savechanges:=True
    i = i + 1
Loop
End Sub

有任何想法吗?

4

2 回答 2

1
Sub Change()

Dim wb As Excel.Workbook
Set wb = ActiveWorkbook 'ThisWorkbook

Dim ws As Excel.Worksheet
Set ws = wb.Sheets("FileSearch Results")

Dim rng As Range
Set rng = ws.UsedRange

Dim cPaths As Integer
cPaths = rng.Column

Dim i As Integer
i = rng.row

'Dim oExcel As Excel.Application ***CHANGED***
'Set oExcel = New Excel.Application ***CHANGED***

'Dim oWB As Workbook ***CHANGED***

Dim komm As Excel.Worksheet
Dim sh1 As Excel.Worksheet

Do While i < 2
    Dim pth As String
    pth = ws.Cells(i, cPaths)
    'Set oWB = oExcel.Workbooks.Open(pth) ***CHANGED***

    Workbooks.Open (pth) '***ADDED***

    Set sh1 = ActiveWorkbook.Worksheets("Sheet1") 'oWB.Worksheets("Sheet1") ***CHANGED***
    With sh1.UsedRange
        .Value = .Value
    End With

    Set komm = ActiveWorkbook.Worksheets("Kommentar") 'oWB.Worksheets("Kommentar") ***CHANGED***
    Application.DisplayAlerts = False
    komm.Delete
    Application.DisplayAlerts = True

    ActiveWorkbook.Close savechanges:=True 'oWB.Close savechanges:=True ***CHANGED***
    i = i + 1

Loop

End Sub

This now opens the workbook and deletes the sheet in the foreground rather than invoking a new instance of Excel and deleting the sheet in the background. This is why the file stays locked, as the new instance which isn't closed by the code, still holds it.

于 2013-05-08T11:23:30.423 回答
0

For anyone running into this in the future (like myself), the actual problem is with the mix up in scope when calling Application.DisplayAlerts.

komm is a sheet in oWB, which exists in the new instance of excel oExcel.

Application is a completely different instance and therefor has no effect.

Since the code isn't actually disabling the prompt in the correct instance of excel (oExcel) and it is presumably not visible, the code will just ignore the command and move on.

The simple fix is to use oExcel instead of Application:

Set komm = oWB.Worksheets("Kommentar")
oExcel.DisplayAlerts = False
komm.Delete
oExcel.DisplayAlerts = True
于 2018-07-09T18:07:51.517 回答