【问题标题】:macro getting bugged while re run重新运行时宏被窃听
【发布时间】:2025-12-12 12:40:02
【问题描述】:

无法在宏下面重新运行有谁知道它为什么会在文件位置中断?

还有人知道如何循环此宏以将更多工作表放入工作簿吗? 尝试从工作簿 2 中提取工作表并以下列方式对其进行格式化。

Sub specalign()
'
' specalign Macro
'

'
    Workbooks.Open Filename:= _
        "C:\\\\Interface Specifications Master v7 7.xlsx"
    Sheets("1.1.1").Select
    Sheets("1.1.1").Move After:=Workbooks( _
        "Interface Specifications Master v7.8.xlsx").Sheets(2)
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 8
    Columns("N:N").Select
    Selection.Delete Shift:=xlToLeft
    Range("N1").Select
    Range(Selection, Selection.End(xlToLeft)).Select
    Selection.ClearContents
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("A1:N1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
    End With
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlTop
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = True
    End With
    Range("Table1[[#Headers],[Spec ID]]").Select
    Selection.AutoFilter
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("Table1[[#Headers],[Spec ID]]").Select
    Range(Selection, Selection.End(xlToRight)).Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("A4").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    With Selection.Font
        .Name = "Arial"
        .Size = 8
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Range("A3:B3,G3:M3").Select
    Range("Table1[[#Headers],[  Conditionality ]]").Activate
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
        .Orientation = 90
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Rows("3:3").RowHeight = 108.75
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 1
    Columns("A:A").ColumnWidth = 8#
    Columns("B:B").ColumnWidth = 8#
    Columns("C:C").ColumnWidth = 34#
    Columns("D:D").ColumnWidth = 22#
    Columns("E:E").ColumnWidth = 17#
    Columns("G:G").ColumnWidth = 8#
    Columns("H:H").ColumnWidth = 22
    Columns("I:I").ColumnWidth = 8#
    Columns("J:J").ColumnWidth = 8#
    Columns("K:K").ColumnWidth = 8
    Columns("L:L").ColumnWidth = 6#
    Columns("M:M").ColumnWidth = 10
    
    
    End Sub

【问题讨论】:

  • 您是否从文件位置中删除了文件夹名称?你有很多可能没有帮助的斜线。
  • 是的,我删除了文件名的地址。宏在选择文件之前效果很好,但在那之后

标签: vba loops


【解决方案1】:

离开@nickslash 所说的你的文件名看起来是这个问题。 但是,您可以做很多事情来改进此代码,例如去掉滚动列和所有选择。

要获取文件路径,您可以右键单击文件夹中的文件并选择属性。 > 在常规选项卡上选择位置。然后你会看到你需要的路径。

当你学习 vba 时,你会比我在这里做的更好。并选择文件并使其更具动态性(可能会提示选择您想要的文件,因为 jean 演示了here)。

以下更新:

Sub specalign()
Dim ws As Worksheet

Dim wb1 As Workbook
Dim wb2 As Workbook

'this is what mine looks like with the directory
Set wb1 = Workbooks.Open("C:\Users\james\Documents\Interface Specifications Master v7.8.xlsx") 'target workbook .8 I kept
Set wb2 = Workbooks.Open("C:\Users\james\Documents\Interface Specifications Master v7 7.xlsx")  'source workbook


For Each ws In wb2.Worksheets
    ws.Copy after:=wb1.Worksheets(2)
Next

【讨论】:

  • 谢谢詹姆斯,现在看起来好多了,你能告诉我是否想在上面工作簿的所有工作表中让它成为一个循环我应该使用什么。
  • 我的意思是从 wb1 复制工作表,然后运行代码以在 wb2 中格式化,然后重复但从 wb1 复制下一张工作表
  • 是的,您可以通过 For each wks in Worksheets 'some code here Next 来做到这一点
  • 哪一张是目标,您要复制的工作表在哪里。所以 Interface Specifications Master v7.8.xlsx 是主要的,你想从 Interface Specifications Master v7 7.xlsx 复制所有的工作表?
  • 是的,詹姆斯,这就是目标