Excel VBA文件已经打开错误当它不是 - Mac Office

时间:2017-03-12 16:22:44

标签: excel vba macos

我正在尝试编写我正在处理的Excel文件的宏代码(在Office 2016 for Mac中)。

代码由两部分组成: 1.一个Workbook_SheetChange监听器,当某些单元改变时,将触发一个打开csv文件,读取它,然后根据我的需要操作它的函数。这部分工作正常。 2.需要从提到的相同csv文件中读取的按钮,并根据它更改工作簿中的单元格。它一直在工作,直到我添加了上述功能。

现在,当我按下按钮从csv读取时,错误跳转了#34;文件已经打开"当我按下调试以查看失败的位置时,它会将我引用到工作表更改宏中的功能(根本不应该触发)。

读取功能是:

Private Sub ReadCSV_Click()
Dim serial As String
Dim signer As String
loc_idx = -1
Dim FilePath As String
FilePath = ThisWorkbook.Path + "/test.csv"
Open FilePath For Input As #1
row_number = 0

Do Until EOF(1)
    Line Input #1, LineFromFile
    LineItems = Split(LineFromFile, ",")
    signer = Replace(LineItems(0), Chr(34), vbNullString) 'Location
    If Not signer = vbNullString Then
        serial = Replace(CStr(LineItems(1)), Chr(34), vbNullString) 'SN
        Call SearchCell(serial) 'Search the SN
        Call SearchLocationCol(f_ws_idx) 'Search Location column in the relevant sheet
        Worksheets(f_ws_idx).Cells(f_row, loc_idx).Value = signer
    End If
    row_number = row_number + 1
Loop

Close #1
MsgBox "Done Updating"

End Sub

监听器功能是:

Option Explicit

Public Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        If Not Sh.Name = "Macros" Then
            Call InsertChange("test", "1111")
        End If
    End Sub

    Private Sub InsertChange(n_loc As String, n_ser As String)
        Dim LineFromFile As String
        Dim LineItems() As String
        Dim split_new_csv() As String
        Dim is_new As Boolean
        Dim serial_changed As String
        Dim location_changed As String
        Dim new_csv_str As String
        Dim signer As String
        Dim serial As String
        Dim row_number As Integer
        Dim rec As Variant
        Dim rec_item As Variant
        is_new = True
        new_csv_str = ""
        serial_changed = n_ser
        location_changed = n_loc

        Dim FilePath As String
        FilePath = ThisWorkbook.Path + "/test.csv"

        Dim FilePathTemp As String
        Dim scriptstr As String

        scriptstr = "return posix path of (path to desktop folder) as string"

        FilePathTemp = MacScript(scriptstr) + "s.csv"
        Open FilePath For Input As #1
            row_number = 0
            Do Until EOF(1) 'Go Through all of the lines in the csv
                Line Input #1, LineFromFile
                LineItems = Split(LineFromFile, ",")
                signer = Replace(LineItems(0), Chr(34), vbNullString) 'Location
                If Not signer = vbNullString Then
                    serial = Replace(CStr(LineItems(1)), Chr(34), vbNullString) 'SN
                    If serial = serial_changed Then
                        is_new = False
                        signer = location_changed
                    End If
                    new_csv_str = new_csv_str + signer + "," + serial + "\n"
                    row_number = row_number + 1
                End If
            Loop
        Close #1

        MsgBox "Done updating"
        End Sub

是的,代码需要清理,但调试错误引用我:

Open FilePath For Input As #1

在听众代码中,虽然它根本不应被激活。我尝试更改变量名称,甚至在侦听器中创建副本并将其作为其他文件使用,但它仍然无法正常工作。

关于如何解决的任何想法?

感谢。

1 个答案:

答案 0 :(得分:0)

我怀疑事件会重新触发workheet_change的执行。我会在输入insertchange时禁用事件。但是,您发布的代码似乎与工作表无关。

Public Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        If Not Sh.Name = "Macros" Then
            application.enableevents=False
            Call InsertChange("test", "1111")
            application.enableevents=true
        End If
End Sub