• enhance macro

    Author
    Topic
    #498348

    Greetings,

    I have the following macro to delete unwanted columns, rename etc. to the attached Excel file:

    Code:
    Sub Delete_Rename_Columns()
    'Make Sure We Are In The Correct Workbook
    On Error GoTo noSheetName:
    Set ws = Sheets("Order Report")
    Set ws = Sheets("Receiving Report")
    On Error GoTo 0
    'Make Sure The Code Hasn't Already Been Run
    If Sheets("Receiving Report").Range("D1") = "Supplier" Then
    MsgBox "It Appears That This Workbook Has Already Been Modified"
    Exit Sub
    End If
    ' *** Order Report ***
    With Sheets("Order Report")
    'Delete Any Unwanted Columns In A:Z Range
    .Range("B1:D1,G1,I1:K1,M1:W1,Y1").EntireColumn.Delete
    'Delete All Columns Beyond Column G
    .Range(.Cells(1, "H"), .Cells(1, .Columns.Count)).EntireColumn.Delete
    End With
    ' *** Receiving Report ***
    With Sheets("Receiving Report")
    'Delete Any Unwanted Columns In A:AA Range
    .Range("B1:K1,M1,O1:P1,R1:W1,Y1:Z1").EntireColumn.Delete
    'Delete Any Columns Beyong Column G
    .Range(.Cells(1, "G"), .Cells(1, .Columns.Count)).EntireColumn.Delete
    'Rename Columns
    .Range("A1") = "Order Date"
    .Range("D1") = "Supplier"
    End With
    Exit Sub
    noSheetName:
    MsgBox "This Does Not Appear To Be The Correct Workbook." & _
    vbCrLf & vbCrLf & _
    " Required Reports Not Found."
    End Sub
    

    I’d like to know if this macro can be enhanced and/or another macro can be created to further automate the following manual process.

    After the macro ran, following are steps taken by end user to complete the monthly report process

    Order Report

      [*]Apply sort by column C: (Order Type) first and then by column A (Order Number)
      [*]Delete STANDARD_RELEASE records from (Order Type) column
      [*]Sort by column A: (Order Number)
      [*]Sum ‘Distribution Amount’ on the basis of (Order Number)

    Receiving report

      [*]Delete blank records in the (Account Code) column
      [*]Apply the same steps as the Order report, except do not omit STANDARD_RELEASE records in the (Order Type) column

    Create a new worksheet

      [*]Copy the Order Report
      [*]Copy Receiving Report beneath it & highlighted it in red (to differentiate)
      [*]Sort by column A: (Order Number)
      [*]Visually check to see what order is received (where distribution amounts are same)

    TIA,
    Regards,

    Viewing 14 reply threads
    Author
    Replies
    • #1486653

      Hi OCM

      If you save an excel file as “.xlsx”, then all vba is ‘removed’.
      Can you post your file as a “.xlsm” file, so that the vba is retained?

      Now, if I ‘manually’ add your code to the .xlsx sample file you attached (and then rename as a .xlsm file), you macro will give the ‘not correct workbook’ message because your sheetnames are different.

      How is your macro executed? Do you use a button? Are there other workbooks open etc??

      I’m sure we could help you, but we need a little more info.

      zeddy

    • #1486706

      Thanks Zeddy,

      If you save an excel file as “.xlsx”, then all vba is ‘removed’.
      Can you post your file as a “.xlsm” file, so that the vba is retained?
      –> As per your suggestion I saved the attached file as macro enable workbook.

      Now, if I ‘manually’ add your code to the .xlsx sample file you attached (and then rename as a .xlsm file), you macro will give the ‘not correct workbook’ message because your sheet names are different.
      –> I modified the sheet names the same way as in the macro. But, you brought up a good point. The
      sheet names will change every month (e.g. Order report 0115, Order report 0215 etc.) what is the best
      way to make this work?

      How is your macro executed? Do you use a button? Are there other workbooks open etc??
      –>Currently I run the macro manually, but eventually my goal is to create a button
      I’m sure we could help you, but we need a little more info.

      Regards,

      • #1486806

        This code (insert a REGULAR module and put it there) should do what you want for sheet names and culling columns/rows. However, you don’t say where you want these subtotals, sheet 2 doesn’t have order numbers,
        Assumes sheets sorted as shown in example

        Sub FixWorksheetsSAS()
        ‘Make Sure We Are In The Correct Workbook
        If Left(Sheet1.Name, 5) “Order” Then Exit Sub
        ‘check if already done
        If Sheet2.Range(“D1”) = “Supplier” Then
        MsgBox “It Appears That This Workbook Has Already Been Modified”
        Exit Sub
        End If
        ‘ *** Order Report ***
        With Sheet1
        ‘Delete Any Unwanted Columns In A:Z Range
        .Range(“B1:D1,G1,I1:K1,M1:W1,Y1”).EntireColumn.Delete
        ‘Delete All Columns Beyond Column G
        .Range(.Cells(1, “H”), .Cells(1, .Columns.Count)).EntireColumn.Delete

        ‘added to delete rows
        With .UsedRange
        .AutoFilter Field:=3, Criteria1:=”STANDARD_RELEASE”
        .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
        End With

        End With

        ‘ *** Receiving Report ***
        With Sheet2 ‘(“Receiving Report”)
        ‘Delete Any Unwanted Columns In A:AA Range
        .Range(“B1:K1,M1,O1:P1,R1:W1,Y1:Z1”).EntireColumn.Delete
        ‘Delete Any Columns Beyong Column G
        .Range(.Cells(1, “G”), .Cells(1, .Columns.Count)).EntireColumn.Delete
        ‘Rename Columns
        .Range(“A1”) = “Order Date”
        .Range(“D1”) = “Supplier”
        ‘added
        With .UsedRange
        .AutoFilter Field:=3, Criteria1:=””
        .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
        End With
        End With
        End Sub

        • #1486824

          Hi Don

          re: “sheet 2 doesn’t have order numbers”

          ..it’s in column [L] (before processing), and column (after processing)

          zeddy

          • #1486844

            OK. I see that now but hesitate to go any further if the OP is NOT coming back. We really need to see an AFTER example on sheet 3

    • #1486858

      Thank you both.
      Dan, I’ll try your code, but to answer your questions,

      1. However, you don’t say where you want these subtotals —> in a separate sheet would be great.
      2. sheet 2 doesn’t have order numbers —>order number in the receiving report (2nd report) is on column L. I think you are aware of this…
      3. Assumes sheets sorted as shown in example  No, my original sheets are not sorted.

      Regards,

      • #1486861

        As requested, post a before/after example showing both sheets before and one after sheet in the same fiie.

    • #1486895

      Hi Don,

      Attached is the sample, let me know if this is what you were looking for.
      I tried your code and got ‘Object required’ error

      TIA,

      Regards

    • #1487102

      Don,

      Thank you, I’ll try this tomorrow in my actual file, but it works in the sample workbook as planned.

      The only thing I noticed is blank records in the account code column for order numbers (51258, 51274, 51303, 51304).

      I think this is because we need to delete blank ‘account code’ values in the receiving report.

      This is on my original post where I listed the manual process steps for receiving report.

      Once we fix this and hit the Click Here button, I think everything in accounting code column should be populated.

      TIA

      Regards,

    • #1487242

      OK. Do you want it as in YOUR example or in MINE without the blanks.

    • #1487336

      Hi Don,

      In both, if possible. If I have to choose one, I like your example w/o blanks.

      TIA,

      Regards

    • #1487394

      OK. See attached.
      BTW, You don’t say how the sheets are populated. I would think that could also be automated in the ONE click.

    • #1487483

      Don,
      Thank you, it is working now. I’m not sure if the following answers your question..
      re. how the sheets are populated and to further automate the rest in one click.

      So far, everything worked as intended the next step in the manual process was to perform a visual comparisons between the two reports (order & Receiving). Let’s just take for example order # 51300 and it’s corresponding distribution amount. Following are possible scenarios:
      a. If order # 51300 exists in order report, but not in receiving – we can display a message like “not in receiving”,
      or ‘missing” etc.
      b. if order # 5103’s distribution amount = $465.67 in both reports we get “0” (order – receiving) or 465.67 – 465.67
      and we are not interested in this value
      c. if distribution amount for order report is different than the receiving distribution amount (delta) we want to know this
      value
      So, once everything works as intended, what is the best way to utilize this macro every month? For example, I’ll be getting January’s file in the beginning of next week?

      TIA
      Regards

      As an example let’s take

      Following is o Omit records that = 0
      o Missing  record is available in order report, but missing from receiving report
      o Negative values 
      o Positive values 

    • #1487515

      You should have mentioned all of this in your ORIGINAL post. It is frustrating to do a project and then be told it works but is not what is needed. I see no reason to continue with this as a freebie.

    • #1487588

      You should have mentioned all of this in your ORIGINAL post. It is frustrating to do a project and then be told it works but is not what is needed. I see no reason to continue with this as a freebie.

      OCM,

      First of all, I would like to once again apologize for the abruptness of the previous member’s response. Understandably, I can relate to the frustration that he may have when one needs to rewrite code because of unmentioned specifications. But we must also try to remember that projects are constantly evolving and that those who are not familiar with VBA might not know what information, or lack of, may change the way the code needs to be written. Nonetheless, I think a little bit of patience goes a long way. Don’t let this incident discourage you from posting your questions. Please note that there are many who are ready and willing to help by offering their time and not using this forum to promote their personal monetary gain. With that said, let me show you what I came up with for you.

      Attached is a workbook that takes a different approach in the form of a “master template”. Clicking the “Get Data” button will initiate the coded that will prompt you to navigate to your source file with the data (.xlsx). Once selected, it will open and import all your data into the master. The master sheets will be renamed the same as your source file and the source file will then close. The data on the two master sheets will be formatted and summed according to your specifications. A third master sheet called Consolidation will pull the files over and validate them giving the messages that you had indicated for various scenarios. I also included a scenario where there are matching order numbers with a receipt but with no order date. Please see the images below. Finally, the workbook will automatically be saved to the path folder you specify in Cell I1 with the same name as the source file but macro enabled (.xlsm). In this manner, the master template is never overwritten and resolves the issue of using it monthly with a different source file.

      HTH,
      Maud

      Order Report (Cell I1 has path. Make sure path ends with a backslash)
      39243-OCM1

      Receiving Report
      39244-OCM2

      Consolidation Report (Verification of missing or non-matching Distribution amounts, and Receipts with no orders)
      39245-OCM3

      • #1487614

        Hi Maud

        I agree with you. I thought that reply was rather brusque.

        Now, about your code:
        A filename can have more than one dot in the name e.g. “sample2015.02.01.xlsx”

        So, instead of using..
        s = Split(wb2.Name, “.”)
        wb2name = s(0)
        ..it would be safer to use
        wb2name =Dir(FileToOpen)

        Or, since you have used Set wb2 = ActiveWorkbook
        you could just use
        wb2name = wb2.Name
        or
        wb2name = ActiveWorkbook.Name

        ..just for info

        zeddy

    • #1487615

      Zeddy,

      Just updated my file to make a referral to a worksheet generic and saw your post. Absolutely correct as usual. You are always thinking outside the box!

      Maud

    • #1487621

      I do NOT need or desire anyone to apologize for me ever. I have been doing this a long time and grow tired of posters who can’t seem to know what they want and when you do an entire project for them decide that that is not really what they wanted anyway.

    • #1487652

      Make no mistake, I am not apologizing for you but rather attempting to repair the ramifications of your ill temper and the good name of this forum. And the only thing I do agree with you is that you should offer your own apologies but I really don’t see that forthcoming. We all have OPs that change course mid-stream and OPs that never even return for the solution, yet we all keep our cool. How about the same from you? I am sure you have a lot to offer so why not make it a positive thing?

      • #1489542

        Since OP never came back, you also wasted your time. Frustrating, isn’t it. BTW, I don’t thnk yours did as OP desired either and I’m not sure that OP would ever be satisfied. My comment about freebie was not asking for a paying project but just meant to comment on doing this kind of freebie project.Should have said
        “I see no reason to continue with this (leaving out the as a ) freebie.”

    • #1489549

      Hi Don

      It’s not a waste of time. We all learn from the postings.
      If the OP doesn’t come back, they are missing out.
      But there could be many reasons why they don’t come back as soon as we would like.
      Especially if we have posted a ‘nice’ solution.
      But there could be reasons for this, for example, I unexpectedly found myself in an Intensive Care ward for quite a while.

      So, just to show willing, I attach my version of a solution, together with the sample file for processing.
      If anyone wants to try it out. save both attached files to a folder of your choice.
      Then click the [Select datafile] button to select that sample file.

      zeddy

    Viewing 14 reply threads
    Reply To: enhance macro

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: