このループVBAスクリプトは、アクティブなブックで最初のワークシートを完了した後に停止しますが、すべてのワークシートをループする必要があります。誰かがループがすべてのワークシートを連続して動かすために私が逃していることを理解するのを助けることができますか?このVBAを現在のワークブックのすべてのワークシートにループさせようとしています。
Sub forEachws()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call Music_Connect_Albums(ws)
Next
End Sub
Sub Music_Connect_Albums(ws As Worksheet)
With ws
.Columns("B:H").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("A13").Select
ActiveCell.FormulaR1C1 = "Artist"
.Range("B13").Select
ActiveCell.FormulaR1C1 = "Title"
.Range("C13").Select
ActiveCell.FormulaR1C1 = "Release"
.Range("D13").Select
ActiveCell.FormulaR1C1 = "Label"
.Range("E13").Select
ActiveCell.FormulaR1C1 = "Age"
.Range("F13").Select
ActiveCell.FormulaR1C1 = "Yr"
.Range("G13").Select
ActiveCell.FormulaR1C1 = "Wk"
.Range("H13").Select
ActiveCell.FormulaR1C1 = "Wk-End"
.Range("A14").Select
ActiveCell.FormulaR1C1 = "=R2C10"
.Range("B14").Select
ActiveCell.FormulaR1C1 = "=R3C10"
.Range("C14").Select
ActiveCell.FormulaR1C1 = "=R4C10"
.Range("D14").Select
ActiveCell.FormulaR1C1 = "=R5C10"
.Range("E14").Select
ActiveCell.FormulaR1C1 = "=R9C10"
.Range("F14").Select
ActiveCell.FormulaR1C1 = "=RIGHT(R8C10,4)"
.Range("G14").Select
ActiveCell.FormulaR1C1 = "=MID(R13C10,6,2)"
.Range("G14").Select
ActiveCell.FormulaR1C1 = "=MID(R13C13,6,2)"
.Range("H14").Select
ActiveCell.FormulaR1C1 = "=RIGHT(R8C10,10)"
.Range("A14:H14").Select
Selection.AutoFill Destination:=Range("A14:H35")
.Range("A14:H35").Select
.Columns("A:H").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
.Rows("1:13").Select
.Range("A12").Activate
.Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
.Columns("I").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With
End Sub
1ワークシートが予期したとおりに修正された後の結果:VBA実行時エラー '1004':Rangeクラスの選択メソッドが失敗しました – aksnave
'.Select'と' Selection.'をすべて削除する必要があります。https ://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros。 –
^^^^は、最も恐ろしいコーディングであるシートを有効にすると、うまくいくはずです'With ws'の直後に' .Activate'を実行します。非アクティブなシート上の範囲は選択できません。しかし、真剣に活性化と選択を取り除く。コードをより迅速かつ信頼性の高いものにするでしょう。 –