Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Copy row from one workbook to another workbook based on conditions in another Workbook

  1. #1
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0

    Copy row from one workbook to another workbook based on conditions in another Workbook

    Code:
    Sub STEP6()
        Dim Wb1 As Workbook
        Dim Wb2 As Workbook
        Dim wb3 As Workbook
        Dim Ws1 As Worksheet
        Dim Ws2 As Worksheet
        Dim ws3 As Worksheet
        Dim strPath As String
        Dim r As Long
        Dim m As Long
        Dim rng As Range
        Dim n As Long
        Application.ScreenUpdating = False
        Set Wb1 = Workbooks.Open(ThisWorkbook.Path & "1.xls")
        Set Ws1 = Wb1.Worksheets(1)
        m = Ws1.Range("H" & Ws1.Rows.Count).End(xlUp).Row
        strPath = ThisWorkbook.Path & ""
        Set Wb2 = Workbooks.Open(strPath & "OrderFormat.xlsx")
        Set Ws2 = Wb2.Worksheets(1)
        Ws2.Range("A1:A4").TextToColumns DataType:=xlDelimited, Tab:=True, _
        SemiColon:=False, Comma:=False, Space:=False, Other:=False, _
        ConsecutiveDelimiter:=False
        Set wb3 = Workbooks.Open(strPath & "BasketOrder..csv")
        Set ws3 = wb3.Worksheets(1)
        Set rng = ws3.Cells.Find(what:="*", searchorder:=xlByRows, SearchDirection:=xlPrevious)
        If rng Is Nothing Then
            n = 1
        Else
            n = rng.Row + 1
        End If
        For r = 2 To m
            If Ws1.Range("H" & r).Value > Ws1.Range("D" & r).Value Then
                Ws2.Range("A1").EntireRow.Copy Destination:=ws3.Range("A" & n)
                n = n + 1
            ElseIf Ws1.Range("H" & r).Value < Ws1.Range("D" & r).Value Then
                Ws2.Range("A3").EntireRow.Copy Destination:=ws3.Range("A" & n)
                n = n + 1
            End If
        Next r
        Application.DisplayAlerts = False
        Wb1.Close SaveChanges:=False
        Wb2.Close SaveChanges:=False
        wb3.SaveAs FileName:=strPath & "BasketOrder..csv", FileFormat:=xlCSV
        wb3.Close SaveChanges:=False
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    End Sub
    Condition:If column H of 1.xls is greater than column D of 1.xls then copy third row of orderformat.xlsx & paste it to basketorder.xlsx
    If column H of 1.xls is smaller than column D of 1.xls then copy first row of orderformat.xlsx & paste it to basketorder.xlsx

    sample file attached below
    Attached Files Attached Files
    Last edited by DocAElstein; 07-26-2020 at 01:23 PM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    Where does the original macro come from?

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://eileenslounge.com/viewtopic.php?p=318868#p318868
    https://eileenslounge.com/viewtopic.php?p=318311#p318311
    https://eileenslounge.com/viewtopic.php?p=318302#p318302
    https://eileenslounge.com/viewtopic.php?p=317704#p317704
    https://eileenslounge.com/viewtopic.php?p=317704#p317704
    https://eileenslounge.com/viewtopic.php?p=317857#p317857
    https://eileenslounge.com/viewtopic.php?p=317541#p317541
    https://eileenslounge.com/viewtopic.php?p=317520#p317520
    https://eileenslounge.com/viewtopic.php?p=317510#p317510
    https://eileenslounge.com/viewtopic.php?p=317547#p317547
    https://eileenslounge.com/viewtopic.php?p=317573#p317573
    https://eileenslounge.com/viewtopic.php?p=317574#p317574
    https://eileenslounge.com/viewtopic.php?p=317582#p317582
    https://eileenslounge.com/viewtopic.php?p=317583#p317583
    https://eileenslounge.com/viewtopic.php?p=317605#p317605
    https://eileenslounge.com/viewtopic.php?p=316935#p316935
    https://eileenslounge.com/viewtopic.php?p=317030#p317030
    https://eileenslounge.com/viewtopic.php?p=317030#p317030
    https://eileenslounge.com/viewtopic.php?p=317014#p317014
    https://eileenslounge.com/viewtopic.php?p=316940#p316940
    https://eileenslounge.com/viewtopic.php?p=316927#p316927
    https://eileenslounge.com/viewtopic.php?p=316875#p316875
    https://eileenslounge.com/viewtopic.php?p=316704#p316704
    https://eileenslounge.com/viewtopic.php?p=316412#p316412
    https://eileenslounge.com/viewtopic.php?p=316412#p316412
    https://eileenslounge.com/viewtopic.php?p=316254#p316254
    https://eileenslounge.com/viewtopic.php?p=316046#p316046
    https://eileenslounge.com/viewtopic.php?p=317050&sid=d7e077e50e904a138c794e1f2115da95#p317050
    https://www.youtube.com/@alanelston2330
    https://www.youtube.com/watch?v=yXaYszT11CA&lc=UgxEjo0Di9-9cnl8UnZ4AaABAg.9XYLEH1OwDIA35HNIei0z-
    https://eileenslounge.com/viewtopic.php?p=316154#p316154
    https://www.youtube.com/watch?v=TW3l7PkSPD4&lc=UgwAL_Jrv7yg7WWC8x14AaABAg
    https://teylyn.com/2017/03/21/dollarsigns/#comment-191
    https://eileenslounge.com/viewtopic.php?p=317050#p317050
    https://eileenslounge.com/viewtopic.php?f=27&t=40953&p=316854#p316854
    https://www.eileenslounge.com/viewtopic.php?v=27&t=40953&p=316875#p316875
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 07-25-2024 at 01:48 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    I got this macro in 2019 & I have not remebered from which forum i got this macro
    Plz see the post I have mentioned all the details & sample file is also attached
    Last edited by fixer; 07-16-2020 at 07:43 PM.

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    Quote Originally Posted by fixer View Post
    I got this macro in 2019 & I have not remebered from which forum i got this macro
    So look for it and find it
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Doc Sir I got this from expertsexchange I think so
    & I don't know exactly from which I'd I asked the question from so sorry for the same
    But I uploaded the sample file for this problem Sir & I mentioned the details too... Sir

  6. #6
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Unable to find the source of that macro Doc Sir
    So forget that macro Sir
    i have removed that macro bcoz it was working with .csv file & now i have replaced that file with .xlsx as per needs



    At that time i was unaware the .csv file file issue so thats y i replaced the .csv file from the process with .xlsx files
    So i need the macro of the same

  7. #7
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Last edited by fixer; 07-21-2020 at 02:14 PM.

  8. #8
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Code:
    Sub STEP6()
        Dim Wb1 As Workbook
        Dim Wb2 As Workbook
        Dim wb3 As Workbook
        Dim Ws1 As Worksheet
        Dim Ws2 As Worksheet
        Dim ws3 As Worksheet
        Dim r As Long
        Dim m As Long
        Dim rng As Range
        Dim n As Long
        Application.ScreenUpdating = False
        Set Wb1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
        Set Ws1 = Wb1.Worksheets(1)
        m = Ws1.Range("H" & Ws1.Rows.Count).End(xlUp).Row
        Set Wb2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\OrderFormat.xlsx")
        Set Ws2 = Wb2.Worksheets(1)
        Ws2.Range("A1:A4").TextToColumns DataType:=xlDelimited, Tab:=True, _
        SemiColon:=False, Comma:=False, Space:=False, Other:=False, _
        ConsecutiveDelimiter:=False
        Set wb3 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\BasketOrder.xlsx")
        Set ws3 = wb3.Worksheets(1)
        Set rng = ws3.Cells.Find(what:="*", searchorder:=xlByRows, SearchDirection:=xlPrevious)
        If rng Is Nothing Then
            n = 1
        Else
            n = rng.Row + 1
        End If
        For r = 2 To m
            If Ws1.Range("H" & r).Value > Ws1.Range("D" & r).Value Then
                Ws2.Range("A1").EntireRow.Copy Destination:=ws3.Range("A" & n)
                n = n + 1
            ElseIf Ws1.Range("H" & r).Value < Ws1.Range("D" & r).Value Then
                Ws2.Range("A3").EntireRow.Copy Destination:=ws3.Range("A" & n)
                n = n + 1
            End If
        Next r
        Application.DisplayAlerts = False
        Wb1.Close SaveChanges:=False
        Wb2.Close SaveChanges:=False
        wb3.Close SaveChanges:=True
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    End Sub
    Problem Solved
    Thnx Alot Doc Sir for helping me in solving this problem
    Have a Awesome Day

  9. #9
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,439
    Rep Power
    10
    Quote Originally Posted by fixer View Post
    .....
    I have removed that macro bcoz it was working with .csv file .....
    please don’t do major edits on posts, after anyone has replied. Minor changes, like correcting mistakes are OK, but do not remove or add a large amount. ( I did tell you about this before, but I expect you forgot )
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  10. #10
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    I reputted the data
    Sorry for the same Doc Sir

Similar Threads

  1. Replies: 2
    Last Post: 07-07-2020, 05:42 PM
  2. Replies: 101
    Last Post: 06-11-2020, 02:01 PM
  3. Replies: 4
    Last Post: 04-10-2014, 10:58 PM
  4. Replies: 2
    Last Post: 09-18-2013, 12:30 AM
  5. Replies: 2
    Last Post: 05-28-2013, 05:32 PM

Tags for this Thread

Posting Permissions

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