两个问题 – 在实例化的工作簿中保存更改,并激活其他工作簿

我有两个电子表格; 我会称它们为电子表格1和电子表格2.电子表格中的一个函数会生成一个月中的几天,如果是在月末,它将尝试调用电子表格2中的module / sub。这是为了生成“每日”报告和“每月”报告。

在这一点上,有两个错误:第一个是当我试图保存我创build的电子表格2的新实例。 错误是它要求以无macros的格式保存工作簿。 我只是想保存它! 不要对格式进行任何更改。 我甚至不确定它试图保存对实例化书对象的更改。

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

任何帮助表示赞赏。

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中,find以下代码。

 Option Explicit Public Sub CheckSpreadsheet(wbook As Excel.Workbook) Set wosheet = wbook.Sheets("Monthly") wosheet.Cells(5, 5) = "Hello world!" End Sub 

不需要另一个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 

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