所以我写了一个makro,它在除第一个工作表外的每个工作表中插入一个新的Column。它工作正常。我唯一的问题是,在完成最后一个工作表后,我希望它跳回到我开始的工作表。我在网上找到的所有解决方案都说,这一行:Sheets(“ Sheet Name”)。Select应该完成任务。但是,它对我没有帮助。我究竟做错了什么?我也希望提出改进代码的建议。
Option Explicit
Sub NeueSpalte()
Dim ende As Boolean
Dim Name As Variant
Dim Anzahl_WS As Integer
Dim Zaehler As Integer
Do While ende = False
Name = InputBox("Name der neuen Spalte")
If StrPtr(Name) = 0 Then
MsgBox ("Abgebrochen!")
Exit Sub
ElseIf Name = "" Then
MsgBox ("Bitte etwas eingeben")
Else
Exit Do
End If
Loop
Anzahl_WS = ActiveWorkbook.Worksheets.Count - 1
Sheets("Rechte auf Verträge der A3").Select
Application.ScreenUpdating = False
For Zaehler = 1 To Anzahl_WS
Cells(1, 2).EntireColumn.Copy
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Activate
ActiveCell.EntireColumn.Insert
Application.CutCopyMode = False
Cells(1, Columns.Count).End(xlToLeft).Activate
ActiveCell.EntireColumn.Select
Selection.ClearContents
Cells(8, 2).MergeCells = False
Cells(1, Columns.Count).End(xlToLeft).Offset(7, 1).Activate
Range(Cells(8, 2), ActiveCell).MergeCells = True
Cells(8, 2).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 = xlMedium
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Value = Name
If ActiveSheet.Name = "Rechte auf Verträge der WW" Then
Exit Sub
Else
ActiveSheet.Next.Select
End If
Next Zaehler
Application.ScreenUpdating = True
Sheets("Rechte auf Verträge der A3").Select
End Sub
预期结果:将b列复制到第一个空列中,删除其内容,并将用户选择的名称插入新列的第1行中。对每张纸都这样做,然后跳回到第二张纸上。
实际结果:它所做的一切都很好,但不会跳到工作表2
答案 0 :(得分:0)
我知道了。问题出在我的for循环的末尾,在if分支中,我写了“ Exit Sub”,如果它在最后一张纸上。我只是将“ Sheets(“ Rechte aufVerträgeder A3”)。Select”放在“退出子”之前
答案 1 :(得分:0)
一个小例子:
Option Explicit
Sub Select_Sheet()
'Declare the variable
Dim ws1 As Worksheet
'Set the variable
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
'Create a with statement to avoid repetition
With ws1
'Refer to range A1
.Range ("A1")
'Refer to the value of range A1
.Range("A1").Value
End With
End Sub
请注意,在创建With Statement
时,使用的范围应带有点“”。在赎金之前.Range("A1").Value