在Excel 2010中,在有人点击移动或复制到新工作簿后,我有一个要求,然后在新工作簿上运行以下语句:
Sub CSAColorFormattingSSRS2008()
ActiveWorkbook.Colors = Workbooks("Book4").Colors
End Sub
问题是“Book4”是工作表来自的上一个工作簿,但此工作簿名称可能会更改。有没有办法通过更改您正在复制的工作簿名称来执行此操作?我试图创建一个按钮来为每个人运行这个宏,但所有过去的书籍名称总是不同。
答案 0 :(得分:1)
您可以执行以下操作:按钮对其执行复制操作,然后设置颜色。
Option Explicit
Sub CopySetColor()
Dim strWkb As String, strSheet As String
strWkb = ThisWorkbook.Name
strSheet = ActiveSheet.Name
'get Workbook name to copy to -> ensure that only 2 workbooks are open, this and the one you want to copy to
Dim wkb As Workbook, wkbCopyTo As Workbook
For Each wkb In Workbooks
If wkb.Name <> strWkb Then
Set wkbCopyTo = wkb
Exit For
End If
Next
Sheets(strSheet).Copy Before:=wkbCopyTo.Sheets(1)
CSAColorFormattingSSRS2008 strWkb
End Sub
Sub CSAColorFormattingSSRS2008(strName As String)
ActiveWorkbook.Colors = Workbooks(strName).Colors
End Sub