MAcro:选择一个文件来修改,修改每个工作表,导出xlsx和pdf

问题描述 投票:0回答:2

我有一个由系统每周导出的文件,需要在每个工作表中稍微修改,并根据该特定单元格中的一个单元格重命名所有工作表(E7)。无论我怎么努力,我都无法让它循环。我缺少什么想法?我假设它与'konstandst'变量有关,以及我如何命名表,但可以修复..

Sub Formateraom()
    ' Format and change name of the sheet
    Dim ws As Worksheet
    Dim weekNR As Variant
    Dim konstnadst As Variant

    weekNR = InputBox("What week number is it?")
    For Each ws In Worksheets
        Set ws = ActiveSheet
        konstnadst = Range("E7")
        Range("A2:C2").Select
        Selection.ClearContents
        Range("A5:T5").Select
        Selection.ClearContents
        Columns("C:C").ColumnWidth = 75#
        Rows("5:7").Select
        With Selection
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
        End With
        With Selection
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
        End With
        Columns("H:H").ColumnWidth = 13
        Range("H7,M7,G7").Select
        Range("G7").Activate
        Selection.NumberFormat = "m/d/yyyy"
        Columns("M:M").ColumnWidth = 13
        Columns("G:G").ColumnWidth = 13
        Range("C3").Select
        ActiveCell.FormulaR1C1 = weekNR
        Range("C4").Select
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range("C3").Select
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        ActiveSheet.Name = "Fakturaunderlag " & konstnadst & " " & weekNR
    Next
End Sub

向任何可以指向正确方向的人发送巨大的业力球!

excel vba pdf
2个回答
0
投票

Set ws = ActiveSheet总是将当前的循环表(即ws)设置为当前的循环表。

这样你就可以得到相同的工作表,在循环开始之前一个工作表

所以你只需要改变

Set ws = ActiveSheet

ws.Activate

从而使当前的环路板成为活动环路板


虽然上面的补丁可能(似乎)工作,但它也是一个糟糕的编码习惯,你被热烈邀请避免Activate/ActiveXXX/Select/Selection模式,并切换到直接和合格的工作表(和工作簿,如果有可能超过一个在宏运行的时候打开)Range参考

所以你的代码可以变成以下代码:

Option Explicit

Sub Formateraom()
    ' Format and change name of the sheet
    Dim ws As Worksheet
    Dim weekNR As Variant
    Dim konstnadst As Variant

    weekNR = InputBox("What week number is it?")
    For Each ws In Worksheets
        With ws ' reference the current loop sheet. inside the 'With ... End With' block, all its members are accessed by means of a dot (.)
            konstnadst = .Range("E7") ' initialize 'konstnadst' to referenced sheet cell E7 value
            .Range("A2:C2").ClearContents
            .Range("A5:T5").ClearContents
            .Columns("C:C").ColumnWidth = 75#
            With .Rows("5:7") ' reference referenced sheet rows 5 to 7
                .VerticalAlignment = xlBottom
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext

                .VerticalAlignment = xlCenter
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext

                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
            End With

            .Columns("H:H").ColumnWidth = 13
            .Range("H7,M7,G7").NumberFormat = "m/d/yyyy"
            .Columns("M:M").ColumnWidth = 13
            .Columns("G:G").ColumnWidth = 13
            .Range("C3").FormulaR1C1 = weekNR

            With .Range("C4") ' reference referenced sheet cell C4
                .HorizontalAlignment = xlRight
                .VerticalAlignment = xlCenter
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With

            With .Range("C3") ' reference referenced sheet cell C3
                .HorizontalAlignment = xlRight
                .VerticalAlignment = xlCenter
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False

                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlCenter
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With

            .Name = "Fakturaunderlag " & konstnadst & " " & weekNR ' change the name of the referenced sheet
        End With
    Next
End Sub

0
投票

以下问题实际上是您需要使用.Select的情况。在几乎所有其他实例中,永远不要使用它。

Question regarding how to export xlsx as pdf

This question should help you learn how to open an excel file using a file dialog box

我不确定你想用weekNR做什么,考虑到你以后用它做这件事:

ActiveCell.FormulaR1C1 = weekNR

所以我会忽略它,直到我得到更多关于它的信息。

由于konstnadstRange对象,正如您已经指定的那样,我建议将其声明为Range对象,并引用您正在使用的工作表,如下所示:

Dim konstandadst As Range
'you need to Set objects such as Ranges, Worksheets, Workbooks, ect.
Set konstandadst = whateverWsThisIs.Range("E7")

使用Range.Activate相当于点击范围,这在你的环境中似乎毫无用处,所以摆脱它。

使用:

Range1.Select
Range2.Select
Range3.Select

结果你只在这个块完成时选择Range3

我强烈建议永远不要使用.Select,而是在你的范围内创建引用变量,直接使用它们,如下所示:

'these select cell A1
Set MyRange = ws.Range("A1")
Set MyRange = ws.Cells(1,1)

'this selects column B
Set MyRange = ws.Range("B:B")

'this selects the row from A1 to B1
Set MyRange = ws.Range(ws.Cells(1,1), ws.Cells(1,2))

'this selects a table defined from A1 to C2
Set MyRange = ws.Range(ws.Cells(1,1), ws.Cells(2,3))

不要这样做:

For Each ws In Worksheets

这样做是因为您想要明确告诉VBA您引用Worksheets集合的工作簿:

For Each ws In ThisWorkbook.Worksheets

或者,如果你像我一样狂热:

For Each ws In Excel.Application.ThisWorkbook.Worksheets

以下是您可以使用Range对象执行的一些相关操作(更多here):

'clears the values in the cells
MyRange.ClearContents
'clears the formatting and formulas in the cells
MyRange.Clear
'adjust column width
MyRange.ColumnWidth = someNumber
'adjust row height
MyRange.RowHeight = someOtherNumber
'eliminate indents (i think)
MyRange.IndentLevel = 0
'change the orientation
MyRange.Orientation = 0

将参考变量设置为所需的范围后,可以像这样使用它们:

With MyRange
    'do the stuff here
End With

代替:

With Selection
    'bad stuff here, don't do the stuff
End With
© www.soinside.com 2019 - 2024. All rights reserved.