Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: How to have a macro run by itself

  1. #1

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    [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.

  3. #3
    Many thanks

    Does this mean I need to do this in each sheet

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    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]

  5. #5
    Thanks again will try this and let you know

  6. #6
    I get an error message saying he doesn't recognize the "Call Reformat " function

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    Forgot to mention, make it Public as well.

  8. #8
    Changed them both to Public so in "ThisWorkbook" and in "Module1", and now nothing happens

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    Post the workbook.

  10. #10
    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]

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    Can you post the whole workbook, I don't have the worksheets that you do.

  12. #12
    No quite sure what you mean ?
    All my print screens are to big to upload

    But I did post he whole code from both

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    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.

  14. #14

  15. #15
    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.

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    Look at OnTime in help, that does delayed execution.

  17. #17
    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]

  18. #18
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    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'

  19. #19
    I know, the problem was the size of my attachments

    Thanks anyway

  20. #20
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    You can post zip files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •