Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: copy and paste by VBA based on criteria

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

    copy and paste by VBA based on criteria

    all files are located same place

    vba is placed in a seperate file sample1.xlsm

    copy all the data from column A TO COLUMN H complete data from sample2.xls and paste it to sample3.xlsb in same columns

    save and close the sample2.xls and sample3.xlsb

    i need to do the same by vba so plz help me in slving this problem sir

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.youtube.com/watch?v=SIDLFRkUEIo&lc=UgzTF5vvB67Zbfs9qvx4AaABAg
    https://www.youtube.com/watch?v=v_1iqtOnUMg&lc=UgxLtKj969oiIu7zNb94AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
    https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg
    https://www.youtube.com/watch?v=ITI1HaFeq_g&lc=Ugx12mI-a39T41NaZ8F4AaABAg.9iDQqIP56NV9iFD0AkeeJG
    https://www.youtube.com/watch?v=vXyMScSbhk4&lc=Ugxa2VYHMWJWXA6QI294AaABAg. 9irLgSdeU3r9itU7zdnWHw
    https://www.youtube.com/watch?v=tPRv-ATUBe4&lc=UgzFkoI0n_BxwnwVMcZ4AaABAg
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugz0Uy2bCSCTb1W-0_14AaABAg.9htChVuaX9W9htG01cKBzX
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg. 9ht16tzryC49htJ6TpIOXR
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgwMKwGZpDjv7vi7pCx4AaABAg
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=Ugw6UrV69zpeKvLOeOV4AaABAg. 9ht16tzryC49htOKs4jh3M
    https://www.youtube.com/watch?v=LuAipOW8BNQ&lc=UgxVW-am20rQ5GFuJ9F4AaABAg
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 10-24-2023 at 02:23 PM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hi,


    To copy all columns is not usually good to do. In whole columns you maybe do not heed all rows

    But this is what you asked for

    vba is placed in a separate file sample1.xlsm
    File is attached : FileAttatchedToPost.jpg : https://imgur.com/xFLyqLQ

    copy all the data from column A TO COLUMN H complete data from sample2.xls
    Columns("A:H").Copy


    paste it to sample3.xlsb in same columns
    I recommend using the Range PasteSpecial Method
    http://eileenslounge.com/viewtopic.p...=34112#p264458
    http://www.eileenslounge.com/viewtop...=25002#p193871
    https://docs.microsoft.com/en-us/off...e.pastespecial


    You can chose the way you want to Paste, and formats
    https://docs.microsoft.com/en-us/off...el.xlpastetype
    .PasteSpecial Paste:= xlPasteValuesAndNumberFormats
    .PasteSpecial Paste:= xlPasteFormats
    .PasteSpecial Paste:= xlPasteColumnWidths




    save and close the sample2.xls and sample3.xlsb
    You can
    Save
    Or
    Save As

    I did do macro recording to check syntax:
    http://www.excelfox.com/forum/showth...ll=1#post12531

    You can just Save That is enough in your case.
    ( You can also SaveAs )

    Code:
    Sub CopyAllColumns()
    ' copy all the data from column A TO COLUMN H complete data from sample2.xls
     Workbooks("sample2.xls").Worksheets.Item(1).Columns("A:H").Copy '
    ' paste it to sample3.xlsb in same columns
     Workbooks("sample3.xlsb").Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
                                                                                                 'Workbooks("sample3.xlsb").Worksheets.Item(1).Range("A1:H65536").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
     Workbooks("sample3.xlsb").Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteFormats
     Workbooks("sample3.xlsb").Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteColumnWidths
     
    ' save and close the sample2.xls and sample3.xlsb   '   http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=12531&viewfull=1#post12531
     Workbooks("sample2.xls").Save
    ' or
     Workbooks("sample2.xls").SaveAs Filename:=ThisWorkbook.Path & "\sample2.xls", FileFormat:=xlExcel8
    
     Workbooks("sample3.xlsb").Save
    ' or
     Workbooks("sample3.xlsb").SaveAs Filename:=ThisWorkbook.Path & "\sample3.xlsb", FileFormat:=xlExcel12
    
     Workbooks("sample2.xls").Close: Workbooks("sample3.xlsb").Close
    End Sub
    

    Alan
    Attached Files Attached Files
    ….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
    Code:
    Sub STEP4()
     Dim w1 As Workbook, w2 As Workbook
     Set w1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls") ' change the file path
     Set w2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb") ' change the file path
    w1.Worksheets.Item(1).Columns("A:H").Copy
    w2.Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    w2.Save
    w2.Close
    w1.Close
    End Sub
    I made this code correct doc sir if there is an error sir

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40455&p=313035#p313035
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312889#p312889
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312886#p312886
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312752#p312752
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312734#p312734
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312727#p312727
    https://www.eileenslounge.com/viewtopic.php?f=18&t=40411&p=312724#p312724
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40374&p=312535#p312535
    https://www.eileenslounge.com/viewtopic.php?p=312533#p312533
    https://www.eileenslounge.com/viewtopic.php?f=44&t=40373&p=312499#p312499
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=Ugy_RiNN_kAqUvZ8W994AaABAg. 9xhyRrsUUOM9xpn-GDkL3o
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=Ugy_RiNN_kAqUvZ8W994AaABAg
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=UgzlC5LBazG6SMDP4nl4AaABAg
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=UgzlC5LBazG6SMDP4nl4AaABAg. 9zYoeePv8sZ9zYqog9KZ5B
    https://www.youtube.com/watch?v=jTmVtPHtiTg&lc=Ugy_RiNN_kAqUvZ8W994AaABAg. 9xhyRrsUUOM9zYlZPKdOpm
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 03-01-2024 at 02:56 PM.

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Your macro is good
    Your macro is working
    There are no errors in it.
    I did test on files enclosed , with this following macro. ( Macro is in sample1.xlsm )
    Code:
    Sub STEP4()
     Dim w1 As Workbook, w2 As Workbook
    Set w1 = Workbooks.Open(ThisWorkbook.Path & "\1.xls") ' Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls") ' change the file path
    Set w2 = Workbooks.Open(ThisWorkbook.Path & "\FundsCheck.xlsb") '  Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb") ' change the file path
    w1.Worksheets.Item(1).Columns("A:H").Copy
    w2.Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    w2.Save
    w2.Close
    w1.Close
    End Sub
    
    Alan



    1.xls : https://app.box.com/s/th2xzmkh7rnfr4qf4dho1kpgudndm073
    Attached Files Attached Files
    ….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
    Thnx Doc Sir for ur great help and for ur great guidance

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hi,

    Some extra info:
    For Copy and Paste values, is sometimes like this: , Rng1.Value = Rng2.Value better…

    To explain:-
    We already saw Sub Vixer9b() ' demo for rng.Value = rng.Value
    http://www.excelfox.com/forum/showth...ll=1#post11479
    http://www.excelfox.com/forum/showth...ll=1#post11485


    That was Rng.Value = Rng.Value – We did use .Value of Rng in two ways

    We can also do rng2.Value = rng1.Value
    ' Or
    do rng1.Value = rng2.Value


    So we have Alternative for:-
    Rng.Copy
    Rng.PasteSpecial Paste:= xlPasteValues


    It works like this:-
    We can use .Value Property two ways for any range, We can do this for Rng1 , Rng2 , Rngx …. Etc…

    Way 1 Put values in ( for example, Rng2 ) :-
    Rng2.Value = ‘ < -------------------------------- ‘ put values in Rng2

    Way 2 Get values out ( for example for Rng1 ) :-
    < -------------- = Rng1.Value ‘ Get values from Rng1

    So we can take values out of a range and put them in another different range: -
    Rng2.Value = Rng1.Value



    Macro is in
    Process.xlsm


    Code:
    Sub STEP4b() ' Rng1.Value = Rng2.Value
     Dim w1 As Workbook, w2 As Workbook
     Set w1 = Workbooks.Open(ThisWorkbook.Path & "\1.xls")                ' w1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")           ' change the file path
     Set w2 = Workbooks.Open(ThisWorkbook.Path & "\FundsCheck.xlsb") ' w2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb") ' change the file path
    Dim Rng1 As Range, Rng2 As Range
    ' If first column and first row is used , then this will work only
     Set Rng1 = w1.Worksheets.Item(1).UsedRange '  or Set Rng1 = w2.Worksheets.Item(1).Range("A1:H" & Rng1.Rows.Count & "")
     Set Rng2 = w2.Worksheets.Item(1).Range("A1:H" & Rng1.Rows.Count & "")
    
    'w1.Worksheets.Item(1).Columns("A:H").Copy
    'w2.Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
     Let Rng2.Value = Rng1.Value
    
    w2.Save
    w2.Close
    w1.Close
    End Sub


    Alan



    1.xls : https://app.box.com/s/th2xzmkh7rnfr4qf4dho1kpgudndm073
    Attached Files Attached Files
    ….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. #7
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Thnx Doc Sir for providing the Great info

  8. #8
    Senior Member
    Join Date
    Jul 2019
    Posts
    382
    Rep Power
    0
    Doc Sir i have a similar problem like this
    Code:
    Sub STEP4()
     Dim w1 As Workbook, w2 As Workbook
     Set w1 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls") ' change the file path
     Set w2 = Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\FundsCheck.xlsb") ' change the file path
    w1.Worksheets.Item(1).Columns("A:H").Copy
    w2.Worksheets.Item(1).Columns("A:H").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    w2.Save
    w2.Close
    w1.Close
    End Sub


    this code paste the data to sheet1 what i need is to paste the data in sheet2 so what i should do for the same sir plz help me sir in solving this problem sir

  9. #9
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    Hello

    Worksheets.Item(1) is first worksheet
    Worksheets.Item(2) is second worksheet
    Worksheets.Item(3) is Third worksheet
    Workshe
    …….etc.

    First4Worksheets.JPG : https://imgur.com/v0h9CaU
    Attachment 2793

    Try
    w2.Worksheets.Item(2).Columns("A:H").Past…………
    Attached Images Attached Images
    ….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
    Thnx Alot Doc Sir i changed the same in the code but i forgot to create a second sheet
    Done Doc Sir thnx alot Sir

Similar Threads

  1. Replies: 48
    Last Post: 09-23-2020, 02:03 AM
  2. Replies: 85
    Last Post: 06-09-2020, 05:58 PM
  3. Copy paste data based on criteria
    By analyst in forum Excel Help
    Replies: 7
    Last Post: 01-13-2014, 12:46 PM
  4. Replies: 8
    Last Post: 10-31-2013, 12:38 AM
  5. 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
  •