Page 6 of 9 FirstFirst ... 45678 ... LastLast
Results 51 to 60 of 86

Thread: Copy Paste based on comparisons calculations in 2 XL files, 1 might be .csv file .Opened in XL=Fail/Chaos

  1. #51
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    If the only change is to paste the data to Ws3, then I see just one error in your macro ,
    Why have you changed to
    For Cnt = 2 To Lr3 ?
    It should still be
    For Cnt = 2 To Lr2
    The macro is going down rows in worksheet Ws2 from row 2 until the last row which is Lr2
    My Lr22 = your Lr3 is the row count for data being pasted out : For each new data is needed a new row - the next row - the next row will be .. + 1

    If the only change is to paste to Ws3 , then my original macro is only needed to be changed in 5 places


    Code:
    Sub Step11b() '   http://www.excelfox.com/forum/showthread.php/2421-copy-and-paste-by-VBA-based-on-criteria?p=13110&viewfull=1#post13110  http://www.excelfox.com/forum/showthread.php/2458-Copy-and-paste-the-data-if-condition-met
    Rem 1 Worksheets info
    Dim Wb1 As Workbook, Wb2 As Workbook, Wb3 As Workbook   '                           If the workbook is already open , then we can refer to it using the workbooks collection object of open workbooks,  Workbooks(" ")
     Set Wb1 = .......   Workbooks("1.xls")         ' Workbooks("1.xlsx")         '          Workbooks("sample1.xlsx")   '                                                 Set Wb1 = Workbooks.Open(ThisWorkbook.Path & "\1.xls")                ' w1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")           ' change the file path   If the workbook is already open , then we can refer to it using the workbooks collection object of open workbooks,  Workbooks(" ")
     Set Wb2 = .......     Workbooks("2.xls")         ' Workbooks("2.xlsx")         '          Workbooks("sample2.xlsx")   '                                                 Set Wb2 = Workbooks.Open(ThisWorkbook.Path & "\FundsCheck.xlsb")      ' w2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb") ' change the file path      If the workbook is already open , then we can refer to it using the workbooks collection object of open workbooks,  Workbooks(" ")
     Set Wb3 = .......
    Dim Ws1 As Worksheet, Ws2 As Worksheet, Ws3 As Worksheet ' Ws22 As Worksheet
     Set Ws1 = Wb1.Worksheets.Item(1)      '                                                                            Set Ws1 = Wb1.Worksheets("anything")  '     sheet name can be anything
     Set Ws2 = Wb2.Worksheets.Item(1)      '                                                                          ' Set Ws2 = Wb2.Worksheets("anything")
    ' Set Ws22 = Wb2.Worksheets.Item(2)
     Set Ws3 = Wb3.Worksheets.Item(2)
    Dim Lr1 As Long, Lr2 As Long, Lr As Long, Lr22 As Long
     Let Lr1 = Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Row      '   http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11466&viewfull=1#post11466      Making Lr dynamic ( using rng.End(XlUp) for a single column. )
     Let Lr2 = Ws2.Range("A" & Ws1.Rows.Count).End(xlUp).Row
    ' Let Lr = IIf(Lr2 > Lr1, Lr2, Lr1)
    Rem 2 do it
    Dim Cnt
        For Cnt = 2 To Lr2
        Dim VarMtch As Variant
         Let VarMtch = Application.Match(CStr(Ws1.Range("I" & Cnt & "").Value), Ws2.Range("B2:B" & Lr2 & ""), 0) ' We look for the string value from each row in column I of Ws1 in the range of column B in Ws2
            If Not IsError(VarMtch) Then ' If we have a match, then  Application.Match  will return an integer of the position along(down) where the match is found
            ' do nothing
            Else '  Application.Match  will return a VB error string if no match could be found
             Ws1.Range("B" & Cnt & ",I" & Cnt & "").Copy  ' if ranges are "in line" - that is to say have the same "width" ( in this example a single row width ) , then VBA lets us copy this to the clipboard
             Let Lr22 = Lr22 + 1 ' next free row in second worksheet of 2.xls
             'Ws22.Range("A" & Lr22 & "").PasteSpecial Paste:=xlPasteValues ' Pasting of copied values which were "in line" allows us to paste out, but the missing in between bits ( columns in this example ) are missed out - the ranges are put together. Co incidentally we want this output in this example
             Ws3.Range("A" & Lr22 & "").PasteSpecial Paste:=xlPasteValues
            End If
        Next Cnt
    End Sub


    or if you prefer to use a different variable for the row count in Ws3 , Lr3 , then


    Code:
    Sub Step11b() '   http://www.excelfox.com/forum/showthread.php/2421-copy-and-paste-by-VBA-based-on-criteria?p=13110&viewfull=1#post13110  http://www.excelfox.com/forum/showthread.php/2458-Copy-and-paste-the-data-if-condition-met
    Rem 1 Worksheets info
    Dim Wb1 As Workbook, Wb2 As Workbook, Wb3 As Workbook   '                           If the workbook is already open , then we can refer to it using the workbooks collection object of open workbooks,  Workbooks(" ")
     Set Wb1 = .......   Workbooks("1.xls")         ' Workbooks("1.xlsx")         '          Workbooks("sample1.xlsx")   '                                                 Set Wb1 = Workbooks.Open(ThisWorkbook.Path & "\1.xls")                ' w1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")           ' change the file path   If the workbook is already open , then we can refer to it using the workbooks collection object of open workbooks,  Workbooks(" ")
     Set Wb2 = .......     Workbooks("2.xls")         ' Workbooks("2.xlsx")         '          Workbooks("sample2.xlsx")   '                                                 Set Wb2 = Workbooks.Open(ThisWorkbook.Path & "\FundsCheck.xlsb")      ' w2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb") ' change the file path      If the workbook is already open , then we can refer to it using the workbooks collection object of open workbooks,  Workbooks(" ")
     Set Wb3 = .......
    Dim Ws1 As Worksheet, Ws2 As Worksheet, Ws3 As Worksheet ' Ws22 As Worksheet
     Set Ws1 = Wb1.Worksheets.Item(1)      '                                                                            Set Ws1 = Wb1.Worksheets("anything")  '     sheet name can be anything
     Set Ws2 = Wb2.Worksheets.Item(1)      '                                                                          ' Set Ws2 = Wb2.Worksheets("anything")
    ' Set Ws22 = Wb2.Worksheets.Item(2)
     Set Ws3 = Wb3.Worksheets.Item(2)
    Dim Lr1 As Long, Lr2 As Long, Lr As Long, Lr3 As Long
     Let Lr1 = Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Row      '   http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11466&viewfull=1#post11466      Making Lr dynamic ( using rng.End(XlUp) for a single column. )
     Let Lr2 = Ws2.Range("A" & Ws1.Rows.Count).End(xlUp).Row
    ' Let Lr = IIf(Lr2 > Lr1, Lr2, Lr1)
    Rem 2 do it
    Dim Cnt
        For Cnt = 2 To Lr2
        Dim VarMtch As Variant
         Let VarMtch = Application.Match(CStr(Ws1.Range("I" & Cnt & "").Value), Ws2.Range("B2:B" & Lr2 & ""), 0) ' We look for the string value from each row in column I of Ws1 in the range of column B in Ws2
            If Not IsError(VarMtch) Then ' If we have a match, then  Application.Match  will return an integer of the position along(down) where the match is found
            ' do nothing
            Else '  Application.Match  will return a VB error string if no match could be found
             Ws1.Range("B" & Cnt & ",I" & Cnt & "").Copy  ' if ranges are "in line" - that is to say have the same "width" ( in this example a single row width ) , then VBA lets us copy this to the clipboard
             Let Lr3 = Lr3+ 1 ' next free row in second worksheet of 2.xls
             'Ws22.Range("A" & Lr22 & "").PasteSpecial Paste:=xlPasteValues ' Pasting of copied values which were "in line" allows us to paste out, but the missing in between bits ( columns in this example ) are missed out - the ranges are put together. Co incidentally we want this output in this example
             Ws3.Range("A" & Lr3 & "").PasteSpecial Paste:=xlPasteValues
            End If
        Next Cnt
    End Sub
    Last edited by DocAElstein; 04-28-2020 at 11:18 AM.
    ….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!!

  2. #52
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Problem Solved
    Thnx Doc Sir for helping me in solving this problem Sir
    Have a Great Day Sir

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

    copy paste the data if condition matches

    Hello Everyone, I am looking for a macro plz see the details below


    Condition 1) Only If column K of sample1.xls is greater than Column D of sample1.xls & Column H of sample1.xls is Greater than column K of sample1.xls then Match Column B of sample1.xls with column B of sample2.csv & if it is there then do nothing & if it is not present then paste the column B data of sample1.xls to column B of sample2.csv


    Condition 2) Only If column K of sample1.xls is lower than Column D of sample1.xls & Column H of sample1.xls is lower than column K of sample1.xls then Match Column B of sample1.xls with column B of sample2.csv & if it is there then do nothing & if it is not present then paste the column B data of sample1.xls to column B of sample2.csv


    Plz Note
    All files are located in a different path (So in the macro the path will be hardcoded, Assume any path & I will edit the path manually by myself)
    Sheet name can be anything
    Macro will be putted in a vba.xlsm


    Plz see the file that I have attached below


    I have explained the details more deeply now, Any doubts plz ask



    http://www.vbaexpress.com/forum/show...dition-matches

    Download a csv file from below link
    https://drive.google.com/file/d/1Ec2...ew?usp=sharing




    cross post .. another !!!!
    https://www.excelforum.com/excel-pro...ml#post5342868
    Attached Files Attached Files
    Last edited by DocAElstein; 06-08-2020 at 10:05 PM. Reason: Cross post !!!!!

  4. #54
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    I think you may have screwed up again and once again given the wrong explanation….
    Quote Originally Posted by fixer View Post
    ... Only If column K of sample1.xls is greater than Column D of sample1.xls & Column H of sample1.xls is Greater than column K of sample1.xls then Match Column B of sample1.xls with column B of sample2.csv....
    … I think you mean…… Match Column I of sample1.xls with column B (Field 2) of sample2.csv

    Field 2(column B) values in your Sample2.csv comma separated values text file:
    NSE ,
    101010
    ,
    6
    , < ,
    12783
    , A , , , , , GTT
    NSE ,
    22
    ,
    6
    , < ,
    12783
    , A , , , , , GTT
    NSE ,
    17388
    ,
    6
    , < ,
    12783
    , A , , , , , GTT


    Column I of Sample1.xls
    Row\Col
    I
    1
    2
    17388
    3
    100
    4
    15083
    5
    25
    6
    22
    7





    Here is your supplied text file, Sample2.csv:
    https://excelfox.com/forum/showthrea...ll=1#post13475

    Here is the corrected explanation that I use…
    Either of these 2 conditions are to result in macro will copy value in column I of sample1.xls to field 2(column B) of text file Sample2.csv on a new line:
    Condition 1) Only If column K of sample1.xls is greater than Column D of sample1.xls & Column H of sample1.xls is Greater than column K of sample1.xls then Match Column I of sample1.xls with second field values (column B) of sample2.csv & if it is there then do nothing & if it is not present then paste the column I data of sample1.xls to append second field values (column B) of sample2.csv

    Condition 2) Only If column K of sample1.xls is lower than Column D of sample1.xls & Column H of sample1.xls is lower than column K of sample1.xls then Match Column I of sample1.xls with second field values (column B) of sample2.csv & if it is there then do nothing & if it is not present then paste the column I data of sample1.xls to append second field values (column B) of sample2.csv

    OR

    __ ( If column K of sample1.xls is greater than Column D of sample1.xls & Column H of sample1.xls is Greater than column K of sample1.xls ) or ( If column K of sample1.xls is lower than Column D of sample1.xls & Column H of sample1.xls is lower than column K of sample1.xls ) Then
    ____
    Match Column I of sample1.xls with second field values (column B) of sample2.csv & if it is there then do nothing & if it is not present then paste the column I data of sample1.xls to append second field values (column B) of sample2.csv

    Before:
    _____ Workbook: Sample1.xls ( Using Excel 2007 32 bit )
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    Exchange Symbol Series/Expiry Open High Low Prev Close LTP
    2
    NSE ACC EQ
    1295.83
    1310
    1280.25
    1270.2
    1310
    17388
    SHORT
    1308.79
    3
    NSE ADANIENT EQ
    151.5
    154.9
    150
    148.35
    155
    100
    SHORT
    153.015
    4
    NSE ADANIPORTS EQ
    329.26
    336.35
    326
    323.8
    331
    15083
    BUY
    325.967
    5
    NSE ADANIPOWER EQ
    39.6
    40
    37.75
    36.4
    39
    25
    SHORT
    39.204
    6
    NSE AMARAJABAT EQ
    636.3
    655.7
    630
    614.6
    600
    22
    SHORT
    629.937
    Worksheet: Sample1 5June

    Sample2.csv
    https://excelfox.com/forum/showthrea...ll=1#post13475
    https://excelfox.com/forum/showthrea...ll=1#post13476




    After: running macro here: https://excelfox.com/forum/showthrea...ll=1#post13478

    Sample2After.csv
    https://excelfox.com/forum/showthrea...ll=1#post13477
    Sample2After.csv : https://app.box.com/s/0j4118cwzzofe76ytb5rqkvz3qj0vseu


    NSE,101010,6,<,12783,A,,,,,GTT
    NSE,22,6,<,12783,A,,,,,GTT
    NSE,17388,6,<,12783,A,,,,,GTT
    ,100,,,,,,,,,,
    ,25,,,,,,,,,,





    Alan




    sample2.csv : https://app.box.com/s/0ej2h41g9fvm94cflf2j60a8o6p3334t
    Sample1.xls : https://app.box.com/s/xh58fgjl74w06hvsd53jriqkohdm6a3q
    macro.xlsm : https://app.box.com/s/z358r7tbc9hzthi539dlj49jsf4gyg8p
    Sample2After.csv : https://app.box.com/s/0j4118cwzzofe76ytb5rqkvz3qj0vseu
    vba.xlsm : https://app.box.com/s/juekenyll42z84j6ms7qonzsngnugoyo
    Last edited by DocAElstein; 06-07-2020 at 03:59 AM.
    ….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. #55
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Code:
    Let PathAndFileName = ThisWorkbook.Path & Application.PathSeparator & "Sample2.csv"
    Path is different, It is not in the same path when i put this
    Code:
    Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\sample2.csv")
    I got error

  6. #56
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Hi Avinash, - remember I expect you to read all my reply. I don’t expect you to understand it all and I will keep helping you further on this issue, - but you know I will not reply quickly. So use the time to read carefully and slowly all I write here for you….…



    Once again your reply makes no sense!! Your reply is nonsense.
    I still think that you have not understood anything about text files. You have understood still nothing about the .csv story…..






    Same story and reply as as here: https://excelfox.com/forum/showthrea...ll=1#post13433

    Same answer as here… https://excelfox.com/forum/showthrea...iles#post13493
    Quote Originally Posted by DocAElstein View Post
    ....This makes no sense, since you are comparing two different things. I have explained that to you in great detail many times already. ....
    ….etc… etc….




    Let me try to explain again….
    You know that code lines like this , Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\sample2.csv") , will ( try ) to open a file. It would open an excel file, no problem. It may or may not work to open a text file. Sometimes it may work. Sometimes it may not work. You must know this, because lots of people have told you all this hundreds of times already!!!

    In my macro I do not have any line like that for sample2.csv

    Why do I not have any line like that for sample2.csv?
    Because (bcoz ) . …..
    sample2.csv is a text file!!!!!!
    My macro does not open a text file with Excel
    Also this macro https://excelfox.com/forum/showthrea...ll=1#post13431 does not open a text file with Excel
    HansV does not try to open a text file with Excel
    Logit here https://chandoo.org/forum/threads/fe...2/#post-264364 , does not try to open a text file with Excel
    I do not try here http://www.eileenslounge.com/viewtop...269105#p269105 at Eileen’s Lounge to open a text file with Excel
    jindon at excelforum here https://www.excelforum.com/excel-pro...ml#post5340067 does not try to open a text file with Excel

    No one anywhere ( except you ) is trying to open a text file in Excel!!!
    Forget Opening text file in Excel. That is stupid. It can be done but only sometimes will work!!!!

    We are all not opening a text file in Excel. We are manipulating a text file using VBA. We use things like this, I already told you hundreds of times… https://www.homeandlearn.org/open_a_...le_in_vba.html - We all open a text file in/ with Excel VBA - we do not ry to open a text file in/ with Excel. To open a text file with Excel is bad, stupid, dangerous. It may not work and may cause many problems later. You have already wasted many weeks of your time and others to open a text file with Excel. It has often given you problems. You go around and around in circles and get no where because of this stupidity!!



    ( If you really want to open the text file with Excel, then you can try to do it after the problem is solved. – I already did it at the end of the macro, to get this: https://excelfox.com/forum/showthrea...ll=1#post13431
    Also, look at the end of the macro:
    Code:
    'Rem 6 Check File in Excel VBA open
    '' Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "Sample2.csv"
    ' Workbooks.Open Filename:=ThisWorkbook.Path & Application.PathSeparator & "Sample2After.csv" ' CHANGE TO SUIT
    End Sub
    If you want to open the text file with Excel then do like this:
    Code:
    Rem 6 Check File in Excel VBA open
    ' Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "Sample2.csv"
     Workbooks.Open Filename:=ThisWorkbook.Path & Application.PathSeparator & "Sample2After.csv" ' CHANGE TO SUIT
    End Sub
    OR like this:
    Code:
    Rem 6 Check File in Excel VBA open
    ' Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "Sample2.csv"
    ' Workbooks.Open Filename:=ThisWorkbook.Path & Application.PathSeparator & "Sample2After.csv" ' CHANGE TO SUIT
    Dim Wb As Workbook
     Set Wb = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\sample2.csv")
    End Sub
    )

    But that all has nothing to do with the issue of this Thread
    If you must, just for fun, you can try to open the file in Excel only at the end of the macro , after the macro has solved the issue.
    The macro solves the issue. It solves the problem. But it does not need to open a text file with Excel – it solves the problem by manipulating a text file using VBA



    Let me try again to explain….
    My macro solves your problem. It answers your question. It works perfectly.
    But, my macro needs to know where ( the path ) to text file is……
    This code line tells it where the text file is
    Code:
     Let PathAndFileName = ThisWorkbook.Path & Application.PathSeparator & "Sample2.csv"  ' ' CHANGE TO SUIT                             ' "sample2 ef 5 June.csv"                                                     From Avinash  : https://excelfox.com/forum/showthread.php/2505-copy-paste-the-data-if-condition-matches?p=13470&viewfull=1#post13470   sample2 ef 5 June.csv : https://app.box.com/s/0j4118cwzzofe76ytb5rqkvz3qj0vseu
    Question:
    Quote Originally Posted by fixer View Post
    .....
    Path is different, It is not in the same path ......
    Answer: ' CHANGE TO SUIT
    Code:
     Let PathAndFileName = "C:\Users\WolfieeeStyle\Desktop\sample2.csv" 
    ( Forget this .._
    Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\sample2.csv")
    _.. that is nonsense and has nothing at all to do with any of the issues in your original question!!!! )




    I cant reply again here for a few hours. So take your time to read all I have written, before any replies…


    Alan
    Last edited by DocAElstein; 06-07-2020 at 10:35 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!!

  7. #57
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Getting Error
    Attached Images Attached Images

  8. #58
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    Please upload the text file ( the file with .csv at the end of its name) that you are using.
    ( You can remove some of the first lines if you want - I only need to have the last few lines - (But I need a file, not a screenshot) )
    ….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!!

  9. #59
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    I already uploaded all the files& I am runing the macro on the same file which I uploaded here

  10. #60
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,313
    Rep Power
    10
    I re downloaded, and re ran the macro (Sub VBAAppendDataToTextFileLineBasedOnTheTextFileAndEx celFileConditions() : https://excelfox.com/forum/showthrea...ll=1#post13478 )

    This is the file I used: https://app.box.com/s/0ej2h41g9fvm94cflf2j60a8o6p3334t , downloaded from post #1 , https://excelfox.com/forum/showthrea...ll=1#post13470

    I don’t get that error. So I am not sure what the problem is.
    Try again.
    If you still get that error, let me know, then I will think again, and maybe try something else.
    ….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!!

Similar Threads

  1. Replies: 26
    Last Post: 09-26-2020, 05:56 PM
  2. Copy paste data based on criteria
    By analyst in forum Excel Help
    Replies: 7
    Last Post: 01-13-2014, 12:46 PM
  3. Replies: 8
    Last Post: 10-31-2013, 12:38 AM
  4. Replies: 2
    Last Post: 09-18-2013, 12:30 AM

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
  •