+ Reply to Thread
Results 1 to 7 of 7

Thread: Repeat section of a Macro after YES/NO question is asked.

  1. #1
    Registered User
    Join Date
    09-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    Repeat section of a Macro after YES/NO question is asked.

    Ok. I have a Macro that runs to basically add a new row to my spreadsheet keeping the formatting and the formulas only. Data gets deleted.
    It works perfectly. It has to un-share the workbook to run.

    I am basically wanting a question to be asked, after the section of the code that adds new row to see if the user wants another row adding. If they select yes another row gets added. The question will then be asked again until they select no. If they select No, then the macro carries on running and re-shares the workbook.

    That way i can have the spreadsheet re-shared at the end rather than expecting the user to do it, as the process currently is at present. The reason i want it to happen this way is if the workbook is re-shared after each row is added, it will take a considerable amount of time.

    This is the code i have already. Any ideas?

    HTML Code: 
    Sub AddNewRow()
    '
    ' AddNewRow Macro
    '
    answer = MsgBox("The file needs to be unshared. Has everyone else saved, and come out of this file?", vbYesNo)
    If answer = vbNo Then Exit Sub
    
    MsgBox "The workbook will now be unshared. Click OK to continue. This may take some time."
    
    Application.DisplayAlerts = False
    If ActiveWorkbook.MultiUserEditing Then
    ActiveWorkbook.ExclusiveAccess
    End If
    Application.DisplayAlerts = True
    
    MsgBox "Sucessfully unshared. Click OK to continue."
    
    
    '
        ActiveSheet.Unprotect
        Rows("2:2").Select
        Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Rows("3:3").Select
        Selection.Copy
        Rows("2:2").Select
        Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
            , SkipBlanks:=False, Transpose:=False
        Rows("2:2").Select
        Application.CutCopyMode = False
        Rows("2:2").Select
        Selection.SpecialCells(xlCellTypeConstants, 3).Select
        Selection.ClearContents
        Range("B2").Select
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables:= _
            True
        
        MsgBox "New row added. Please remember to re-share the workbook."
            
            
    End Sub
    Thanks in advance.
    Last edited by gonzalraf; 01-28-2012 at 01:51 PM. Reason: Additional Info

  2. #2
    Registered User
    Join Date
    09-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Repeat section of a Macro after YES/NO question is asked.

    P.S. I dont want the questions at the beginning to be asked again. I just want it to run the part of the macro that adds the row.

  3. #3
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: Repeat section of a Macro after YES/NO question is asked.

    Hi gonzalraf,

    This feels like a "GoTo Line Label" type of question. See http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx for an example or http://www.java2s.com/Code/VBA-Excel...ementworks.htm

    The idea is you do some test and then jump back to a Line Label above in your code to do stuff over again.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

  4. #4
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,998

    Re: Repeat section of a Macro after YES/NO question is asked.

    How about a logic like this?
    'pseudo code
    
    Sub theSub()
    
        Ask beginning questions
    
        UnShare
    
        Do
            Add Rows
        Loop While MsgBox("Do it again?", vbYesNo) = vbYes
    
        Re-share
    
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    09-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Repeat section of a Macro after YES/NO question is asked.

    Thank you. This solved it.

    Quote Originally Posted by MarvinP View Post
    Hi gonzalraf,

    This feels like a "GoTo Line Label" type of question. See http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx for an example or http://www.java2s.com/Code/VBA-Excel...ementworks.htm

    The idea is you do some test and then jump back to a Line Label above in your code to do stuff over again.

  6. #6
    Registered User
    Join Date
    09-16-2011
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Repeat section of a Macro after YES/NO question is asked.

    How do i mark as solved?

  7. #7
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: Repeat section of a Macro after YES/NO question is asked.

    You need to edit your original post and go to advanced and edit the prefix of the thread title to Solved.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0