Hi, all. I am Beginner in VBA. I have 2 workbook, which is book 1 & book2 and inside the file have 1 worksheet for each. I have run the code that I have attach below and it show duplicate action result in one file and the other file still remain the same (does not do the action that I have wrote in my code). May I know how can I run the code once to do the action I need for two different workbook? Thanks in advanced.
VBA Code:
'Run two file at once
Sub Try()
Dim wb1 As Workbook
Dim ws1 As Worksheet
Dim x As Long, y As Long, z As Long
Set wb1 = Workbooks("B1_Book")
With wb1.Worksheets("Sheet1")
'Trim Column E & Column B
x = .Range("B2").End(xlDown).Row
y = .Range("E2").End(xlDown).Row
z = .Range("F2").End(xlDown).Row
For a = 2 To x
.Cells(a, "B").Value = Trim(.Cells(a, "B").Value)
Next a
For b = 2 To y
.Cells(b, "E").Value = Trim(.Cells(b, "E").Value)
Next b
'Trim cell G1
Range("G1").Value = Trim(Range("G1"))
'Change cell format
Columns("N:N").NumberFormat = "@"
'Insert Column
Range("F1").EntireColumn.Insert
'Insert Name
Range("F1").Select
ActiveCell.FormulaR1C1 = "Number"
'Pair data from two column
For c = 2 To z
.Cells(c, "F").Value = Cells(c, "E") & Cells(c, "B")
Next c
'Filter and delete
.Range("A1:S1").AutoFilter 9, Array("Unfulfilled", "New"), xlFilterValues
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
.Range("A1:S1").AutoFilter 11, ""
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
.Range("A1:S1").AutoFilter 18, "Y"
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
End With
Dim wb2 As Workbook
Dim ws2 As Worksheet
Dim x1 As Long, y1 As Long, z1 As Long
Set wb2 = Workbooks("B2_Book")
With wb2.Worksheets("Sheet1")
'Trim Column E & Column B
x1 = .Range("B2").End(xlDown).Row
y1 = .Range("E2").End(xlDown).Row
z1 = .Range("F2").End(xlDown).Row
For a1 = 2 To x1
.Cells(a1, "B").Value = Trim(.Cells(a1, "B").Value)
Next a1
For b1 = 2 To y1
.Cells(b1, "E").Value = Trim(.Cells(b1, "E").Value)
Next b1
'Trim cell G1
Range("G1").Value = Trim(Range("G1"))
'Change cell format
Columns("N:N").NumberFormat = "@"
'Insert Column
Range("F1").EntireColumn.Insert
'Insert Name
Range("F1").Select
ActiveCell.FormulaR1C1 = "Number"
'Pair data from two column
For c1 = 2 To z1
.Cells(c1, "F").Value = Cells(c1, "E") & Cells(c1, "B")
Next c1
'Filter and delete
.Range("A1:S1").AutoFilter 9, "Unfulfilled"
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
.Range("A1:S1").AutoFilter 11, ""
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
.Range("A1:S1").AutoFilter 17, "Y"
.AutoFilter.Range.Offset(1).EntireRow.Delete
.AutoFilterMode = False
End With
End Sub