+ Reply to Thread
Results 1 to 4 of 4

Macro will not execute when called in a different sheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2014
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    7

    Post Macro will not execute when called in a different sheet.

    I have a column with dates. I have designed a macro that takes the last value in the column and puts the date of the next day in the cell below, and continues this way until the date of "today" is reached. The column is in "sheet1", "column H". If I create a control button in "sheet1" the code will execute quickly. However, I want the control button in "sheet2". When I do this, Excel takes forever to execute the code and eventually freezes.

    A spreadsheet showing an example of the problem is attached. The general code of the macro is

     Private Sub CommandButton1_Click()
    Dim Row As Long
    Dim lr As Variant
    lr = Worksheets("Sheet1").Cells(Rows.Count, "H").End(xlUp).Row
        Dim StartD As Date, EndD As Date
        StartD = Cells(lr, "H")
        EndD = Date
        For Row = 1 To (EndD - StartD)
        Application.ScreenUpdating = False
            Worksheets("Sheet1").Cells(lr + Row, "H") = StartD + Row
        Next Row
        Application.ScreenUpdating = True
    End Sub
    Any suggestions on how I can avoid this, but still have the control button in "sheet2"?
    Attached Files Attached Files
    Last edited by cudh; 08-05-2014 at 08:46 AM. Reason: spelling

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro want execute when called in a different sheet.

    lr = Worksheets("Sheet1").Cells(Rows.Count, "H").End(xlUp).Row

    Worksheets("Sheet1").Cells(lr + Row, "H") = StartD + Row

    There are the lines to change

    Rows.Count should be Worksheets("Sheet1").Rows.Count

    i think
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    08-01-2014
    Location
    Copenhagen, Denmark
    MS-Off Ver
    2013
    Posts
    7

    Re: Macro want execute when called in a different sheet.

    Thanks for the help! The first line above I changed. I didn't change the second line, but instead changed
    StartD = Cells(lr, "H")
    to
    StartD = Worksheets("Sheet1").Cells(lr, "H")
    .
    Now it works and the code looks like this

    Private Sub CommandButton1_Click()
    Dim Row As Long
    Dim lr As Variant
    lr = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "H").End(xlUp).Row
        Dim StartD As Date, EndD As Date
        StartD = Worksheets("Sheet1").Cells(lr, "H")
        EndD = Date
        For Row = 1 To (EndD - StartD)
        Application.ScreenUpdating = False
            Worksheets("Sheet1").Cells(lr + Row, "H") = StartD + Row
        Next Row
        Application.ScreenUpdating = True
    End Sub

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro will not execute when called in a different sheet.

    You need to take Sheet 1 out the lr row, otherwise sheet2's geometery will be based on Sheet1, so if no rows in SHeet1, nothing will happen in sheet 2

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Prevent macro being called from another sheet
    By RMQCON in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2014, 01:33 AM
  2. [SOLVED] When sheet is activated, execute macro in other sheet and return to sheet
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-16-2012, 10:50 AM
  3. Error When Macro is Called From Another Sheet
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2011, 08:29 AM
  4. Automatically execute macro on separate sheet
    By johnrlalor in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-08-2010, 09:17 AM
  5. Protected Sheet Allows Macro to Execute
    By theSizz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2010, 02:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1