我有一个文件,系统每周都会导出该文件,该文件需要在每个工作表中稍作修改,而所有工作表都需要基于该特定单元格(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
向任何能向我指出正确方向的人发送巨大的业力球!
答案 0 :(得分:0)
Set ws = ActiveSheet
始终将当前循环表(即ws
)设置为当前 Active 之一。
这样,您将始终获得相同的工作表,该工作表在循环开始前处于活动状态
所以您只需要更改
Set ws = ActiveSheet
到
ws.Activate
因此将当前循环工作表设置为 active 一个
但是尽管上面的补丁程序可能(似乎)有效,但它也是一种不良的编码习惯,因此我们热烈地邀请您避免使用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
答案 1 :(得分: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
因此,在我获得有关它的更多信息之前,我将忽略它。
由于konstnadst
是一个Range
对象(如您所分配的),因此建议您将其声明为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