XL 2016 Visible Cells into Array after filter

R_da

XLDnaute Nouveau
Hello Everyone,

i'm new to vba and I want to make a filter depending on some criteria and then copy the visible cells into an array.
I made the code below but it takes even the hidden cells values.
Can anyone please help me?
Sub BOM_extract()

Dim i As Long
Dim lastline As Long
Dim code_obj As String

Set Datasheet = Sheets("Data")
Set TSheet = Sheets("Raw_data")

Datasheet.Select
Selection.AutoFilter

Datasheet.Select
Datasheet.Range("$A$2:$EI$254").AutoFilter Field:=11, Criteria1:="Table"

lastline = Datasheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1

Dim my_code_obj() As String
ReDim my_code_obj(1 To lastline)

For i = 1 To lastline

my_code_obj(i) = Range("A3").Offset(1).SpecialCells(xlCellTypeVisible).Cells(i, 1).Value

Next i

TSheet.Select
Range("B3:B" & lastline).Value = Excel.WorksheetFunction.Transpose(my_code_obj)

End Sub


it would be appreciated
 

Dranreb

XLDnaute Barbatruc
Hello.
Try this :
VB:
Sub BOM_extract()
   Dim Datasheet As Worksheet, TSheet As Worksheet, FltRng As Range, MyCodeObj(), _
      Area As Range, TbInput(), Ri As Long, Rf As Long
   Set Datasheet = Sheets("Data")
   Set TSheet = Sheets("Raw_data")
   Datasheet.Select
   Selection.AutoFilter
   Datasheet.Range("$A$2:$EI$254").AutoFilter Field:=11, Criteria1:="Table"
   Set FltRng = Datasheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible)
   ReDim MyCodeObj(1 To Datasheet.UsedRange.Rows.Count, 1 To 1)
   For Each Area In FltRng.Areas
      If Area.Rows.Count > 1 Then
         TbInput = Area.Value
         For Ri = 1 To UBound(TbInput, 1)
            Rf = Rf + 1
            MyCodeObj(Rf, 1) = TbInput(Ri, 1)
            Next Ri
      Else
         Rf = Rf + 1
         MyCodeObj(Rf, 1) = Area.Value
         End If
      Next Area
   TSheet.Select
   TSheet.[B3].Resize(UBound(MyCodeObj, 1)).Value = MyCodeObj
   End Sub
 

Dudu2

XLDnaute Barbatruc
Bonjour @R_da, @Dranreb

Here is another option based on @R_da original code.

I do not understand what were these 2 instructions for (I removed them):
Datasheet.Select
Selection.AutoFilter

If no filters on, the program crashes.

Edit: at the end, no need to transpose, Excel can deal with the 1 dimension table.

VB:
Option Explicit

Sub BOM_extract()
    Dim Datasheet As Worksheet
    Dim TSheet As Worksheet
    Dim i As Long
    Dim lastline As Long
    Dim my_code_obj() As Variant
    Dim Cell As Range

    Set Datasheet = Sheets("Data")
    Set TSheet = Sheets("Raw_data")

    'Filter Datasheet
    Datasheet.Range("$A$2:$EI$254").AutoFilter Field:=11, Criteria1:="Table"

    'Line of the last cell filtered on Datasheet column #1
    lastline = Datasheet.Cells(Rows.Count, 1).End(xlUp).Row

    'Dimension the table to the number of filtered cells
    ReDim my_code_obj(1 To Datasheet.Range("A3:A" & lastline).SpecialCells(xlCellTypeVisible).Count)

    'Copy filtered cells into the table
    For Each Cell In Datasheet.Range("A3:A" & lastline).SpecialCells(xlCellTypeVisible)
        i = i + 1
        my_code_obj(i) = Cell.Value
    Next Cell

    'Value TSheet range with table content
    TSheet.Range("B3:B" & 3 - 1 + UBound(my_code_obj)).Value = my_code_obj
End Sub
 
Dernière édition:

Dranreb

