两个问题 - 保存实例化工作簿中的更改,以及激活其他工作簿

时间:2017-03-08 14:51:42

标签: excel vba excel-vba

我有两个电子表格;我将它们称为电子表格1和电子表格2.电子表格1具有生成该月日期的功能,如果它在月末,则它会尝试在电子表格中调用模块/子这是为了生成"每天"报告和"每月"报告。

此时,有两个错误:第一个是我尝试保存我创建的电子表格2的新实例。错误是它要求以无宏格式保存工作簿。我只是想保存它!不对格式进行任何更改。我甚至不确定是否正在尝试将更改保存到实例化的book对象。

第二个是在电子表格2中,即使我将其设置为活动工作表(我认为),活动工作表仍然作为首先运行宏的电子表格1上的工作表出现。

感谢任何帮助。

    Option Explicit
Public Function LastWeekOfMonth() As Boolean

'finds the current date
    Dim CurrentDate As Date
    CurrentDate = CDate(ActiveSheet.Cells(FIRST_DATA_ROW, 1))

'find filepath and filename of the monthly documentation file
    Dim mFilePath As String
    Dim mFileName As String

    mFilePath = "F:\Project Sweep\Kim Checklist\Barry Polinsky\Brathwaite, Tamika\"
    mFileName = Cells(3, 4) & ".m_d.xlsm"

     'if it is the last week of the month, write a monthly report, and return true to continue with the face to face paperwork
     If (31 - Day(CurrentDate)) <= 7 Then
        'write a monthly report
        Dim app As New Excel.Application
        Dim book As Excel.Workbook

      '  app.Visible = False 'Visible is False by default, so this isn't necessary
        Set book = app.Workbooks.Add(mFilePath & mFileName)

        'run the subroutine CheckSpreadsheet in module WriteReport in target book
        app.Run "'" & mFilePath & mFileName & "'!" & "WriteReport" & ".CheckSpreadsheet", book
      '  CheckSpreadsheet (book)




        'error next line
        book.Save
        book.Close

        app.Quit
        Set app = Nothing
        LastWeekOfMonth = True

    'if it is not, simply continue with the face to face paperwork
     Else
        LastWeekOfMonth = False
     End If

End Function

在目标工作表中,在模块WriteReport,子例程CheckSpreadsheet中,找到以下代码。

Option Explicit

Public Sub CheckSpreadsheet(wbook As Excel.Workbook)


    Set wosheet = wbook.Sheets("Monthly")

wosheet.Cells(5, 5) = "Hello world!"

End Sub

1 个答案:

答案 0 :(得分:1)

不需要有另一个Excel实例,隐藏工作簿的属性是Windows,以隐藏工作簿使用的Excel窗口。还要记住,工作簿可以有多个窗口。

如果您确定要隐藏的工作簿只有一个窗口,请使用以下行:

Workbooks("WbkName").Windows(1).Visible = False

如果工作簿有多个窗口,请使用此过程:

Sub Wbk_Hide()
Dim wbk As Workbook, wdw As Window
    Set wbk = Workbooks("WbkName")   'Update as required
    For Each wdw In wbk.Windows
        wdw.Visible = False
    Next
End Sub

我相信这会改变你的程序范围,让我知道。