VBA: Filter and Copy Data into Next Available Row

nickshep85

New Member
Joined
Mar 21, 2012
Messages
37
Hi all,

I have a macro which I am working on that imports a large text file into Excel. This text file then needs to be separated into two sheets "Tyres" and "Mechanical". I have got so far as to import the file, then show which rows need to be transferred to which sheets, but am stuck on how I would do this. Please can someone put me out of my misery?

Code:
Sub Transfer_Data()

'Add New Workbook
Workbooks.Add template:=xlWorksheet

      Dim mypath As String
      mypath = ThisWorkbook.Path

'Add New Sheet and Overwrite Last File
ActiveWorkbook.Sheets.Add
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs (mypath & "/ImportFile.xls")
    Application.DisplayAlerts = True
    
'Name Sheets
Sheets("Sheet1").Name = "Tyres"
Sheets("Sheet2").Name = "Mechanical"

'Add Title and Date into Both Sheets
Dim wksh As Worksheet
On Error Resume Next
For Each wksh In Worksheets
    With wksh
    .Range("A1").FormulaR1C1 = "=MID(CELL(""filename""),SEARCH(""["",CELL(""filename""))+1, SEARCH(""]"",CELL(""filename""))-SEARCH(""["",CELL(""filename""))-5)"

    .Range("B1").FormulaR1C1 = "=today()"
    .Range("B1").Select

    .Range("B1").Copy
    .Range("B1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Application.Goto Reference:="R1C1"
    .Application.CutCopyMode = False
    
    'Add Headers into both Sheets
    .Range("A2").Value = "CODE"
    .Range("B2").Value = "DESCRIPTION"
    .Range("C2").Value = "XXX"
    .Range("D2").Value = "XXX"
    .Range("E2").Value = "XXX"
    .Range("F2").Value = "XXX"
    .Range("G2").Value = "PRICE"
    .Range("H2").Value = "XXX"
    .Range("I2").Value = "XXX"
  
    End With
    
    Next wksh
    
    'Format Both Sheets
    Sheets.Select
    Range("A1:I2").Select
    With Selection
      .Font.Size = 14
      .Font.Bold = True
      .Font.Color = vbWhite
      .Interior.Color = vbBlue
    End With
    
    Range("A1").Select
    Sheets("Tyres").Activate

    'Set LastRows for both sheets
    Dim LastTyres As Long
    LastTyres = Sheets("Tyres").Cells.Find(What:="*", _
    searchdirection:=xlPrevious, _
    SearchOrder:=xlByRows).Row
        
    Sheets("Mechanical").Activate
    
    Dim LastMech As Long
    LastMech = Sheets("Mechanical").Cells.Find(What:="*", _
    searchdirection:=xlPrevious, _
    SearchOrder:=xlByRows).Row
    
    'Copy TRUE Values from PriceFile into next available row in Mechanical
    
    
End Sub

The file "PriceFile" is my imported text file which, depending on the size of the file, may be over two or more sheets. Column I in each sheet has a formula which gives a TRUE / FALSE result. For this, rows with TRUE values need to be transferred into the "Mechanical" sheet in "ImportFile". Rows with FALSE values need to be transferred into the "Tyres" sheet.

The data in the below example would be transferred into the Mechanical Sheet, my PriceFile workbook uses "XXX" just as column headers.

[TABLE="width: 576"]
<tbody>[TR]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZY08M[/TD]
[TD="align: center"]12-22.5 XZY P/W 08MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]116.05[/TD]
[TD="align: center"]116.96[/TD]
[TD="align: center"]VSV999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
</tbody>[/TABLE]

Any help in this would be much appreciated, a final solution even more so.

Thanks.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
EDIT:

Since I posted this thread I have tried to filter out my results by adding in the below code

Code:
    'Copy TRUE Values from PriceFile into next available row in Mechanical
    Workbooks("PriceFile").Activate
    
    For Each ws In Worksheets
    
    With ws
    .AutoFilterMode = False
        With Range("I2", Range("I" & Rows.Count).End(xlUp))
            .AutoFilter 1, "TRUE"
            On Error Resume Next
            .Offset(0).SpecialCells(xlCellTypeVisible).EntireRow.Copy
        End With
        
        Workbooks("ImportFile").Activate
        Sheets("Mechanical").Activate
        Range("A" & LastMech + 1).PasteSpecial (xlPasteAll)
        
        
    End With
    Next ws

This does select the correct cells from the first sheet in PriceFile and pastes them into the Mechanical tab in ImportFile, but when this should loop back to the PriceFile workbook to filter the next sheet, it filters the data in my ImportFile.

Can anyone see what I'm doing wrong here?
 
