+ Reply to Thread
Results 1 to 55 of 55

VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Hello -

    I have no idea how to accomplish this task and seeking help.
    We receive an excel file every week with already existing records from prior week and new records added.
    Currrently we do a vlookup between current week spreadsheet (CP) and prior week spreadsheet (PP):
    if a records number (Note ID) in the CP exist in the PP then we copy/paste THREE fields values (Assess,, Comments, ECC) from the PP sheet to the CP sheet, these 3 fields are ADDED as shown the the attched example with the respected names

    I was wondering if a VBA macro with a botton where the user can open and click on the botton, the botton will do the follwing:
    1. Ask for the PRIOR period excel workbook (PP) path to be selected where we would like to COPY the notes in the three fields based on match with field "NOTE ID" (Note ID is the primary kep)
    2. Ask for the CURRENT period excel workbook (CP) path to be selected where we would to PASTE to
    3. If the record number (NOTE ID) in CP exist in PW sheet (we have a match) then we copy/paste (the information found in the three fields "Assess,, Comments, ECC" ) from the PP sheet to the CW sheet.

    If someone can help it would be greatly appreciated. Thank you in advance.

    Attached is a zip file contains 4 excel workbooks:
    PP - This is the prior period raw data (how it is received)
    PP+Comments - This is the PP but with the three fields added (Assess,, Comments, ECC) then based on reviews we input some information in the 3 fields ((Assess,, Comments, ECC)

    CP - This is the current period raw data (this data will always have new records and MAY have records from the prior period with comments (PP+Comments) with same primary key "Note ID"
    CP+Comments COPIED from PP - This is the CP we add the three fields and then we do a lookup from (PP+Comments) based on "Note ID" if there is a match then we copy and based the matched three fields information from PP+Comments to the new CP+Comments.
    Attached Files Attached Files
    Last edited by msaleh; 09-30-2022 at 11:33 AM. Reason: Clarify with excat needs

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    It would be easier to help and test possible solutions if you could attach copies of your two files. Explain in detail what you want to do referring to specific
    cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Hello - I have edited my notes and added a zipfile. Hope this will be helpfull and thank you for the attention.

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,278

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Picking the two folders is easy enough...
    Sub test()
    
    '1. Ask for the prior week (PW) path to be selected
        Dim PW As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select Prior Week Folder"
            If .Show = -1 Then
                PW = .SelectedItems(1)
            Else
                MsgBox "No folder selected."
                Exit Sub
            End If
        End With
    
    '2. Ask for the current week (CW) path to be selected
        Dim CW As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select Current Week Folder"
            If .Show = -1 Then
                CW = .SelectedItems(1)
            Else
                MsgBox "No folder selected."
                Exit Sub
            End If
        End With
        
    '3. Create two new fields at the end of the table of (CW) named (PFName and PLName)
    
    '4. If the record number (Encounter ID) in CW exist in PW sheet (we have a match)
    '   then we copy/paste (Patient First Name and Patient Last Name) from the PW sheet
    '   to the new fields (PFName and PLName) in CW sheet.
    
        
        MsgBox PW & vbNewLine & CW
    
    
    End Sub
    Now we need to know the names of the files in those folders to open, and also the names of the worksheets in each of those files to work on.

  5. #5
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Thank you Dangelor! I have made some clarifications to the intial request and supplied a zip-file.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,760

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Just for future reference: there is no need to SHOUT in your thread titles. All upper case is actually harder to read than sentence case, anyway. Thanks.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  7. #7
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Thank you Ali!

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    In the current week file and in the previous week file, you have duplicate values for Note ID 4552093. Is this an error or can there be duplicate Note ID values in both files?

  9. #9
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    I know I will forget to mention everything:
    1. When we receive the file, first we go over the field named "TERMED" and sort,
    2. We look where the value is "Yes", that is an indication there this is a termed person.
    3. We note the corresponding Document Owner Name, lets say ODNAME-153
    3. Now We delete all records for that (Document Owner Name, ODNAME-153) before we do any work. In other words; we delete all records where "Note ID" is showing more than once but first we make sure that Document Owner Name is Terrmed in one or more records. Hope this makes sense. If you think it is better to include the mechanism in the code to delete these or leave it out as we can do that prior to running any code. Thanks!

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Can there be duplicate Note ID's for any Document Owner Names that are not "Termed"? If there are any duplicate Note ID's at all in column R, that creates a problem.

  11. #11
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    After we remove the ALL records for any Document Owner Names that are termed, there should not be any duplicate Note ID, all Note ID's should be unique and that would be the primary key for each record.

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Make sure that there are no duplicate ID's in both files before trying this macro.
    Sub CopyData()
        Application.ScreenUpdating = False
        Dim i As Long, pwWS As Worksheet, cwWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object, CP As Workbook, PP As Workbook
        Dim flder As FileDialog, FileName As String, FileChosen As Integer
        Set flder = Application.FileDialog(msoFileDialogFilePicker)
        flder.Title = "Please select prior week file."
        FileChosen = flder.Show
        FileName = flder.SelectedItems(1)
        Set PP = Workbooks.Open(FileName)
        Set flder = Application.FileDialog(msoFileDialogFilePicker)
        flder.Title = "Please select current week file."
        FileChosen = flder.Show
        FileName = flder.SelectedItems(1)
        Set CP = Workbooks.Open(FileName)
        Set pwWS = PP.Sheets("All Unfinalized Notes Detai")
        Set cwWS = CP.Sheets("All Unfinalized Notes Detai")
        v1 = pwWS.Range("A2").CurrentRegion.Value
        v2 = cwWS.Range("A2").CurrentRegion.Value
        Set dic = CreateObject("Scripting.Dictionary")
        For i = LBound(v2) To UBound(v2)
            dic.Add v2(i, 18), Nothing
        Next i
        For i = LBound(v1) To UBound(v1)
            If dic.exists(v1(i, 18)) Then
                cwWS.Range("B" & i + 1).Resize(, 3).Value = Array(v1(i, 2), v1(i, 3), v1(i, 4))
            End If
        Next i
        Application.ScreenUpdating = True
    End Sub

  13. #13
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Thank you very much Mumps1.
    WHen I try this code I get the following error message
    "Run-time error 9, supscription out of range" and when I click on debug it at at
    " For i = LBound(v2) To UBound(v2)
    dic.Add v2(i, 18), Nothing

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    The attached file shows the result when I run the macro using the two files you posted. It worked with no errors. Are you using the macro on the same two files?
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    That is the issue, I pasted your macro to a new excel workbook. I was hoping to save the macro to a button in a stand alone excel for the users to click on it so they can do the process since they users don't know how to use macro.
    Also, I was going to ask if the formatting for the three fields coped and pasted would be same from PP to CP, reason for that they cells in the three fields are colored based on the review and if it was just text copy/paste it will loss the colors.

    Thank you again for all your help, greatly appreciated.

  16. #16
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    I pasted your macro to a new excel workbook.
    This is the best approach. Also, you should notice that the copied fields retain the cell formatting, in this case the font color. Is the macro working for you or are you still getting an error. If you are still getting an error, post copies of the two files that are generating the error.

  17. #17
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Just so I understand before I answer, I am using the same files I sent you in the zip folder (PP, and CP)
    Where should I paste the macro please? New workbook-sheet, PP, or the CP? and should I paste the code as new Module?

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    I just don't understand about the existence of PP.xlsx and CP.xlsx.

    This is for PP + Comments and CP + Comments workbooks.

    Sub test()
        Dim fn(1) As String, a(1), i As Long, ii As Long, x
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP+Comments")
        If fn(1) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With Workbooks.Open(fn(0)).Sheets(1).[a2].CurrentRegion
            a(0) = Application.Index(.Value, Evaluate("row(1:" & .Rows.Count & ")"), [{18,2,3,4}])
            .Parent.Parent.Close False
        End With
        With Workbooks.Open(fn(1)).Sheets(1).[a2].CurrentRegion
            a(1) = Application.Index(.Value, Evaluate("row(1:" & .Rows.Count & ")"), [{2,3,4,18}])
            For i = 2 To UBound(a(1), 1)
                x = Application.Match(a(1)(i, 4), Application.Index(a(0), 0, 1), 0)
                For ii = 1 To 3
                    If IsNumeric(x) Then
                        a(1)(i, ii) = a(0)(x, ii + 1)
                    Else
                        a(1)(i, ii) = Empty
                    End If
                Next
            Next
            .Columns(2).Resize(UBound(a(1), 1), 3) = a(1)
            .Parent.Parent.Close True
        End With
        Application.ScreenUpdating = True
    End Sub

  19. #19
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Current period's new excel file is PP,
    Prior Peiod excel file that was reviewd and comments has been added

    So, we receive (CP) file, we need to compare with (PP+Comments), we copy and paste the three fields from the PP+Comments to CP where the "Note ID" is same. After we copy paste we start revieing other records in CP and we type new comments for the new records we reviewed. Once completed the review we save the file ( This excel file becomes the PP+Comments once the FUTURE new period excel file is received.) then we start again in a loop. Hope this helps.

  20. #20
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Try it in a new workbook.

  21. #21
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Ok, will try it in a new book again and will let you know. Thank you!

  22. #22
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Mumps1 -
    I just tried and I got the same error as before
    "Run-time error 9, supscription out of range" and when I click on debug it at at
    " For i = LBound(v2) To UBound(v2)
    dic.Add v2(i, 18), Nothing

    I am attaching the two files I used.
    Attached Files Attached Files

  23. #23
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,888

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    The CP file doesn't contain the Assess, Comments and ECC columns.

  24. #24
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Quote Originally Posted by Mumps1 View Post
    The CP file doesn't contain the Assess, Comments and ECC columns.
    Hi Mumps1 -
    Thank you very much for the help in this.That is correct, the CP doesn't contain the three fields, the fields will need to be created in it.

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Now I think I understand.

    Insert 3 columns in CP.xlsx with the results.
    Sub test()
        Dim fn(1) As String, a(1), b, i As Long, ii As Long, x
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
        If fn(1) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With Workbooks.Open(fn(0)).Sheets(1).[a2].CurrentRegion
            a(0) = Application.Index(.Value, Evaluate("row(1:" & .Rows.Count & ")"), [{2,3,4,18}])
            .Parent.Parent.Close False
        End With
        With Workbooks.Open(fn(1)).Sheets(1).[a2].CurrentRegion
            a(1) = Application.Index(.Value, 0, 15)
            ReDim b(1 To UBound(a(1), 1), 1 To 3)
            For i = 2 To UBound(a(1), 1)
                x = Application.Match(a(1)(i, 1), Application.Index(a(0), 0, 4), 0)
                If IsNumeric(x) Then
                    For ii = 1 To 3
                        b(i, ii) = a(0)(x, ii)
                    Next
                End If
            Next
            .Columns("b:d").Insert
            .Columns("b:d").Value = b
            .Range("b1:d1") = a(0)
            .Parent.Parent.Close True
        End With
        Application.ScreenUpdating = True
    End Sub

  26. #26
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Jindon -
    Thank you first of all. Is it possible to preserve the format of the cells for the three fileds (font color, cell color...) excaxtly copy and paste with all the formatting?

  27. #27
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Change to
    Sub test()
        Dim fn(1) As String, a, i As Long, r As Range, x
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
        If fn(1) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With Workbooks.Open(fn(0)).Sheets(1).[a2].CurrentRegion
            Set r = Union(.Columns("b:d"), .Columns("r"))
            With Workbooks.Open(fn(1)).Sheets(1).[a2].CurrentRegion
                a = Application.Index(.Value, 0, 15)
                .Columns("b:d").Insert
                r.Rows(1).Copy .Cells(1, "b").Resize(, 3)
                For i = 2 To UBound(a, 1)
                    x = Application.Match(a(i, 1), r.Areas(2), 0)
                    If IsNumeric(x) Then r.Rows(x).Resize(, 3).Copy .Cells(i, "b")
                Next
                Application.CutCopyMode = False
                .Parent.Parent.Close True
            End With
            .Parent.Parent.Close False
        End With
        Application.ScreenUpdating = True
    End Sub

  28. #28
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Quote Originally Posted by jindon View Post
    Change to
    ...
    Hi Jindon -

    Again, thank you for the help in this process, your last code seems to work fine, I will keep testing to confirm.

    The original (CP) excel file we receive is 1000-3000 rows, and We usually have to do some FILTERING and DELETING of records to the ORIGINAL excel (CP) file. We do the filtering manually, I was wondering ONLY if it is not a hassle and ONLY if it it reasonable to ask: If a function can automate the filtering to the ORIGINAL excel file before we do the copy/base code above. Again, I thought to ask ONLY if it is reasonable:

    FILTERING:
    1. Appointment status = Arrived
    2. Occupation = Nurse practitioner, Physician and Physician assistant
    3. Note Type = Office, Nurse, Letter, Teleph, Pediat, and STARTS with "RES"
    4. Note STatus = Needs input, Amended Signed, Amended Unsigned

    DELETING:
    Also, we delete some records based on the following:
    We check the field "TERMED" for the value "Yes" (If any) and we note the name under the field "DOCUMENT OWNER", then we search for that name and delete all records where that document owner's name.

    Then we save as this ORIGINAL excel fiel after the filtering and deletion we save it as (CP) before we start the copy/paste from the PP+Comments the three fields (the code you already wrote above.)

    Thank you again!
    Last edited by AliGW; 10-06-2022 at 03:24 AM. Reason: Please DON'T quote unnecessarily!

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Filtering above with AND or OR?
    Then delete?
    Last edited by AliGW; 10-06-2022 at 03:24 AM. Reason: Please DON'T quote unnecessarily!

  30. #30
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    FILTERING with "AND" please.
    Last edited by AliGW; 10-06-2022 at 03:23 AM. Reason: Please DON'T quote unnecessarily!

  31. #31
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    So Filtering

    Appointment status = Arrived
    And
    Occupation = Nurse practitioner or Physician or Physician assistant
    And
    Note Type = Office or Nurse or Letter or Teleph or Pediat or RES*
    And
    Note STatus = Needs input or Amended Signed or Amended Unsigned

    Is that correct?

  32. #32
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Yes, that is for the filtering.
    Last edited by AliGW; 10-06-2022 at 03:23 AM. Reason: Please DON'T quote unnecessarily!

  33. #33
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Sorry, I am trying my best to clarify.
    Last edited by AliGW; 10-06-2022 at 03:23 AM. Reason: Please DON'T quote unnecessarily!

  34. #34
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    If that's true, CP.xlsx uploaded has no matched data, correct?

  35. #35
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Not sure I completely understand what you mean however, the CP I uploaded is a only 100 or so records while the actual ORIGINAL excel file (Sorry, I can't share) before the filtering and deletion has 1000-3000 records.
    The CP I uploaded may not have OTHER values for the mentioned fields other than what we need to filter on. you may need to change some of the values in the current fields to test the filtering. Not sure if I answered your question.
    Last edited by AliGW; 10-06-2022 at 03:22 AM. Reason: Please DON'T quote unnecessarily!

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    See attached
    Sheet1 must have filtering list for Advanced Filter.
    Sub test()
        Dim fn(1) As String, a, i As Long, r As Range, x
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
        If fn(1) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With Workbooks.Open(fn(0)).Sheets(1).[a2].CurrentRegion
            Set r = Union(.Columns("b:d"), .Columns("r"))
            With Workbooks.Open(fn(1)).Sheets(1).[a2].CurrentRegion
                FilterAndDelete .Cells
                a = Application.Index(.Value, 0, 15)
                .Columns("b:d").Insert
                r.Rows(1).Copy .Cells(1, "b").Resize(, 3)
                For i = 2 To UBound(a, 1)
                    x = Application.Match(a(i, 1), r.Areas(2), 0)
                    If IsNumeric(x) Then r.Rows(x).Resize(, 3).Copy .Cells(i, "b")
                Next
                Application.CutCopyMode = False
                .Parent.Parent.Close True
            End With
            .Parent.Parent.Close False
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Sub FilterAndDelete(rng As Range)
        Dim r As Range, x
        With rng
            Set r = .Offset(, .Columns.Count + 2).Cells(1)
            ThisWorkbook.Sheets(1).Cells(1).CurrentRegion.Copy r
            .AdvancedFilter 1, r.CurrentRegion
            If .Columns(1).SpecialCells(12).Count > 1 Then
                .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
            End If
            If .Parent.FilterMode Then .Parent.ShowAllData
            r.CurrentRegion.Clear
            x = Filter(.Parent.Evaluate("transpose(if(" & .Columns("d").Address & _
                "=""yes""," & .Columns("b").Address & "))"), False, 0)
            .AutoFilter 2, x, 7
            If .Columns(1).SpecialCells(12).Count > 1 Then
                .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
            End If
            If .Parent.FilterMode Then .Parent.ShowAllData
        End With
    End Sub
    Attached Files Attached Files

  37. #37
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Hi -
    I am getting a run time error "1004"
    and when debug it highlights the code: ".Parent.ShowAllData" in the part:

    End If
    If .Parent.FilterMode Then .Parent.ShowAllData
    End With
    End Sub
    Last edited by AliGW; 10-06-2022 at 03:22 AM. Reason: Please DON'T quote unnecessarily!

  38. #38
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    OK, tables...
    Sub test()
        Dim fn(1) As String, a, i As Long, r As Range, x, tbl As ListObject
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
        If fn(1) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With Workbooks.Open(fn(0)).Sheets(1).ListObjects(1)
            Set r = Union(.ListColumns(2).Range.Resize(, 3), .ListColumns(18).Range)
            With Workbooks.Open(fn(1)).Sheets(1)
                Set tbl = .ListObjects(1)
                FilterAndDelete tbl
                With tbl
                    a = Application.Index(.Range, 0, 15)
                    .Parent.Columns("b:d").Insert
                    r.Rows(1).Copy .HeaderRowRange.Columns(2)
                    For i = 2 To UBound(a, 1)
                        x = Application.Match(a(i, 1), r.Areas(2), 0)
                        If IsNumeric(x) Then r.Rows(x).Resize(, 3).Copy .ListColumns(2).Range(i)
                    Next
                End With
                Application.CutCopyMode = False
                .Parent.Close True
            End With
            .Parent.Parent.Close False
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Sub FilterAndDelete(rng As ListObject)
        Dim r As Range, x
        With rng
            Set r = .Parent.Cells(1, .ListColumns.Count + .ListColumns(1).Range.Column + 2)
            ThisWorkbook.Sheets(1).Cells(1).CurrentRegion.Copy r
            .Range.AdvancedFilter 1, r.CurrentRegion
            If .ListColumns(1).Range.SpecialCells(12).Count > 1 Then
                .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
            End If
            If .Parent.FilterMode Then .Parent.ShowAllData
            r.CurrentRegion.Clear
            x = Filter(.Parent.Evaluate("transpose(if(" & .ListColumns(4).Range.Address & _
                "=""yes""," & .ListColumns(2).Range.Address & "))"), False, 0)
            .Range.AutoFilter 2, x, 7
            If .ListColumns(1).Range.SpecialCells(12).Count > 1 Then
                .DataBodyRange.EntireRow.Delete
            End If
            If .Parent.FilterMode Then .ShowAutoFilter = False
        End With
    End Sub

  39. #39
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Dear Jindon - I have tried the last modified code using the sample files and seems to work fine for the copy/paste of the three fields if exist, and for the deletion of records for "Termed".
    However, I made a mistake in my response to you regarding the filtering, you asked if it is "AND" or "OR" I said "And" but IT SHOULD BE "OR". I tested fabricated records but it is filtering records correctly when I change any of the filtering fields to a vlaues not listed in "sheet1". it was a mistake from my end please forgive me if it is a hassle.

    If possible to change the filtering to "OR" please.
    And one more thing; Could it be possible after the copy/paste, deletion and filtering not to save the new result file over the original CP?
    Instead if possible for the new modified CP file to be saved as "Save AS - CP_DATE and TIME STAMP) or (Promte the user to save with the desired file name of choice)? I would like to preserve or keep the original "CP". Thank you again for all the trouble!
    Last edited by AliGW; 10-06-2022 at 03:21 AM. Reason: Please DON'T quote unnecessarily!

  40. #40
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    See attached
    Sub test()
        Dim fn(2) As String, a, i As Long, r As Range, x, tbl As ListObject
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
        If fn(1) = "False" Then Exit Sub
        fn(2) = Application.GetSaveAsFilename("CP_" & Format$(Now, "yyyymmmdd hhmmss"), _
                                        "Excel Book,*.xlsx", , "Enter New File Name")
        If fn(2) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With Workbooks.Open(fn(0)).Sheets(1).ListObjects(1)
            Set r = Union(.ListColumns(2).Range.Resize(, 3), .ListColumns(18).Range)
            With Workbooks.Open(fn(1)).Sheets(1)
                Set tbl = .ListObjects(1)
                FilterAndDelete tbl
                With tbl
                    a = Application.Index(.Range, 0, 15)
                    .Parent.Columns("b:d").Insert
                    r.Rows(1).Copy .HeaderRowRange.Columns(2)
                    For i = 2 To UBound(a, 1)
                        x = Application.Match(a(i, 1), r.Areas(2), 0)
                        If IsNumeric(x) Then r.Rows(x).Resize(, 3).Copy .ListColumns(2).Range(i)
                    Next
                End With
                Application.CutCopyMode = False
                With .Parent
                    .SaveAs fn(2)
                    .Close False
                End With
            End With
            .Parent.Parent.Close False
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Sub FilterAndDelete(rng As ListObject)
        Dim r As Range, x
        With rng
            If .ShowAutoFilter Then .ShowAutoFilter = False
            Set r = .Parent.Cells(1, .ListColumns.Count + .ListColumns(1).Range.Column + 2)
            ThisWorkbook.Sheets(1).Cells(1).CurrentRegion.Copy r
            .Range.AdvancedFilter 1, r.CurrentRegion
            If .Parent.FilterMode Then .DataBodyRange.Delete
            If .Parent.FilterMode Then .Parent.ShowAllData
            r.CurrentRegion.Clear
            x = Filter(.Parent.Evaluate("transpose(if(" & .ListColumns(4).Range.Address & _
                "=""yes""," & .ListColumns(2).Range.Address & "))"), False, 0)
            If UBound(x) > -1 Then
                .ShowAutoFilter = True
                .Range.AutoFilter 2, x, 7
                If .ListColumns(1).Range.SpecialCells(12).Count > 1 Then
                    .DataBodyRange.Delete
                End If
            End If
            .ShowAutoFilter = False
        End With
    End Sub
    Attached Files Attached Files
    Last edited by AliGW; 10-06-2022 at 03:22 AM. Reason: Please DON'T quote unnecessarily!

  41. #41
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Not sure why the new code is not working as desired.
    Please see attached CP and PP+Comments I tested

    In (CP) I highligted records with action:
    GREEN records - These 5 records exist in the (PP+Comments) with comments in any of the three fields, so comments should be copied and pasted in the new resulted file.
    RED records - Termed 4 records, so any record(s) having that "Document Owner Name" should not be included.
    BLUE records - 6 records having either "Occupation" or "Note Type" not from the list and these records should not be included.
    Attached Files Attached Files
    Last edited by msaleh; 10-06-2022 at 08:20 AM. Reason: Correction

  42. #42
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    82,760

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Administrative Note to ALL Participants:

    Please don't quote whole posts, especially when you are responding to the one immediately preceding your own - it's just clutter and rarely necessary.

    If you are responding out of sequence, it is usually enough just to mention the helper's user name (e.g @AliGW).

    If you do need to quote, limit the quoted section just to the section to which you wish to draw your helper's attention or a direct question to which you wish to respond.

    For normal conversational replies, try using the QUICK REPLY box below or the REPLY button instead of REPLY WITH QUOTE.

  43. #43
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Thank you AliGW for the infromation. Will do!

  44. #44
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    I wrote as you requested.

    Upload your EXACT deired result of new CP workbook from above 2 workbooks.

  45. #45
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    I am very sorry. I don't know why. See attached resulted file.
    Attached Files Attached Files

  46. #46
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    If the last workbook is your desired result, my code is working.
    what's wrong?

  47. #47
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    My apology, maybe my explination is not clear:
    The file I send you last (CP_2022Oct06 083938) is not the desired result but it is the result from using the last code based on the two files (CP) and (PP+Comments) Attached to this reply is the desired result.

    I tried to explaining in my prior reply using the colored records in (CP) and what sould happen to these records;
    (RED and BLUE colored records in the CP would not be included in the desired new result for the reasons explianed in the prior reply) and
    (GREEN would have matched "NOTE ID" in (PP+Comments) with comments in the 3-fields, thus we need to copy/past comments from the three fields from (PP+Comments) to the new desired new result. in conclusion, the new desired file would have all the (CP) records except the records in BLUE and RED and would have the comments copied for the matched "NOTE ID" records.

    I hope this helps.
    Attached Files Attached Files

  48. #48
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    In you result, you have many rows with Physician in Occupation column.

    Also Arrived in Appointment Status column.

    why?

    How are you actualy picking the rows to be deleted from CP.xls manually?

  49. #49
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Maybe I wanst clear on the filtering. We do the following filtering to keep those meeting the condition and not to excleude.

    FILTERING:
    1. First we filter records to keep (include) when "Appointment status" = Arrived
    2. Next, from the remaining result we filter records to keep (include) when "Occupation" = Nurse practitioner, or Physician or Physician assistant
    3. Next, from the remaining result we filter records to keep (include) "Note Type" = Office, or Nurse, or Letter, or Teleph, pr Pediat, or any that STARTS with "RES"
    4. Next, from the remaining result we filter records to keep (include) "Note STatus" = Needs input, Amended Signed, Amended Unsigned

    Do I have any conflict in the test data? (recall that I tried to make up the test files for the CP and PP+Comments files and hope there are no conflict.)

  50. #50
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Dear Jindon -

    I am very sorry to keep bothering you with this request, you have been very patient and helpfull.
    I have made a new TEST files for "CP" and "PP+Comments" as I found some inconsistancy with my prior files.
    With the consideration about the filtering process I mentioned in the preior reply I am attaching the new files along with the Desired file.

    I also attached CP-Explianed I tried to highlight with colors to make it easy to understand.
    GREEN, are exsisting in PP+Commnets with notes in any of the 3 fields, Thus notes from the 3 fields will be copied
    RED- Document Owner Name is TEREMED
    BLUE - Records that should not show in the result, Highlighted reason in Dark Blue.

    Also, I am receiving a message write after the Save As file "CP_Oct..."The message says "Delete entire sheet now?"
    Attached Files Attached Files

  51. #51
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Explanation of #49 and files in #50 helped.

    try the attached.
    Sub test()
        Dim fn(2) As String, a, i As Long, r As Range, x, tbl As ListObject, dic As Object
        fn(0) = Application.GetOpenFilename("Excel Book,*xls?", , "Select PP+Comments")
        If fn(0) = "False" Then Exit Sub
        fn(1) = Application.GetOpenFilename("Excel Book,*xls?", , "Select CP")
        If fn(1) = "False" Then Exit Sub
        fn(2) = Application.GetSaveAsFilename("CP_" & Format$(Now, "yyyymmmdd hhmmss"), _
                                        "Excel Book,*.xlsx", , "Enter New File Name")
        If fn(2) = "False" Then Exit Sub
        Application.ScreenUpdating = False
        With ThisWorkbook.Sheets(1).Cells(1).CurrentRegion
            ReDim myList(1 To 2, 1 To .Columns.Count)
            For i = 1 To .Columns.Count
                myList(1, i) = .Cells(1, i)
                myList(2, i) = Filter(.Parent.Evaluate("transpose(if(" & .Columns(i).Offset(1).Address & _
                            "<>""""," & .Columns(i).Offset(1).Address & "))"), False, 0)
            Next
        End With
        With Workbooks.Open(fn(0)).Sheets(1).ListObjects(1)
            Set r = Union(.ListColumns(2).Range.Resize(, 3), .ListColumns(18).Range)
            With Workbooks.Open(fn(1)).Sheets(1)
                Set tbl = .ListObjects(1)
                For i = 1 To UBound(myList, 2)
                    myList(2, i) = GetAll(myList(2, i), tbl, myList(1, i))
                Next
                FilterAndDelete tbl, myList, fn(2)
                .Parent.Close False
            End With
            fn(2) = Mid$(fn(2), InStrRev(fn(2), "\") + 1)
            With Workbooks(fn(2)).Sheets(1)
                Set tbl = .ListObjects(1)
                With tbl
                    a = Application.Index(.Range, 0, 15)
                    .Parent.Columns("b:d").Insert
                    r.Rows(1).Copy .HeaderRowRange.Columns(2)
                    For i = 2 To UBound(a, 1)
                        x = Application.Match(a(i, 1), r.Areas(2), 0)
                        If IsNumeric(x) Then r.Rows(x).Resize(, 3).Copy .ListColumns(2).Range(i)
                    Next
                End With
                Application.CutCopyMode = False
                .Parent.Close True
            End With
            .Parent.Parent.Close False
        End With
        Application.ScreenUpdating = True
    End Sub
    
    Sub FilterAndDelete(rng As ListObject, myList, fn As String)
        Dim a, x, i As Long, ii As Long, iii As Long, ub As Long, temp
        With rng
            temp = .Name
            If .ShowAutoFilter Then .ShowAutoFilter = False
            rng.ShowAutoFilter = True
            For i = 1 To UBound(myList, 2)
                .Range.AutoFilter myList(1, i), myList(2, i), 7
            Next
            .Parent.Copy
            With ActiveWorkbook.Sheets(1)
                .ListObjects(1).Delete
                .Parent.SaveAs fn
                rng.Range.SpecialCells(12).Copy .[a2]
                .ListObjects.Add(1, .[a2].CurrentRegion, 1).Name = temp
                .ListObjects(1).TableStyle = ""
                Set rng = .ListObjects(1)
            End With
        End With
        With rng
            x = Filter(.Parent.Evaluate("transpose(if(" & .ListColumns(4).Range.Address & _
                "=""yes""," & .ListColumns(2).Range.Address & "))"), False, 0)
            If UBound(x) > -1 Then
                .ShowAutoFilter = True
                .Range.AutoFilter 2, x, 7
                If .ListColumns(1).Range.SpecialCells(12).Count > 1 Then
                    .DataBodyRange.EntireRow.Delete
                End If
            End If
            .ShowAutoFilter = False
        End With
    End Sub
    
    Function GetAll(x, tbl As ListObject, ref)
        Dim a, ub As Long, i As Long, ii As Long, dic As Object
        Set dic = CreateObject("Scripting.Dictionary")
        For i = LBound(x) To UBound(x)
            If x(i) Like "*[*]*" Then
                a = tbl.ListColumns(ref).Range.Value
                For ii = 1 To UBound(a, 1)
                    If a(ii, 1) Like x(i) Then dic(a(ii, 1)) = Empty
                Next
                If dic.Count Then
                    ub = UBound(x)
                    ReDim Preserve x(ub + dic.Count)
                    For ii = 0 To dic.Count - 1
                        x(ub + ii + 1) = dic.keys()(ii)
                    Next
                End If
            End If
        Next
        GetAll = x
    End Function
    Attached Files Attached Files

  52. #52
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Dear Jindon - This last code seems to be working with the test files. I will be in the office tomorrow to test with the actual real files. I will keep you updated.
    I can't thank you enough for the effort and patients.

  53. #53
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Dear Jindon,

    Once again I would like to thank you and everyone participated in assisting. Thank you very much.

    I do have another process that consume time and was wondering if you would be will ing to assist.
    I am attaching two files and here is the detailed expination. Please let me know if you are able to assist and if you have any question for me to clarify.

    **********************************************************
    1. 1.
    Extract new “UNF” file from the system and save with today’s date and time in a new folder Let’s say it is saved as “UNF-11282022”
    • a.
    Sheet1 in “UNF-11282022” contains the following field in that order:
    • b.
    Practice, Document Owner, NPI, Termed?, Occupation, MRN, Patient Name, DOB, Encounter Date, Appointment Date, Appointment Type, Appointment Status, Note Status, Note ID,, Note Type, Age in Days,
    1. 2.
    Add three fields at the end of the table; “President/Chair”, “Dir/Admin”, “PCDA”
    • a.
    Practice, Document Owner, NPI, Termed?, Occupation, MRN, Patient Name, DOB, Encounter Date, Appointment Date, Appointment Type, Appointment Status, Note Status, Note ID, Note Type, Age in Days, President/Chair, Dir/Admin, PCDA
    1. 3.
    We have another sheet in another workbook called “PCDA MASTER TEMPLATE” the sheet contain a table that has the following fields
    • a.
    Practice, Document Owner, NPI, Occupation, President/Chair, Director/Administrator, PCDA,
    • b.
    This table is updated MANUALLY based on the following process :
    • i.
    Filter the new “UNF-11282022” on the field named “Occupation” to be ONLY = “Physician, Physician Assistant, Nurse Practitioner”
    • ii.
    Add the following formulas in the following fields in the “UNF-11282022” file (Basically vlookup the Document owner in the “PCDA MASTER TEMPLATE” if no match it show this is a new provider.)
    • 1.
    President/Chair = IF(OR([@Occupation]="Physician",[@Occupation]="Physician Assistant",[@Occupation]="Nurse Practitioner"),IFNA(VLOOKUP([@[Document Owner]],'PCDA MASTER TEMPLATE.xlsx'!Table2[[Document Owner]:[PCDA]],4,0),"*New Provider*"),"Non-NP-PA-MD")
    • 2.
    Dir/Admin =IF(OR([@Occupation]="Physician",[@Occupation]="Physician Assistant",[@Occupation]="Nurse Practitioner"),IFNA(VLOOKUP([@[Document Owner]],'PCDA MASTER TEMPLATE.xlsx'!Table2[[Document Owner]:[PCDA]],5,0),"*New Provider*"),"Non-NP-PA-MD")
    • 3.
    PCDA =IF(OR([@Occupation]="Physician",[@Occupation]="Physician Assistant",[@Occupation]="Nurse Practitioner"),IFNA(VLOOKUP([@[Document Owner]],'PCDA MASTER TEMPLATE.xlsx'!Table2[[Document Owner]:[PCDA]],6,0),"*New Provider*"),"Non-NP-PA-MD")
    • c.
    Once the lookup is completed we identify the new providers and go back to the “PCDA MASTER TEMPLATE” and add them by copying the appropriate 4- fields information (Practice Document Owner NPI Occupation President/Chair Director/Administrator PCDA) for each new provider from the “UNF-11282022” to the bottom of the table in “PCDA MASTER TEMPLATE”. And this is how the “PCDA MASTER TEMPLATE” gets updated for the next time we do the process
    • d.
    Now “UNF-11282022” is refreshed to update once the “PCDA MASTER TEMPLATE” is updated and we should have no new providers.
    1. 4.
    Next we need to generate from “UNF-11282022” a separate excel workbook with one sheet in it that has all the information based on the Filed “PCDA” FOR EACH PCDA ( Each PCDA will have a separate workbook named after the name for that PCDA.)
    ********************************************************************


    Thank you!
    Mike
    Attached Files Attached Files
    Last edited by msaleh; 11-28-2022 at 06:11 PM.

  54. #54
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,666

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Extract new “UNF” file from the system
    How?
    save with today’s date and time in a new folder Let’s say it is saved as “UNF-11282022”
    New folder in which folder?

  55. #55
    Registered User
    Join Date
    11-15-2012
    Location
    United States
    MS-Off Ver
    Excel 2016
    Posts
    36

    Re: VBA MACRO COPY and PASTE BASED ON CONDITION BETWEEN EXCEL WORKBOOKS

    Hi Jindon -

    We have a internal software system where we run the report, once the report is ready we save it as (UNF-Todays date) into a specific folder on our shared drive called UNF-Master (E://UNF-MASTER) DId I answer your question?

+ 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. Excel VBA Loop Macro - Copy Different Ranges and Paste into Multiple Workbooks
    By Hoover5896 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2017, 08:47 PM
  2. [SOLVED] Macro to copy paste between two workbooks (Excel 2016) fizzling
    By mab.alianza.ez8 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2017, 11:21 AM
  3. Replies: 5
    Last Post: 11-07-2016, 07:49 AM
  4. Replies: 2
    Last Post: 04-27-2016, 11:30 AM
  5. [SOLVED] Copy Row data with condition and paste in new workbooks
    By tdugga in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-23-2013, 03:41 PM
  6. [SOLVED] Macro to copy row data based on a condition from several workbooks.
    By Ilikeideas in forum Excel Programming / VBA / Macros
    Replies: 42
    Last Post: 04-01-2013, 04:09 AM
  7. Excel Macro to insert two rows based on condition and copy and paste multiple cells
    By mannabhalo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-13-2012, 12:56 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