Re: Hide all sheets - but remain on current sheet
Fastactic!
thank you very much
Re: Hide all sheets - but remain on current sheet
Fastactic!
thank you very much
Re: Hide all sheets - but remain on current sheet
Thank you for the quick reply cytop,
But I have several sheets I will be keeping unhidden, so hiding all but the active sheet isn't the solution.
cheers
Hi guys,
A pretty simple one here..
I have the code to hide sheets, but I want to be able to remain on the sheet where the macro button was pressed:
Sub HideAll()
'
' Hide_data_sheets Macro
'
'
Sheets(Array("FTSE All Share", "Int. Risk (12)", "All Risk (12)", "Low Risk (6)", _
"High Risk (6)", "A. Tesco", "B. Sainsbury", "C. Morrisons", "D. Marks and Spencer" _
, "E. Booker Group", "F. Thorntons", "G. HSBC", "H. LLOYDS", "I. STAND", "J. BARC", _
"K. EIIB", "L. RBS", "M. Kroger", "N. Wal-Mart", "O. Target", "P. Bank of America", _
"Q. Maxvalu Tokai", "R. Kansai", "S. Albis", "T. Bank of Japan")).Select
Sheets("T. Bank of Japan").Activate
ActiveWindow.SelectedSheets.Visible = False
End Sub
Display More
The same applies for my Unhide all
Sub UnhideAll()
Dim wsSheet As Worksheet
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Visible = xlSheetVisible
Next wsSheet
End Sub
Thank you
Re: Copy cells from one workbook to current workbook (the one running the macro)
simple. Many thanks 5nizza!
This is a simple one, but ive been searching forums for ages (maybe i've been searching the wrong thing)
The workbook I am copying from is fine to access.
It has already been defined as a workbook that is now called wbopen
for example
Dim wbopen As Workbook
set wbopen = workbooks.open('cannot post files actual location)
With wbopen.Sheets("sheet1")
Range("a5").Copy
However, I just cant seem to figure out how I can use my current workbook to paste back into.
surely its simple?
something like activeworkbook.name?
I should stress that the workbook i want to paste into is already open (as it was used to run the macro)
For ease of explaining, lets say I want to copy cell "A5" from wbopen and paste it into "A1" in my current work book that im running the macro on
Many thanks in advance
Re: userform that returns specific cell
Sorry maybe I shouldnt of posted that first bit of code, I just wanted a way to replace "D3" for example, with a user input.
However...you have helped me solve my problem (I wasnt using text after textbox1)
Thank you for the help and quick reply
I have created a userform (but I dont fully understand them so no point posting my userform code)
The userform has two input boxes. Because of the lack of code I will call them textbox1 and textbox2.
textbox1 is for the user to enter the column letter
textbox2 is to enter the row number
I should also mention the userform has two buttons, "OK" and "Cancel". My "Cancel" button works and exits the userform/sub.
Again for the lack of knowledge I will refer to these buttons as Commandbutton1 and Commandbutton2
My overall aim is that the following code takes a range reference according to what has been entered in the userform.
-------------rough example---------------
'autofill to last row
LastRow = Cells(Rows.Count, "q").End(xlUp).Row
Range[B]("D3").[/B]AutoFill Destination:=Range[B]("D3:D"[/B] & LastRow)
'copy and paste special values
ActiveSheet.Calculate
Range([B]"D3:D"[/B] & LastRow).Copy
ActiveSheet.Range[B]("D3").[/B]PasteSpecial xlPasteValues
For example...I want to create something like
dim inputcolumn as integer
dim inputrow as integer
inputcolum = textbox1
inputrow = textbox2
range("inputcolumn" & "inputrow").value........etc (as above code)
So basically whenever theres a reference in my code to a cells column or row (in example, "D3") it takes the value from the userform box.
I know the question is quite messy/vague.
Can anybody help me?
Many thanks in advance
Re: Execute macro only on visible cells after filtering
Many thanks for both responses, has helped my understanding of special cells method a lot!!
FYI
After much debating and taking into account the helpful responses. I have re-designed my code to the following:
Sub mduration_paste_special()
'
' test Macro
Dim LastRow As Long
Dim t
'user message box
msgbox "Caution: Screen may appear to freeze." & vbCrLf & vbCrLf & "Please do not close excel.", vbInformation, "Please Wait..."
t = Timer
Application.DisplayAlerts = False
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
Range("AD3").Select
'apply formula
ActiveCell.FormulaR1C1 = _
"=IF(OR(RC17=""Bond"",RC17=""Non-concerned bond"",RC17=""Non-EEA gvt"")=TRUE,IF(or((rc11=0),(RC29=0)),0,IF(RC28="""",MDURATION(DATEVALUE(""31/12/2009""),ROUNDUP((DATEVALUE(""31/12/2009"")+(RC29*365)),0),RC36%,RC36%/(RC11/RC5),1),MDURATION(DATEVALUE(""31/12/2009""),RC28,RC36%,(RC36%/(RC11/RC5)),1))),""not applicable"")"
'autofill to last row
LastRow = Cells(Rows.Count, "q").End(xlUp).Row
Range("AD3").AutoFill Destination:=Range("AD3:AD" & LastRow)
'copy and paste special
ActiveSheet.Calculate
Range("AD3:AD" & LastRow).Copy
ActiveSheet.Range("AD3").PasteSpecial xlPasteValues
Application.DisplayAlerts = True
Application.Calculation = xlCalculateautomatic
Application.ScreenUpdating = True
'display elapsed time
msgbox "MDuration Complete." & vbCrLf & vbCrLf & "Elapsed time: " & Format((Round(Timer - t, 2)), " 00") & "seconds", vbInformation
End Sub
Display More
As most can see it pastes the formula into the first cell in the column.
Autofills that formula to the last row.
Copys the cells and then paste special the values back.
displays a "elapsed time" box
COMPLETION TIME: 20sec approx (for over 130,000 rows)
Once again many thanks for all your posts, they have been a great help to me!
Re: Execute macro only on visible cells after filtering
ahhh. In which case special cells isn't an option. Thank you both for the input and heads up
:thanx:
could you change the thread title to include (FYI)
I've been stuck on this one for ages...
I have alot of data, I want to apply a macro to only the relevent rows to save calculation speed. Simple.
I have a working autofilter so that parts not the issue
Through my extensive (banging my head aginst the computer) research, It seems the best way to do this is to name a range for the filtered data and apply the code.
I want the range to be variable, from A3-AK"last row of data" (Endx1up NOT down)
the output will be in column AD
'---------Code that has already auto filtered ----------
'Want: code that selects and names the range
'Want: a way of applying the code i already have to these cells
`--------- my code ------------
So in summary, I need (please):
1.A way to name a variable range
2.A way to apply my code to this variable range
3.Results in colunm AD (but this one im sure i can figure out for myself)
THANK YOU!!!!!!!!
Re: Speed up slow calculation in vba
UPDATED CODE
Sub calculate_modified_duration()
Dim modified_duration As Variant
Dim yield As Single
Dim coupon As Single
Dim nav As Single
Dim nominal As Single
Dim mdate As Date
Dim mdateround As Date
Dim asset As String
Dim maturity As Double
Dim modified_duration_round As Variant
Dim t
t = Timer
'to speed up sheet calculation: mdowsett 04/11/2010
Application.DisplayAlerts = False
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
'change to correct column (Mduration) and first row: mdowsett 04/11/2010
Range("ad3").Select
Do
'Only bonds etc remaing.
'NOTE capital B and non capital b on "Non-concerned Bonds" both need to be accounted for: mdowsett 05/11/2010
If ActiveCell.Offset(0, -13).Value <> "Bond" And ActiveCell.Offset(0, -13).Value <> "Non-concerned Bond" And ActiveCell.Offset(0, -13).Value <> "Non-EEA gvt" And ActiveCell.Offset(0, -13).Value <> "Non-concerned bond" Then
ActiveCell.Value = ""
'if nav has no value: mdowsett 09/11/2010
ElseIf ActiveCell.Offset(0, -19).Value = 0 Then ActiveCell.Value = "not applicable"
'if term to maturity has no value: mdowsett 09/11/2010
ElseIf ActiveCell.Offset(0, -1).Value = 0 Then ActiveCell.Value = 0
'if maturity date has no value: mdowsett 05/11/2010
ElseIf ActiveCell.Offset(0, -2).Value = "" Then
'calculation for parts of mduration: mdowsett 04/11/2010
mdateround = Round((40178 + (ActiveCell.Offset(0, -1).Value * 365)), 0)
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'muduration with a missing maturity date: 05/11/2010
modified_duration_round = Application.WorksheetFunction.mduration("12/31/2009", mdateround, coupon, yield, 1, 1)
ActiveCell.Value = modified_duration_round
ElseIf ActiveCell.Offset(0, -2).Value <> "" Then
maturity = ActiveCell.Offset(0, -1).Value
mdate = ActiveCell.Offset(0, -2).Value
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'mduration with maturity date: 05/11/2010
modified_duration = Application.WorksheetFunction.mduration("12/31/2009", mdate, coupon, yield, 1, 1)
ActiveCell.Value = modified_duration
End If
ActiveCell.Offset(1, 0).Select
'until asset type has no value: 05/11/2010
Loop Until ActiveCell.Offset(0, -13).Value = 0
'turn calculations back on: mdowsett 04/11/2010
Application.Calculation = xlCalculateAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True
msgbox "MDuration Complete. Elapsed time " & Format((Round(Timer - t, 2) / 60), "00.00") & "mins"
End Sub
Display More
Re: Speed up slow calculation in vba
Hi Rob,
Basically I wanted to find a way to calculate mduration quickly for over 130,000 cells, although it takes a long time (at the moment). It has to run off a button so that other users (who are not familular with the mduration calculation) can easily calculate it, or be it have a long coffee break while it is.
Im looking into autofiltering so that it can run faster and applying the suggestions from "AAE", but for me it's a slow process as i'm very new to vba.
Thank you for taking the time to help (I have changed the names as suggested, many thanks)
Re: Speed up slow calculation in vba
Thank you both for your quick replies and help!
cytop:
I have changed the coding as you've said and am now looking into With, End With...so thank you!!
AAE:
It has to run using code. Im looking into special cells and filtering. So thank you aswell!!
If I manage to reduce the time i will update the post so you can (hopefully) approve.
Once again thank you both for putting the time in to help!
Re: Speed up mduraiton calculation in vba
Sorry i posted a messy code.............here is a neater version with very slight (but irrevent) bits taken out.................
Sub mduration()
Dim mduration As Variant
Dim yield As Single
Dim coupon As Single
Dim nav As Single
Dim nominal As Single
Dim mdate As Date
Dim mdateround As Date
Dim asset As String
Dim maturity As Double
Dim mdurationround As Variant
'to speed up sheet calculation: mdowsett 04/11/2010
Application.DisplayAlerts = False
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
'change to correct column (Mduration) and first row: mdowsett 04/11/2010
Range("ad3").Select
Do
'Only bonds etc remaing.
'NOTE capital B and non capital b on "Non-concerned Bonds" both need to be accounted for: mdowsett 05/11/2010
If ActiveCell.Offset(0, -13).Value <> "Bond" And ActiveCell.Offset(0, -13).Value <> "Non-concerned Bond" And ActiveCell.Offset(0, -13).Value <> "Non-EEA gvt" And ActiveCell.Offset(0, -13).Value <> "Non-concerned bond" Then
ActiveCell.Value = ""
'if nav has no value: mdowsett 09/11/2010
ElseIf ActiveCell.Offset(0, -19).Value = 0 Then ActiveCell.Value = "not applicable"
'if term to maturity has no value: mdowsett 09/11/2010
ElseIf ActiveCell.Offset(0, -1).Value = 0 Then ActiveCell.Value = 0
'if maturity date has no value: mdowsett 05/11/2010
ElseIf ActiveCell.Offset(0, -2).Value = "" Then
'calculation for parts of mduration: mdowsett 04/11/2010
maturity = ActiveCell.Offset(0, -1).Value
mdateround = Round((40178 + (maturity * 365)), 0)
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'muduration with a missing maturity date: 05/11/2010
mdurationround = Application.WorksheetFunction.mduration("12/31/2009", mdateround, coupon, yield, 1, 1)
ActiveCell.Value = mdurationround
ElseIf ActiveCell.Offset(0, -2).Value <> "" Then
maturity = ActiveCell.Offset(0, -1).Value
mdate = ActiveCell.Offset(0, -2).Value
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'mduration with maturity date: 05/11/2010
mduration = Application.WorksheetFunction.mduration("12/31/2009", mdate, coupon, yield, 1, 1)
ActiveCell.Value = mduration
End If
ActiveCell.Offset(1, 0).Select
'until asset type has no value: 05/11/2010
Loop Until ActiveCell.Offset(0, -13).Value = 0
'turn calculations back on: mdowsett 04/11/2010
Application.Calculation = xlCalculateAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True
msgbox "Completed", 0, "MDuration"
End Sub
Display More
This is my first code ive written in vba so I apologise in advance...
My problem is this...
I have created a macro that calculates mduration, it take the relevent inputs and plugs it into the macro version of mduration (a function). The macro runs fine all the way through. However, it takes about 13mins to complete!!!
I have tried everything that I could think of but alas my knowledge of vba is not good enough.
My question is:
Can anyone help me find a way to reduce this speed, in an ideal world to under a minute.
IMPORTANT THINGS TO BE AWARE OF:
- The this macro runs on 130,000 rows!
- The only way i could think to create this macro is with a loop (suggestions welcome)
I have used alot of "offset" in my coding so the actual columns are the following;
AD = where i want the results to appear
AK = coupon
K = NAV
E = Nominal
AB = Mdate
Q = asset type
AC = maturity
Once again this is my first code and first post so its not perfect.
Thank you!!!!
Sub mduration()
Dim mduration As Variant
Dim yield As Single
Dim coupon As Single
Dim nav As Single
Dim nominal As Single
Dim mdate As Date
Dim mdateround As Date
Dim asset As String
Dim maturity As Double
Dim mdurationround As Variant
'to speed up sheet calculation: mdowsett 04/11/2010
Application.DisplayAlerts = False
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
'change to correct column (Mduration) and first row: mdowsett 04/11/2010
Range("ad3").Select
Do
'Only bonds etc remaing: mdowsett 05/11/2010
If ActiveCell.Offset(0, -13).Value <> "Bond" And ActiveCell.Offset(0, -13).Value <> "Non-concerned Bond" And ActiveCell.Offset(0, -13).Value <> "Non-EEA gvt" And ActiveCell.Offset(0, -13).Value <> "Non-concerned bond" Then
ActiveCell.Value = ""
'if nav has no value
ElseIf ActiveCell.Offset(0, -19).Value = 0 Then ActiveCell.Value = "not applicable"
'if term to maturity has no value
ElseIf ActiveCell.Offset(0, -1).Value = 0 Then ActiveCell.Value = 0
'if maturity date has no value: 05/11/2010
ElseIf ActiveCell.Offset(0, -2).Value = "" Then
'calculation for parts of mduration: mdowsett 04/11/2010
maturity = ActiveCell.Offset(0, -1).Value
mdateround = Round((40178 + (maturity * 365)), 0)
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'muduration with a missing maturity date: 05/11/2010
mdurationround = Application.WorksheetFunction.mduration("12/31/2009", mdateround, coupon, yield, 1, 1)
ActiveCell.Value = mdurationround
ElseIf ActiveCell.Offset(0, -2).Value <> "" Then
maturity = ActiveCell.Offset(0, -1).Value
mdate = ActiveCell.Offset(0, -2).Value
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'mduration with maturity date: 05/11/2010
mduration = Application.WorksheetFunction.mduration("12/31/2009", mdate, coupon, yield, 1, 1)
ActiveCell.Value = mduration
End If
ActiveCell.Offset(1, 0).Select
'until asset type has no value: 05/11/2010
Loop Until ActiveCell.Offset(0, -13).Value = 0
'turn calculations back on: mdowsett 04/11/2010
Application.Calculation = xlCalculateAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True
msgbox "Completed", 0, "MDuration"
End Sub
Display More