excel 编辑已打开的工作簿的电子表格

ilmyapht  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(157)

我可以打开、编辑、保存和关闭已关闭的电子表格。
我现在要做的是编写VBA代码,用于访问已打开的工作簿,将数据插入其中一个工作表的单元格中,然后保存数据。下面的代码不会出错,但不会将文本插入单元格中。

Sub CheckIfWorkBookIsOpen()
    Dim FilePath As String
    Dim FileName1 As String
    Dim myXL As Object
    
    FileName1 = "C:\Users\rasch\Documents\Daily to do lists\My current to do list8.xlsx"
    FilePath = IsWBOpen(FileName1)
    If FilePath = True Then
        MsgBox "File is Open"
        Set myXL = GetObject(, "Excel.Application")
        myXL.Workbooks(FileName1).Worksheets(1).Cells(3, 3) = "Helloooooooo!"
    Else
        MsgBox "File is Closed"
    End If
    Set myXL = Nothing
End Sub
vlju58qv

vlju58qv1#

您可以尝试对您的代码进行这种修改

Option Explicit

Sub CheckIfWorkBookIsOpen()    
    
    Dim FileName1 As String
    FileName1 = "C:\Users\rasch\Documents\Daily to do lists\My current to do list8.xlsx"

    Dim myXL As Object
    Set myXL = GetObject(, "Excel.Application") ' set the running instance of Excel and make it available for further calls
            
        Dim wb As Workbook
        If IsWBOpen(myXL, FileName1, _
                    wb) Then
            MsgBox "File is Open"
            wb.Worksheets(1).Cells(3, 3) = "Helloooooooo!"
        Else
            MsgBox "File is Closed"
        End If
        
        Set myXL = Nothing
            
End Sub

Function IsWBOpen(myXL As Object, fullFileName As String, _
                  wb As Workbook) As Boolean
    Dim fileName As String
        fileName = Split(fullFileName, myXL.PathSeparator)(UBound(Split(fullFileName, myXL.PathSeparator)))
        
        On Error Resume Next 'ignore any possible error originated by the following line
            Set wb = myXL.Workbooks(fileName) ' try setting a workbook object as if a workbook with the given name were alerady open
        On Error GoTo 0 ' reset normal error handling
        
            If Not wb Is Nothing Then ' if workbook actually set
                IsWBOpen = UCase$(wb.FullName) = UCase$(fullFileName) ' the searched workbook is open if its path matches the searched workbook one
            End If
End Function

如果您在ExcelVBA会话中运行所有这些操作,则可以省略所有myXL处理

Option Explicit

Sub CheckIfWorkBookIsOpen()
    
    Dim FileName1 As String
    FileName1 = "C:\Users\rasch\Documents\Daily to do lists\My current to do list8.xlsx"
    FileName1 = "c:\Users\Developers\Downloads\Book1 to be deleted.xlsm"
    
        Dim wb As Workbook
        If IsWBOpen(FileName1, _
                    wb) Then
            MsgBox "File is Open"
            wb.Worksheets(1).Cells(3, 3) = "Helloooooooo!"
        Else
            MsgBox "File is Closed"
        End If
            
End Sub

Function IsWBOpen(fullFileName As String, _
                  wb As Workbook) As Boolean
    Dim fileName As String
        fileName = Split(fullFileName, Application.PathSeparator)(UBound(Split(fullFileName, Application.PathSeparator)))
        
        On Error Resume Next 'ignore any possible error originated by the following line
            Set wb = Workbooks(fileName) ' try setting a workbook object as if a workbook with the given name were alerady open
        On Error GoTo 0 ' reset normal error handling
        
            If Not wb Is Nothing Then ' if workbook actually set
                IsWBOpen = UCase$(wb.FullName) = UCase$(fullFileName) ' the searched workbook is open if its path matches the searched workbook one
            End If
End Function

相关问题