Compare files & Insert row per condition VBA
Solved/Closed
j3kj3k
Posts
15
Registration date
Tuesday 1 June 2010
Status
Member
Last seen
11 July 2012
-
1 Jun 2010 à 17:07
j3kj3k Posts 15 Registration date Tuesday 1 June 2010 Status Member Last seen 11 July 2012 - 15 Jun 2010 à 18:37
j3kj3k Posts 15 Registration date Tuesday 1 June 2010 Status Member Last seen 11 July 2012 - 15 Jun 2010 à 18:37
Related:
- Vba insert row
- Saints row 2 cheats - Guide
- Insert key - Guide
- Vba find - Guide
- Vba case like - Guide
- Insert check mark in word - Guide
2 responses
rizvisa1
Posts
4478
Registration date
Thursday 28 January 2010
Status
Contributor
Last seen
5 May 2022
766
3 Jun 2010 à 18:10
3 Jun 2010 à 18:10
See if that works for you. I am presuming that all "XX" had to be replaced with "Y"
Sub FixData()
Dim SheetName1 As String
Dim SheetName2 As String
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim lRowWS1 As Long
Dim lRowWS2 As Long
Dim iTempWS1 As Integer
Dim iTempWS2 As Integer
Dim lFixRow As Long
SheetName1 = "FileA"
SheetName2 = "FileB"
Set WS1 = Sheets(SheetName1)
Set WS2 = Sheets(SheetName2)
lRowWS1 = WS1.Cells(Rows.Count, "A").End(xlUp).Row
lRowWS2 = WS2.Cells(Rows.Count, "A").End(xlUp).Row
iTempWS1 = WS1.Cells.Find("*", Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column + 1
iTempWS2 = WS2.Cells.Find("*", Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column + 1
WS1.Select
With Range(Cells(1, iTempWS1), Cells(lRowWS1, iTempWS1))
.FormulaR1C1 = "=""|*| "" & LEFT(RC1, FIND("" "", RC1, 1)-1)"
.Copy
.PasteSpecial xlPasteValues
End With
With Range(Cells(1, iTempWS1 + 1), Cells(lRowWS1, iTempWS1 + 1))
.FormulaR1C1 = "=SUBSTITUTE(RC1, ""XX"", ""Y"")"
.Copy
.PasteSpecial xlPasteValues
End With
With Range(Cells(1, iTempWS1 + 2), Cells(lRowWS1, iTempWS1 + 2))
.FormulaR1C1 = "=LEN(RC" & iTempWS1 & ")"
.Copy
.PasteSpecial xlPasteValues
End With
Range(Cells(1, iTempWS1), Cells(lRowWS1, iTempWS1 + 2)).Select
Selection.Sort _
Key1:=Cells(1, iTempWS1 + 2), Order1:=xlAscending, _
Key2:=Cells(1, iTempWS1), Order2:=xlAscending, _
Key3:=Cells(1, iTempWS1 + 1), Order3:=xlAscending, _
Header:=xlNo, MatchCase:=False
WS2.Select
With Range(Cells(1, iTempWS2), Cells(lRowWS2, iTempWS2))
.FormulaR1C1 = "=""|*| "" & RC1"
.Copy
.PasteSpecial xlPasteValues
End With
With Range(Cells(1, iTempWS2 + 1), Cells(lRowWS2, iTempWS2 + 1))
.FormulaR1C1 = "=LOOKUP(10000,SEARCH('" & SheetName1 & "'!R1C" & iTempWS1 & ":R" & lRowWS1 & "C" & iTempWS1 & ", RC" & iTempWS2 & ",1),'" & SheetName1 & "'!C" & iTempWS1 + 1 & ":C" & iTempWS1 + 1 & ")"
.Copy
.PasteSpecial xlPasteValues
End With
With Range(Cells(1, iTempWS2 + 2), Cells(lRowWS2, iTempWS2 + 2))
.FormulaR1C1 = "=IF(ISERROR(RC[-1]), """", RC[-1])"
.Copy
.PasteSpecial xlPasteValues
End With
For lFixRow = 1 To lRowWS2
If (Cells(lFixRow, iTempWS2 + 2) <> "") Then
Rows(lFixRow + 5).Insert
Cells(lFixRow + 5, 1) = Cells(lFixRow, iTempWS2 + 2)
lRowWS2 = lRowWS2 + 1
End If
Next lFixRow
Range(Cells(1, iTempWS2), Cells(lRowWS2, iTempWS2 + 2)).Clear
WS1.Select
Range(Cells(1, iTempWS1), Cells(lRowWS2, iTempWS1 + 2)).Clear
Set WS1 = Nothing
Set WS2 = Nothing
End Sub
rizvisa1
Posts
4478
Registration date
Thursday 28 January 2010
Status
Contributor
Last seen
5 May 2022
766
1 Jun 2010 à 17:30
1 Jun 2010 à 17:30
Could you please upload a sample file with sample data etc on some shared site like https://authentification.site , http://wikisend.com/ , http://www.editgrid.com etc and post back here the link to allow better understanding of how it is now and how you foresee.
j3kj3k
Posts
15
Registration date
Tuesday 1 June 2010
Status
Member
Last seen
11 July 2012
1 Jun 2010 à 18:16
1 Jun 2010 à 18:16
Here they are : Let me know if you have any issue to see the file.
http://www.editgrid.com/user/j3kj3k/FileA
http://www.editgrid.com/user/j3kj3k/FileB
http://www.editgrid.com/user/j3kj3k/FileB-RESULT
Thanks!!!!!
http://www.editgrid.com/user/j3kj3k/FileA
http://www.editgrid.com/user/j3kj3k/FileB
http://www.editgrid.com/user/j3kj3k/FileB-RESULT
Thanks!!!!!
rizvisa1
Posts
4478
Registration date
Thursday 28 January 2010
Status
Contributor
Last seen
5 May 2022
766
1 Jun 2010 à 19:50
1 Jun 2010 à 19:50
Could FileA and FileB be incorporated into same workbook as two different worksheet?
j3kj3k
Posts
15
Registration date
Tuesday 1 June 2010
Status
Member
Last seen
11 July 2012
3 Jun 2010 à 14:33
3 Jun 2010 à 14:33
Hi rzvisa1
Absolutely.... I just separated input and output data on a diffrent files just to make my question clear.
Thanks.
Absolutely.... I just separated input and output data on a diffrent files just to make my question clear.
Thanks.
j3kj3k
Posts
15
Registration date
Tuesday 1 June 2010
Status
Member
Last seen
11 July 2012
3 Jun 2010 à 18:45
3 Jun 2010 à 18:45
Thanks rzvisa1 !!!!!!!!!!!
rizvisa1
Posts
4478
Registration date
Thursday 28 January 2010
Status
Contributor
Last seen
5 May 2022
766
3 Jun 2010 à 20:27
3 Jun 2010 à 20:27
I have just updated the code. The sort was wrong. If stars and moon align, it would have given you wrong result. You should update too