-
How to have a macro run by itself
I have created
[VBA]Public Function Reformat()
Sheets("Bonds").Select
Range("A4:T30").Select
Selection.Copy
Sheets("Upload Bonds").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Currency").Select
Cells.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Upload Currency").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Function[/VBA]
I would like this macro to run automatically as soon as a value has been updated in the original sheets or all the time as long as no button needs to be added
Many thanks in advance
-
[vba]
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "H10" '<== change to suit
On Error GoTo ws_exit
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
Call Reformat
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub
Private Function Reformat()
Worksheets("Bonds").Range("A4:T30").Copy
Sheets("Upload Bonds").Select
Range("A2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
Sheets("Currency").Cells.Copy
Sheets("Upload Currency").Select
Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
End Function
[/vba]
This is worksheet event code, which means that it needs to be
placed in the appropriate worksheet code module, not a standard
code module. To do this, right-click on the sheet tab, select
the View Code option from the menu, and paste the code in.
-
Many thanks
Does this mean I need to do this in each sheet
-
Put the Reformat code in a general code module and add this to ThisWorkbook
[vba]
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Const WS_RANGE As String = "H10" '<== change to suit
On Error GoTo ws_exit
Application.EnableEvents = False
If Not Intersect(Target, Sh.Range(WS_RANGE)) Is Nothing Then
With Target
Call Reformat
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub
[/vba]
-
Thanks again will try this and let you know
-
I get an error message saying he doesn't recognize the "Call Reformat " function
-
Forgot to mention, make it Public as well.
-
Changed them both to Public so in "ThisWorkbook" and in "Module1", and now nothing happens
-
-
This is in "ThisWorkbook"
[VBA]Public Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Const WS_RANGE As String = "A4" '<== change to suit
On Error GoTo ws_exit
Application.EnableEvents = False
If Not Intersect(Target, Sh.Range(WS_RANGE)) Is Nothing Then
With Target
Call Reformat
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub[/VBA]
And this in "Module 1"
[VBA]Public Function Reformat()
Worksheets("Bonds").Range("A4:T30").Copy
Sheets("Upload Bonds").Select
Range("A2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
Sheets("Currency").Cells.Copy
Sheets("Upload Currency").Select
Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
End Function
[/VBA]
-
Can you post the whole workbook, I don't have the worksheets that you do.
-
No quite sure what you mean ?
All my print screens are to big to upload
But I did post he whole code from both
-
I won't be able to see the problem because when I install it will work correctly for me. It is probably erroring in Reformat, but I cannot see that, or tell why, from a distance.
-
-
OK Sorry it works now, my fault didn't adjust the range as I should have
My "original" sheets are formulas whih are updated when these are updated the macro doesn't react
Last edited by Neodubois; 04-12-2007 at 04:44 AM.
-
Look at OnTime in help, that does delayed execution.
-
thanks it is working fine now.
I have the following macro which should also run by itself when the document is opened, do I also just paste it it "ThisWorkbook"?
[VBA]Private Sub Workbook_Open()
Workbooks.Add
With Application
.Calculation = xlManual
.MaxChange = 0.001
.CalculateBeforeSave = False
End With
ActiveWorkbook.PrecisionAsDisplayed = False
Application.AskToUpdateLinks = False
End Sub[/VBA]
-
FYI You can post a workbook using Manage Attachments in the Go Advanced section.
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
-
I know, the problem was the size of my attachments
Thanks anyway
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules