首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel宏帮助-堆叠宏

Excel宏帮助-堆叠宏
EN

Stack Overflow用户
提问于 2010-12-14 14:40:24
回答 3查看 772关注 0票数 0

我正在使用以下子例程将单个文件夹中的多个Excel文件合并为包含多个工作表的单个工作簿。

代码语言:javascript
复制
Sub Merge2MultiSheets()

Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim MyPath As String
Dim strFilename As String

Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
MyPath = "C:\MyPath" ' <-- Insert Absolute Folder Location
Set wbDst = Workbooks.Add(xlWBATWorksheet)
strFilename = Dir(MyPath & "\*.xls", vbNormal)

If Len(strFilename) = 0 Then Exit Sub

Do Until strFilename = ""            
    Set wbSrc = Workbooks.Open(Filename:=MyPath & "\" & strFilename)                
    Set wsSrc = wbSrc.Worksheets(1)                
    wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)                
    wbSrc.Close False            
    strFilename = Dir()            
Loop
wbDst.Worksheets(1).Delete

Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

最终产品是一个包含多个工作表(以及一个空白工作表1)的excel文件。我想知道如何才能将另一个宏应用到这个新创建的工作簿。例如,我希望这个新工作簿中的所有工作表的标题都以某种方式加粗和着色,并删除空的工作表。

例如:

代码语言:javascript
复制
Sub Headers()

Rows("1:1").Select
Selection.Font.Bold = True
With Selection.Interior
    .ColorIndex = 37
    .Pattern = xlSolid
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With

End Sub
EN

回答 3

Stack Overflow用户

发布于 2010-12-15 04:02:00

代码语言:javascript
复制
Sheets.Select       'selects all sheets'
Rows("1:1").Select
Selection.Interior.ColorIndex = 37
票数 1
EN

Stack Overflow用户

发布于 2010-12-14 14:47:00

在Headers中添加一个指定工作表的参数,然后在复制后的Do循环中的某个地方调用sub,如:

代码语言:javascript
复制
Call Headers(wbDst.Worksheets(wbDst.Worksheets.Count))

你的第二个sub看起来像这样:

代码语言:javascript
复制
Sub Headers(workingSheet As Worksheet)

workingSheet.Rows("1:1").Select
Selection.Font.Bold = True
With Selection.Interior
.
.
.
票数 0
EN

Stack Overflow用户

发布于 2010-12-14 18:41:06

此代码将执行以下操作:

1)首先,按照您在帖子中的要求删除Sheet1

2)设置剩余工作表中第一行的格式

代码语言:javascript
复制
Sub Headers()
Dim wkSheet As Worksheet

//Delete Sheet1. Note that alerts are turned off otherwise you are prompted with a dialog box to check you want to delete sheet1
Application.DisplayAlerts = False
Worksheets("Sheet1").Delete
Application.DisplayAlerts = False

//Loop through each worksheet in workbook sheet collection
For Each wkSheet In ActiveWorkbook.Worksheets
    With wkSheet.Rows("1:1")
        .Interior.ColorIndex = 37
        //Add additional formatting requirements here
    End With
Next

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4436570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档