XLDnaute Barbatruc
Hello.
VB:
Sub BOM_extract()
   Dim Datasheet As Worksheet, TSheet As Worksheet, FltRng As Range, TbOutput(), _
      Area As Range, TbInput(), Ri As Long, Ro As Long
   Set Datasheet = Sheets("Data")
   Set TSheet = Sheets("Raw_data")
   Datasheet.Select
   Selection.AutoFilter
   Datasheet.Range("$A$2:$EI$254").AutoFilter Field:=11, Criteria1:="Table"
   Set FltRng = Datasheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
   ReDim TbOutput(1 To Datasheet.UsedRange.Rows.Count, 1 To 2)
   For Each Area In FltRng.Areas
      TbInput = Area.Value
      For Ri = 1 To UBound(TbInput, 1)
         Ro = Ro + 1
         TbOutput(Ro, 1) = TbInput(Ri, 1) ' A
         TbOutput(Ro, 2) = TbInput(Ri, 4) ' D
         Next Ri, Area
   TSheet.Select
   TSheet.[B3:C3].Resize(UBound(TbOutput, 1)).Value = TbOutput
   End Sub
 

Dudu2

XLDnaute Barbatruc
Bonjour,
VB:
Private Const CopyColumn1 = 1
Private Const CopyColumn2 = 4
#Const DestinationOneColumnOnly = False

Sub BOM_extract()
    Dim Datasheet As Worksheet
    Dim TSheet As Worksheet
    Dim i As Long
    Dim lastline As Long
    Dim my_code_obj() As Variant
    Dim Cell As Range

    Set Datasheet = Sheets("Data")
    Set TSheet = Sheets("Raw_data")

    'Filter Datasheet
    Datasheet.Range("$A$2:$EI$254").AutoFilter Field:=11, Criteria1:="Table"

    'Line of the last cell filtered on Datasheet column #1
    lastline = Application.Max(Datasheet.Cells(Rows.Count, CopyColumn1).End(xlUp).Row, _
                               Datasheet.Cells(Rows.Count, CopyColumn2).End(xlUp).Row)

#If DestinationOneColumnOnly Then
    'Dimension the table to the number of filtered cells * 2
    ReDim my_code_obj(1 To Datasheet.Range("A3:A" & lastline).SpecialCells(xlCellTypeVisible).Count * 2, 1 To 1)

    'Copy filtered cells into the table
    For Each Cell In Datasheet.Range("A3:A" & lastline).SpecialCells(xlCellTypeVisible)
        i = i + 1
        my_code_obj(i, 1) = Cells(i, CopyColumn1).Value
        i = i + 1
        my_code_obj(i, 1) = Cells(i, CopyColumn2).Value
    Next Cell
#Else
    'Dimension the table to the number of filtered cells
    ReDim my_code_obj(1 To Datasheet.Range("A3:A" & lastline).SpecialCells(xlCellTypeVisible).Count, 1 To 2)

    'Copy filtered cells into the table
    For Each Cell In Datasheet.Range("A3:A" & lastline).SpecialCells(xlCellTypeVisible)
        i = i + 1
        my_code_obj(i, 1) = Cells(i, CopyColumn1).Value
        my_code_obj(i, 2) = Cells(i, CopyColumn2).Value
    Next Cell
#End If

    'Value TSheet range with table content
    TSheet.Range("B3:B" & 3 - 1 + UBound(my_code_obj)).Value = my_code_obj
End Sub
 

Staple1600

XLDnaute Barbatruc
Bonjour le fil

[Pour suggestion et confort de lecture]
Tous les XLDnautes ne sont pas anglophones.
Pourquoi vous ne faites pas un peu G..gle Translate ci et là ?
Histoire d'avoir un fil "bi-langue" (une première sur XLD ;) )
[/Pour suggestion et confort de lecture]
 

Dudu2

XLDnaute Barbatruc
1613326152404.gif
@Staple1600,
Sorry, for that. FYI I don't speak English, I just speak Globish and Google does not propose this option in it's translater !
1613326191638.gif
 

Discussions similaires

Réponses
2
Affichages
689

Statistiques des forums

Discussions
312 270
Messages
2 086 684
Membres
103 370
dernier inscrit
pasval