+ Reply to Thread
Results 1 to 6 of 6

[VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheets.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-11-2016
    Location
    USA
    MS-Off Ver
    2010
    Posts
    19

    [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheets.

    I'm usually pretty adept at Excel but god help me I cannot figure this one out. Here is my macro "Magic":

    http://pastebin.com/kQs4vQAU

    But I need help. What I need to do is make another macro that will:

    0. Delete sheets "Sheet2" and "Sheet3"

    1. Look in column "A" for the word "To:"

    1. Look to the right of that cell (it'll be in column B) and compare THAT text value to the next instance of "To:"'s adjacent cell. If they do NOT match, go to initial "To:"'s section and copy all rows between blank row BEFORE the cell that contains "AGENT STATEMENT" all the way down from "To:" up to and including the row that contains a cell (located) in "A" which starts with "Thank you for choosing" (there may be some other text after the word choosing). Then paste that in a new worksheet. If it DOES match, however, continue on down to the next "To:" in column "A" until it doesn't find a match. When that happens go to initial "To:"'s section and copy all rows between blank row BEFORE the cell that contains "AGENT STATEMENT" all the way down from "To:" up to and including the row that contains the last cell (located) in "A" which starts with "Thank you for choosing" that fell in the matched section (there may be some other text after the word choosing). Then paste it into a new worksheet.

    4. Repeat above step until there is no longer a "To:" to look for.

    5. In each of these worksheets copy all of the text ABOVE the first cell that contains "Order" and PASTE it into cell K1.

    3. Finally, delete "Sheet1".

    Also, in the pastebin how can I update "Worksheets("Sheet1").Columns("A:Z").AutoFit" so that it applies to the sheet it's currently working on as it loops through all the sheets using above?

  2. #2
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheet

    Are you still wanting help with this?
    If so, can you please reply and attach your file (desensitized). Attach by clicking on Go Advanced (next to Post Quick Reply) and look for paperclip icon.
    Also paste your code into your reply, wrapping code tags around it ( see above # icon)

  3. #3
    Registered User
    Join Date
    01-11-2016
    Location
    USA
    MS-Off Ver
    2010
    Posts
    19

    Re: [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheet

    Quote Originally Posted by Kevin# View Post
    Are you still wanting help with this?
    If so, can you please reply and attach your file (desensitized). Attach by clicking on Go Advanced (next to Post Quick Reply) and look for paperclip icon.
    Also paste your code into your reply, wrapping code tags around it ( see above # icon)
    Yes please! I attached it here. Honestly I don't really need anything from my original macro EXCEPT for calling it on each sheet (so right after Step 5). My macro is called "Magic". Thanks!

    I made some slight modification in the attachment because I realized in my workbook the term "Thank you for choosing Company!" doesn't necessarily always show up in column A. Is it possible to look across all cells in a row?
    Attached Files Attached Files

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheet

    Could you post the code here rather than an pastebin?
    If posting code please use code tags, see here.

  5. #5
    Valued Forum Contributor
    Join Date
    01-03-2016
    Location
    Conwy, Wales
    MS-Off Ver
    2016
    Posts
    974

    Re: [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheet

    See if this is close to what you are looking for
    Still using sheet2 - so have not deleted
    Run macro with CTRL + t


    Sub IndividualStatements()
    Dim ws As Worksheet, wt As Worksheet
    Dim lR As Long
    Dim myStr As String
    Dim cellRowA As Long, cellRowB As Long
    Set ws = Sheets("Sheet1")
    Set wt = Sheets("Sheet2")
    
    lR = ws.Range("A1048576").End(xlUp).Row
    With wt
        .Cells.Clear  'clear prior values
    'create table of ranges to copy from sheet1 including first& last rows
        s = 1
        For r = 1 To lR
            If ws.Cells(r, 1).Value = "AGENT STATEMENT" Then
                s = s + 1
                .Cells(s, 2) = r - 1
                .Cells(s - 1, 3) = r - 2
                    If s = 2 Then .Cells(s - 1, 3) = ""
            End If
        
            If ws.Cells(r, 1).Value = "To:" Then
                ws.Cells(r, 2).Copy Destination:=.Cells(s, 1)
            End If
                If r = lR Then .Cells(s, 3) = r
        Next r
            lR = .Range("A1048576").End(xlUp).Row
            For s = lR To 2 Step -1
                If .Cells(s, 1) = .Cells(s - 1, 1) Then
                    .Cells(s - 1, 3) = .Cells(s, 3)
                    .Rows(s).EntireRow.Delete
                End If
            Next s
        .Cells(1, 2).Value = "From row"
        .Cells(1, 3).Value = "To row"
        
       lR = .Range("A1048576").End(xlUp).Row
        For s = 2 To lR
            cellRowA = .Cells(s, 2).Value
            cellRowB = .Cells(s, 3).Value
            
    'create worksheets for each customer
            On Error Resume Next
            Application.DisplayAlerts = False
            Sheets(.Cells(s, 1).Value).Delete
            Sheets("sheet3").Delete
            Application.DisplayAlerts = True
            Worksheets.Add.Name = .Cells(s, 1).Value
            Cells.Clear
            Set myrange = ws.Range("A" & cellRowA & ":G" & cellRowB)
            myrange.Copy
            Cells(1, 1).PasteSpecial
            
        Next s
    End With
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-11-2016
    Location
    USA
    MS-Off Ver
    2010
    Posts
    19

    Re: [VBA] Tricky request! Moving sheets, calling macro within macro, applying to all sheet

    Looks really good! I may ask for some slight additional tweaks after I do some thorough testing. Really appreciate this help so, so much.

+ 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. Mergring two sheets information together using a search word by applying macro
    By nekken22 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-20-2015, 07:30 PM
  2. [SOLVED] Tricky: Macro to compare cells of two sheets, no exact matches though
    By elgato74 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-16-2014, 08:19 AM
  3. Loop Macro + Very Hidden Sheets = Tricky?
    By lilanngel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-07-2014, 03:38 AM
  4. Quick Question about applying macro to multiple sheets
    By sale_MB in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-18-2013, 10:25 AM
  5. Applying a macro to multiple sheets
    By swallis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-15-2011, 01:36 AM
  6. Applying extra criteria when using macro to password protect sheets
    By Zyphon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-17-2007, 11:03 AM
  7. Applying Macro to only certain sheets
    By Darin Kramer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-13-2005, 01:05 PM

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