2016-06-22 15 views
0

Wir haben ein Makro über eine Schaltfläche in Excel 2013 arbeiten, um eine neue Zeile mit Formel von oben einzufügen. Das Problem ist, wenn es läuft und ich sage zum Beispiel aus Zeile 10 kopieren die Formel in der neuen Zeile 11 alle noch in Zeile 10 nicht 11 gelesen?Formel von oben kopieren

Sub Loop_InsertRowsandFormulas() 

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Risk Input Sheet") 
Dim vRows As Long 
Dim lastCol As Long 
Dim firstRow As Long 

firstRow = InputBox("Enter Row To Start Insert From.") 
vRows = InputBox("Enter Number Of Rows Required") 

If firstRow = 0 Or vRows = 0 Then Exit Sub 
Debug.Print firstRow 
IngA = ws.Cells(5, ws.Columns.Count).End(xlToLeft).Column 
For myLoop = 1 To vRows 
    ws.Range("A" & (firstRow + myLoop)).EntireRow.Insert 
    ws.Range("A" & (firstRow + myLoop) & ":BB" & (firstRow + myLoop)).Formula = ws.Range("A" & firstRow & ":BB" & firstRow).Formula 
Next 

End Sub 
+0

Wie kann ich es nur kopieren die Formel und nicht den Text. – Chris

Antwort

0

Sie müssen eine Copy/Paste tun. Zum Beispiel, wenn A1 enthält:

=B1+C1 

Lauf:

Sub qwerty() 
    Range("A2").Formula = Range("A1").Formula 
End Sub 

A2 mit =B1+C1 auch verlassen.

Wenn Sie die kopierte Formel "anpassen", dann:

Sub ytrewq() 
    Range("A1").Copy Range("A2") 
End Sub 

EDIT # 1:

Statt:

ws.Range("A" & (firstRow + myLoop) & ":BB" & (firstRow + myLoop)).Formula = ws.Range("A" & firstRow & ":BB" & firstRow).Formula 

Verwendung so etwas wie:

ws.Range("A" & firstRow & ":BB" & firstRow).Copy ws.Range("A" & (firstRow + myLoop) & ":BB" & (firstRow + myLoop)) 
+0

Sorry, aber VBA ist neu für mich. Können Sie erklären?? – Chris

+0

@Chris Siehe meine ** EDIT # 1 ** –

1

Ich kann kein Lob dafür nehmen. Danke Dave.

Sub Loop_InsertRowsandFormulas() 

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Risk Input Sheet") 
Dim vRows As Long 
Dim lastCol As Long 
Dim firstRow As Long 

firstRow = InputBox("Enter Row To Start Insert From.") 
vRows = InputBox("Enter Number Of Rows Required") 

If firstRow = 0 Or vRows = 0 Then Exit Sub 
Debug.Print firstRow 
IngA = ws.Cells(5, ws.Columns.Count).End(xlToLeft).Column 
For Myloop = 1 To vRows 
    ws.Range("A" & (firstRow + Myloop)).EntireRow.Insert 
    ws.Range("N" & (firstRow) & ":AW" & (firstRow + Myloop)).FillDown 
Next 

End Sub 
+0

vielen Dank für das Posten des Updates. –