Solved

Amend Code

Posted on 2013-11-21
2
209 Views
Last Modified: 2013-11-22
Hi,

I have a sub below that deals with sheets calculation D1 & D2 &d3 before it deals with  calculation variance d2 & d3

Can someone amend so it deals with calculation variance d2 & d3 first and then the rest

Many thanks
Seamus

--------------------







Sub PrepTool()

' Macro expands sheet for use with required number of records
' Be aware that many aspects of this macro are hard coded (e.g. the lengths of the lines to be copied down)
' and, as such, can be easily broken if the tool is amended


Dim Records As Long
Dim Selection As Integer
Dim x As Integer ' counter

On Error GoTo Finish

Selection = MsgBox("You are about to be asked how many records you will need in the tool. Be aware that choosing a large number of records will inflate the size of the tool markedly. You should save a new version of the tool before choosing to expand for your records as this cannot be undone.  Choose 'Cancel' to go back and save", vbOKCancel)

If Selection = vbCancel Then Exit Sub

Records = InputBox("How many records are you planning to input?", "Sample", 2)

Application.ScreenUpdating = False


'Turns off auto-calculate
Application.Calculation = xlCalculationManual


For x = 1 To 2

    With Sheets("Input - D" & x)
        .Activate
        .Unprotect
    End With
        Range("B8:DW8").AutoFill Range("B8:DW" & (Records + 7))
    ActiveSheet.Protect
    With Sheets("Calculation - D" & x)
        .Activate
        .Unprotect
    End With
        Range("A14:BJ14").AutoFill Range("A14:BJ" & (Records + 13))
    ActiveSheet.Protect

Next x

    With Sheets("Calculation - variation - D2")
        .Activate
        .Unprotect
    End With
        Range("A12:FE12").AutoFill Range("A12:FE" & (Records + 11))
    ActiveSheet.Protect

    With Sheets("Input - D3")
        .Activate
        .Unprotect
    End With
        Range("B8:DW8").AutoFill Range("B8:DW107")
    ActiveSheet.Protect
   
    With Sheets("Calculation - D3")
        .Activate
        .Unprotect
    End With
        Range("A14:BJ14").AutoFill Range("A14:BJ113")
    ActiveSheet.Protect
   
    With Sheets("Calculation - variation - D3")
        .Activate
        .Unprotect
    End With
        Range("A12:FE12").AutoFill Range("A12:FE111")
    ActiveSheet.Protect


Finish:

Sheets("Instructions for use").Activate

Application.ScreenUpdating = True

MsgBox "Your Tool is now ready for use", vbOKOnly, "Ready"

End Sub
0
Comment
Question by:Seamus2626
2 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39666942
Try this

Sub PrepTool()

' Macro expands sheet for use with required number of records
' Be aware that many aspects of this macro are hard coded (e.g. the lengths of the lines to be copied down)
' and, as such, can be easily broken if the tool is amended


Dim Records As Long
Dim Selection As Integer
Dim x As Integer ' counter

On Error GoTo Finish

Selection = MsgBox("You are about to be asked how many records you will need in the tool. Be aware that choosing a large number of records will inflate the size of the tool markedly. You should save a new version of the tool before choosing to expand for your records as this cannot be undone.  Choose 'Cancel' to go back and save", vbOKCancel)

If Selection = vbCancel Then Exit Sub

Records = InputBox("How many records are you planning to input?", "Sample", 2)

Application.ScreenUpdating = False


'Turns off auto-calculate
Application.Calculation = xlCalculationManual

    With Sheets("Calculation - variation - D2")
        .Activate
        .Unprotect
    End With
        Range("A12:FE12").AutoFill Range("A12:FE" & (Records + 11))
    ActiveSheet.Protect

    With Sheets("Calculation - variation - D3")
        .Activate
        .Unprotect
    End With
        Range("A12:FE12").AutoFill Range("A12:FE111")
    ActiveSheet.Protect

For x = 1 To 2

    With Sheets("Input - D" & x)
        .Activate
        .Unprotect
    End With
        Range("B8:DW8").AutoFill Range("B8:DW" & (Records + 7))
    ActiveSheet.Protect
    With Sheets("Calculation - D" & x)
        .Activate
        .Unprotect
    End With
        Range("A14:BJ14").AutoFill Range("A14:BJ" & (Records + 13))
    ActiveSheet.Protect

Next x

    With Sheets("Input - D3")
        .Activate
        .Unprotect
    End With
        Range("B8:DW8").AutoFill Range("B8:DW107")
    ActiveSheet.Protect
   
    With Sheets("Calculation - D3")
        .Activate
        .Unprotect
    End With
        Range("A14:BJ14").AutoFill Range("A14:BJ113")
    ActiveSheet.Protect
   


Finish:

Sheets("Instructions for use").Activate

Application.ScreenUpdating = True

MsgBox "Your Tool is now ready for use", vbOKOnly, "Ready"

End Sub

Open in new window

0
 

Author Closing Comment

by:Seamus2626
ID: 39668334
Thanks ssaqibh!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question