Forum

COPY AND PASTE COND...
 
Notifications
Clear all

COPY AND PASTE CONDITIONALLY

10 Posts
3 Users
0 Reactions
81 Views
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 

All files are located in a different path
vba code will be placed in a macro.xlsm
i have two files 1.xls & 2.csv
check wheather column H of 1.xls is greater or lower than column D of 1.xls
if column H of 1.xls is greater than column D of 1.xls then match column I of 1.xls with column B of 2.csv & if it matches then put this symbol "<" in column D of 2.csv & copy paste the data of column K of 1.xls in column E of 2.csv
or
if column H of 1.xls is lower than column D of 1.xls then match column I of 1.xls with column B of 2.csv & if it matches then put this symbol ">" in column D of 2.csv & copy paste the data of column K of 1.xls in column E of 2.csv

save and close both the file
plz help me in solving this problem by vba1.PNGresult.PNG

 
Posted : 19/04/2020 5:47 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Sholtan,

This sounds like homework.  Have you tried to solve it yourself?  Please attach your attempt and I'll be happy to guide you.

Regards

Phil

 
Posted : 19/04/2020 9:09 am
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 

Philip Sir i dont know much about vba 

but i know how to open the file by vba  actually i am unable to write the condition in vba language so can u plz help me out here this was very tricky for me so i directly asked for help 

 
Posted : 19/04/2020 2:04 pm
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 

Sub test() Dim wb1 As Workbook, wb2 As Workbook Dim ws1 As Worksheet, ws2 As Worksheet Dim rg1 As Range, i As Long, c As Range Set wb1 = Workbooks.Open("C:UsersWolfieeeStyleDesktop1.xls") Set wb2 = Workbooks.Open("C:UsersWolfieeeStyleDesktopAlert..csv") Set ws1 = wb1.Worksheets.Item(1) Set ws2 = wb1.Worksheets.Item(1) Set rg1 = ws1.Cells(1, 1).CurrentRegion With rg1 For i = 2 To rg1.Rows.Count If .Cells(i, 8) > .Cells(i, 4) Then c = ws2.Columns(2).Find(.Cells(i, 9)) If Not c Is Nothing Then c.Offset(, 2).Value = "<" c.Offset(, 3).Value = .Cells(i, 11) End If Else c = ws2.Columns(2).Find(.Cells(i, 9)) If Not c Is Nothing Then c.Offset(, 2).Value = ">" c.Offset(, 3).Value = .Cells(i, 11) End If End If Next i End With wkb1.Close SaveChanges:=True wkb2.Close SaveChanges:=True End Sub

 
Posted : 23/04/2020 12:41 am
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 

i tried the code but i met with the error with this line c = ws2.Columns(2).Find(.Cells(i, 9))

 
Posted : 23/04/2020 12:42 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi,

Please attach all the files you are working with so I don't have to recreate them.

Thanks

Phil

 
Posted : 24/04/2020 7:53 am
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 

plz see the below attachment

 
Posted : 24/04/2020 2:11 pm
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 
  1. Sub Not_tested()
  2. Dim wkb1 As Workbook, wkb2 As Workbook
  3. Dim sBk1 As String, sBk2 As String
  4. Dim ws1 As Worksheet, ws2 As Worksheet
  5. Dim rg1 As Range, i As Long, c As Range
  6.  
  7.  
  8.  
  9. sBk1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*")
  10. sBk2 = Application.GetOpenFilename("CSV Files (*.csv), *.csv")
  11. Set wkb1 = Workbooks.Open(sBk1)
  12. Set wkb2 = Workbooks.Open(sBk2)
  13. Set ws1 = wkb1.Worksheets("1-Sheet1")
  14. Set ws2 = wkb2.Worksheets("Alert")
  15. Set rg1 = ws1.Cells(1, 1).CurrentRegion
  16. With rg1
  17. For i = 2 To rg1.Rows.Count
  18. If .Cells(i, 8) > .Cells(i, 4) Then
  19. Set c = ws2.Columns(2).Find(.Cells(i, 9))
  20. If Not c Is Nothing Then 'if match found
  21. c.Offset(, 2).Value = "<"
  22. c.Offset(, 3).Value = .Cells(i, 11)
  23. End If
  24. Else
  25. Set c = ws2.Columns(2).Find(.Cells(i, 9))
  26. If Not c Is Nothing Then 'if match found
  27. c.Offset(, 2).Value = ">"
  28. c.Offset(, 3).Value = .Cells(i, 11)
  29. End If
  30. End If
  31. Next i
  32. End With
  33. End Sub
 
Posted : 25/04/2020 7:11 am
(@leonardo1234)
Posts: 26
Eminent Member
Topic starter
 

Problem Solved 

Thnx Alot Sir for ur great help

 
Posted : 25/04/2020 7:12 am
(@Anonymous)
Posts: 0
New Member Guest
 

Thank you for sharing

 
Posted : 26/04/2020 4:45 am
Share: