Wrote a code to copy cells from columns E, F, G, C, H, I, D, from File A to columns A, B, C, D, E, K, L in File B...in the same order.
Whats the catch ?: File A has a Table around 80+ rows with column A having 5 unique values. I am trying to make the code filter the column A to select one unique value and that will be referenced from a cell range in File C (File that has code). Once filtered I need only the visible cells from the specified columns copied over to the columns in File B.
Below is my code that doesn't work for some reason...Any help is appreciated!
Whats the catch ?: File A has a Table around 80+ rows with column A having 5 unique values. I am trying to make the code filter the column A to select one unique value and that will be referenced from a cell range in File C (File that has code). Once filtered I need only the visible cells from the specified columns copied over to the columns in File B.
Below is my code that doesn't work for some reason...Any help is appreciated!
VBA Code:
Sub CopyDataToFileB()
Dim wbA As Workbook, wbB As Workbook
Dim wsA As Worksheet, wsB As Worksheet
Dim rngFilter As Range
Dim i As Long
'Set the workbooks and worksheets
Set wbA = Workbooks.Open(Environ("USERPROFILE") & "\TEST FOLDER\FileA.xlsx")
Set wsA = wbA.Worksheets("Sheet1")
Set wbB = Workbooks.Open(Environ("USERPROFILE") & "\TEST FOLDER\FileB.xlsx")
Set wsB = wbB.Worksheets("Sheet1")
'Filter the data in File A based on column F
wsA.Range("A1").AutoFilter Field:=1, Criteria1:="Sales"
'Get the filtered range
Set rngFilter = wsA.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible)
'Copy the filtered data to File B
With wsB
'Copy the data to the destination columns
For i = 2 To rngFilter.Rows.Count
.Range("A" & i - 1).Value = rngFilter.Cells(i, 5).Value
.Range("B" & i - 1).Value = rngFilter.Cells(i, 6).Value
.Range("C" & i - 1).Value = rngFilter.Cells(i, 7).Value
.Range("D" & i - 1).Value = rngFilter.Cells(i, 3).Value
.Range("E" & i - 1).Value = rngFilter.Cells(i, 8).Value
.Range("K" & i - 1).Value = rngFilter.Cells(i, 9).Value
.Range("L" & i - 1).Value = rngFilter.Cells(i, 4).Value
Next i
End With
'Turn off the filter
wsA.AutoFilterMode = False
End Sub