2017-01-30 2 views
0

Hallo Jungs halten, so dies ist mein Code:VBA-Code Spalten über verschieben und Formel

Sub Biz1_Shift_OnePeriod() 
    'Shift all values one period to the left 

    'Message Box Question 
    Ans = MsgBox("Update data by one year?", vbYesNo + vbQuestion, "Data Update") 
    If Ans = vbNo Then Exit Sub 


    'Turn off screen updating & calculation to make code run faster 
    Application.ScreenUpdating = False 
    Application.Calculation = xlCalculationManual 

    Dim CopyFromWks As Worksheet 
    Dim CopyToWks As Worksheet 
    Dim j As Integer 
    Dim C As Range 


    '--------------------------------------------------------------------- 
    'Business - Balance Sheet 
    ' 
    ' 
    'Set the worksheet 
    Sheets("Balance Sheet").Select 
    Range("A2").Select 
    Set CopyToWks = Sheets("Balance Sheet") 
    Set CopyFromWks = Sheets("Balance Sheet") 
    ' 
    'Copy data loop from 2nd Historical to 3rd Historical 
    Set Copyfrom = CopyFromWks.Range("L:L") 
    Set Copyto = CopyToWks.Range("I:I") 
    For i = 1 To 1 
     For j = 1 To 95 
      For Each C In Copyfrom.Cells(j, i) 
       If C.Locked = False Then 
        Copyto(j, i).Value = Copyfrom(j, i).Value 
       End If 
      Next 
     Next 
    Next 
    ' 
    'Copy data loop from 1st Historical to 2nd Historical 
    Set Copyfrom = CopyFromWks.Range("O:O") 
    Set Copyto = CopyToWks.Range("L:L") 
    For i = 1 To 1 
     For j = 1 To 95 
      For Each C In Copyfrom.Cells(j, i) 
       If C.Locked = False Then 
        Copyto(j, i).Value = Copyfrom(j, i).Value 
       End If 
      Next 
     Next 
    Next 
    ' 
    'Set Historical Yr 1 to Zero 
    Set Copyto = CopyToWks.Range("O:O") 
    For i = 1 To 1 
     For j = 1 To 95 
      For Each C In Copyfrom.Cells(j, i) 
       If C.Locked = False Then 
        Copyto(j, i).Value = 0 
       End If 
      Next 
     Next 
    Next 
    ' 
    'Set Current equal to Zero 
    Set Copyto = CopyToWks.Range("R:R") 
    For i = 1 To 1 
     For j = 1 To 95 
      For Each C In Copyto.Cells(j, i) 
       If C.Locked = False Then 
        Copyto(j, i).Value = 0 
       End If 
      Next 
     Next 
    Next 

Was will ich meine Spalten tun verschieben über nach links. Ich dachte, eine Copy-Paste-Methode würde tun, und für jetzt habe ich die letzte Spalte auf 0 gesetzt. Allerdings brauche ich die letzte Spalte, um alle ihre Formeln zu behalten, aber nicht aus irgendeiner Datenquelle ziehen. Ich kam auf eine Idee, um eine andere Spalte zu erstellen, die versteckt werden würde und die ganze Formel dort speichern würde und diese verschieben würde, wenn das Makro ausgelöst wird. Ich wollte euch fragen, ob es einen besseren Weg gibt, um das zu tun und ein bisschen Brainstorming zu machen.

+0

Was wie Ihre Formeln sind? – user3598756

+0

@ user3598756 Meine Formeln sind wie folgt: = IFERROR (IF $ 4 = "1120S", INDEX (B1HY1! $ A $ 3: $ F $ 300, Match (20, B1HY1! $ F $ 3: $ F $ 300,0), 5) + INDEX (B1HY1! $ A $ 3: $ F $ 300, MATCH ("K 12a", B1HY1! $ F $ 3: $ F $ 300,0), 5), IF (P $ 4 = 1120, INDEX (B1HY1 ! $ A $ 3: $ F $ 300, MATCH (27, B1HY1! $ F $ 3: $ F $ 300,0), 5), IF (P $ 4 = 1065, INDEX (B1HY1! $ A $ 3: $ F $ 300, MATCH (21, B1HY1! $ F $ 3: $ F $ 300,0), 5), 0))), 0) –

+0

Haben Sie @MakPo Lösung versucht? – user3598756

Antwort

2

Versuchen

Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _ 
    SkipBlanks:=False, Transpose:=False 
Verwandte Themen