Upvote 0
Rich (BB code):
With Range("I2", Range("I" & Rows.Count).End(xlUp)
Should be:
Rich (BB code):
With .Range("I2", Range("I" & Rows.Count).End(xlUp)
 
Upvote 0
OK, next problem. When I run this, the first sheet in PriceFile starts with FALSE in Col I, the code selects Row 1 and then pastes into the next available row in ImportFile, which is not what I need. It then loops back to Sheet2 and copies the rows with a TRUE value and pastes them as it should.

Any ideas? Shall I upload the PriceFile as an attachment if it will help?
 
Upvote 0
Sheet1 is something like below:

[TABLE="width: 576"]
<tbody>[TR]
[TD="align: center"]XXX
[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[/TR]
[TR]
[TD="align: center"]100015BANDBTARM
[/TD]
[TD="align: center"]1000-15 B/DAG BTA REM TL[/TD]
[TD="align: center"]25[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]G18[/TD]
[TD="align: center"]106.58[/TD]
[TD="align: center"]170.06[/TD]
[TD="align: center"]RLT999[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]100015BANDCAS[/TD]
[TD="align: center"]10-15 BAND CASING[/TD]
[TD="align: center"]25[/TD]
[TD="align: center"]99[/TD]
[TD="align: center"]198[/TD]
[TD="align: center"]15[/TD]
[TD="align: center"]15[/TD]
[TD="align: center"]CAS999[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]100015XMINED2[/TD]
[TD="align: center"]1000-15 MICH XMINE D2 TL[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]G18[/TD]
[TD="align: center"]829.28[/TD]
[TD="align: center"]1180.45[/TD]
[TD="align: center"]ERT999[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]100015XTA[/TD]
[TD="align: center"]1000-15 MICH XTA[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]G18[/TD]
[TD="align: center"]390.72[/TD]
[TD="align: center"]530.77[/TD]
[TD="align: center"]NLT001[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]100015YOKOCAS[/TD]
[TD="align: center"]100015YOKOCAS[/TD]
[TD="align: center"]228[/TD]
[TD="align: center"]ZZ[/TD]
[TD="align: center"]ZZZ[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]5[/TD]
[TD="align: center"]CAS999[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]10001690903RIFR[/TD]
[TD="align: center"]1000-16 BKT 9090 3 RIB T/FRO[/TD]
[TD="align: center"]410[/TD]
[TD="align: center"]99[/TD]
[TD="align: center"]21C[/TD]
[TD="align: center"]78.65[/TD]
[TD="align: center"]117.97[/TD]
[TD="align: center"]AGR999[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]1000169SUPRRIB8[/TD]
[TD="align: center"]1000-16SUPR 9090 3RIBT/FR PR8[/TD]
[TD="align: center"]148[/TD]
[TD="align: center"]99[/TD]
[TD="align: center"]21C[/TD]
[TD="align: center"]66.1[/TD]
[TD="align: center"]118.95[/TD]
[TD="align: center"]AGR999[/TD]
[TD="align: center"]FALSE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZY04M[/TD]
[TD="align: center"]12-22.5 XZY P/W 04MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]58.02[/TD]
[TD="align: center"]58.93[/TD]
[TD="align: center"]VSV999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZY06M[/TD]
[TD="align: center"]12-22.5 XZY P/W 06MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]87.04[/TD]
[TD="align: center"]87.95[/TD]
[TD="align: center"]VSV999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
</tbody>[/TABLE]


And Sheet2 is like the below:

[TABLE="width: 576"]
<tbody>[TR]
[TD="align: center"]XXX
[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX[/TD]
[TD="align: center"]XXX
[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZY08M[/TD]
[TD="align: center"]12-22.5 XZY P/W 08MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]116.05[/TD]
[TD="align: center"]116.96[/TD]
[TD="align: center"]VSV999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZY10M[/TD]
[TD="align: center"]12-22.5 XZY P/W 10MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]145.06[/TD]
[TD="align: center"]145.97[/TD]
[TD="align: center"]VSV999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZY12M[/TD]
[TD="align: center"]12-22.5 XZY P/W 12MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]174.07[/TD]
[TD="align: center"]174.98[/TD]
[TD="align: center"]VSV999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZYR02M[/TD]
[TD="align: center"]12-22.5 XZY REM P/W 02MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]14.51[/TD]
[TD="align: center"]15.42[/TD]
[TD="align: center"]RHT999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZYR04M[/TD]
[TD="align: center"]12-22.5 XZY REM P/W 04MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]29.02[/TD]
[TD="align: center"]29.93[/TD]
[TD="align: center"]RHT999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZYR06M[/TD]
[TD="align: center"]12-22.5 XZY REM P/W 06MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]43.53[/TD]
[TD="align: center"]44.44[/TD]
[TD="align: center"]RHT999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZYR08M[/TD]
[TD="align: center"]12-22.5 XZY REM P/W 08MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]58.03[/TD]
[TD="align: center"]58.94[/TD]
[TD="align: center"]RHT999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZYR10M[/TD]
[TD="align: center"]12-22.5 XZY REM P/W 10MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]72.54[/TD]
[TD="align: center"]73.45[/TD]
[TD="align: center"]RHT999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
[TR]
[TD="align: center"]P122XZYR12M[/TD]
[TD="align: center"]12-22.5 XZY REM P/W 12MM[/TD]
[TD="align: center"]999[/TD]
[TD="align: center"]XX[/TD]
[TD="align: center"]R24[/TD]
[TD="align: center"]87.05[/TD]
[TD="align: center"]87.96[/TD]
[TD="align: center"]RHT999[/TD]
[TD="align: center"]TRUE[/TD]
[/TR]
</tbody>[/TABLE]

So I would need the last two rows from Sheet1 and all of Sheet 2 inserted into Mechanical on ImportFile.xls. The FALSE values from sheet1 would need to go into the Tyres tab in ImportFile.xls.

Apologies for the tables, but I was unsure of the best way to show my data.

Any help is greatly appreciated, I can feel the grey hairs multiplying by the day!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top