2016-09-16 7 views
0

多分私はこれをあまりにも待っていましたが、Excelのワークシートをコピーするマクロがあります。明らかにVBAコピーシートマクロと数式r1c1を使用

Sub Macro4() 
' 
' Macro4 Macro 
' 
' Keyboard Shortcut: Ctrl+a 
' 
Sheets("<Null>").Select 
ActiveSheet.Buttons.Add(541.5, 97.5, 95.25, 43.5).Select 
ActiveSheet.Buttons.Add(541.5, 169.5, 94.5, 42.75).Select 
Sheets("<Null>").Copy After:=Sheets(3) 
ActiveCell.FormulaR1C1 = "='Dividing Walls Only'!RC[-2]" 
Range("C4").Select 
Sheets("<Null> (2)").Select 
ActiveSheet.Buttons.Add(541.5, 97.5, 95.25, 43.5).Select 
ActiveSheet.Buttons.Add(541.5, 169.5, 95.25, 42.75).Select 
Sheets("<Null> (2)").Copy After:=Sheets(4) 
Range("C3").Select 
ActiveCell.FormulaR1C1 = "='Dividing Walls Only'!R[1]C[-2]" 
Range("C4").Select 
Sheets("<Null> (3)").Select 
ActiveSheet.Buttons.Add(541.5, 97.5, 95.25, 43.5).Select 
ActiveSheet.Buttons.Add(541.5, 169.5, 95.25, 42.75).Select 
Sheets("<Null> (3)").Copy After:=Sheets(5) 
Range("C3").Select 
ActiveCell.FormulaR1C1 = "='Dividing Walls Only'!R[2]C[-2]" 
Range("C4").Select 
End Sub 

が、これは180回を繰り返すことは愚かなことでしょう:私は何もやろうとしていることはループ(この記録したマクロからわずかR1C1式)にこれを含めています。

Sub CopySheet() 

Call OptimizeCode_Begin 

Dim x As Integer 

x = InputBox("Enter number of times to copy active sheet") 
For numtimes = 1 To x 
    'Loop by using x as the index number to make x number copies 
    ActiveWorkbook.ActiveSheet.Copy _ 
     After:=ActiveWorkbook.Sheets(3) 
     'Put copies in front of Sheet3 
     'Might need to move the new sheets 

Next 

Call OptimizeCode_End 

End Sub 

私がやりたい何を自動的に入力することから私を続けるだろう、各シートにR1C1式を進めるために、ネストされたループまたは何かを取り入れているのいずれか:これは私が既に持っているマクロはコピー用紙でありますすべてのシートがコピーされた後で参照しようとしているセル。どんな助けもありがとう。

ありがとうございます!

ジャスティン

+0

最初のビットを変更してワークシートの入力をしてループ内で呼び出すのはなぜですか?私はあなたが何をしようとしているのか理解できないかもしれません。 – Liss

+0

@ジャスティン・ビーチレイ新しいコピーシートごとにセル「C4」の数式をどのように進めるかわからない、私の答えは以下のとおりです –

答えて

0

あなたの投稿からわかったことから、以下のコードはユーザーが選択した回数に応じてInputBoxで実行され、最後の後にシートがコピーされます。

作成されたシートごとに、式をセルC4に追加します。各シートの式を進める上での論理がわかりません。

Sub CopySheets() 

Dim x   As Long 
Dim numtimes As Long 
Dim newSht  As Worksheet 


x = Application.InputBox("Enter number of times to copy active sheet", Default:=1, Type:=1) 

' optimize run time 
Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 
Application.EnableEvents = False 
Application.DisplayAlerts = False 

' create the Buttons on the original sheet 
' (will be copied inside the loop for all other sheets) 
ActiveSheet.Buttons.Add(541.5, 97.5, 95.25, 43.5).Select 
ActiveSheet.Buttons.Add(541.5, 169.5, 94.5, 42.75).Select 

For numtimes = 1 To x 

    'Loop by using x as the index number to make x number copies 
    ActiveWorkbook.ActiveSheet.Copy _ 
      After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count) 

    Set newSht = ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count) 

    ' give the new Sheet Name the reference of num of times 
    newSht.Name = "<NULL " & numtimes & ">" 

    ' advance the row number in the formula 
    newSht.Range("C3").FormulaR1C1 = "='Dividing Walls Only'!R[" & numtimes & "1]C[-2]" 

Next numtimes 

' Resume Settings 
Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 
Application.EnableEvents = True 
Application.DisplayAlerts = True 

End Sub 
+0

ありがとう、これは完璧に機能しました。私は最初のシートにボタンが必要なので、ボタンのコード行を取り出しました。私はまた行を進めている行に "&numtimes&"の後に "1"があることに気づいたので、R [11]、R [21]などに値を設定していましたが、それは簡単な修正でした。 –

+0

あなたが欲しかった正確な参照、答えとしてマークしていただきありがとうございました –

0

が、これはあなたが後にしているものです場合があります

あなたは前者があなたをすることができますので、私は エクセル(すなわち Applicationに)代わりに VBA InputBox() 1の InputBox()方法を使用参照
Option Explicit 

Sub CopySheet() 
    Dim numtimes As Long, x As Long, rowIndex As Long 

    Call OptimizeCode_Begin 
    rowIndex = 4 '<-- this is the row index that will be used in the formula that'll be written in the first new sheet 
    numtimes = Application.InputBox("Enter number of times to copy active sheet", Default:=1, Type:=1) 
    For x = 1 To numtimes 
     'Loop by using x as the index number to make x number copies 
     ActiveWorkbook.ActiveSheet.Copy _ 
     After:=ActiveWorkbook.Sheets(3) 
     Range("C3").Formula = "='Dividing Walls Only'!A" & rowIndex '<--| write formula in the new sheet cell "C3" referencing "Dividing Walls Only" worksheet column "A" cell in current 'rowIndex' 
     rowIndex = rowIndex + 1 '<--| update row index for subsequent new sheet formula 
    Next 
    Call OptimizeCode_End 
End Sub 

リターンデータ型も指定してください(数値入力の場合はタイプ= 1)ので、ユーザー入力を強制的に入力してください。

関連する問題