Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

search data based on dropdown

0

hi

I desighned  this  simple  macro  but   doesn't  work  well  . it  should  bring  the  data  based on cell G5   

I  know   to  can do that  by  formula   ,  but  my  real  data  about  2200 rows   and  then  it  takes  from me  more  time  to  pull down .  also  the  macro  is  better  than  the  formula  especially  when  every  time  increase  data .  I  don't  have  to  pull down  the  formula . I put  the  expected  result  when search  based on cell G5 .

Sub getData()

Dim cell As Range

Dim sh As Worksheet

Dim lRow As Integer

Set sh = ActiveSheet

For Each cell In sh.Range("B4:B" & sh.Cells(Rows.Count, "B").End(xlUp).Row)

    lRow = sh.Cells(Rows.Count, "F").End(xlUp).Row + 1

    If cell.Value = sh.Cells(5, 7).Value Then

        sh.Cells(lRow, "F").Value = cell.Offset(0, -1).Value

        sh.Cells(lRow, "G").Value = cell.Offset(0, 1).Value

        sh.Cells(lRow, "H").Value = cell.Offset(0, 2).Value

    End If

Next cell

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, [G5]) Is Nothing Then

    Call getData

End If

End Sub

any  help.   it  would  truly  appreciate 

Answer
Discuss

Answers

1
Selected Answer

Hellow Alaa,

The code below avoids looping through all entries in your data because that would take too long. Instead, it applies a filter, copies the filtered data to a temporary location to the right of the data in the sheet, removes the column you don't want, and then copies the remaining data to the result table. Any previous result is deleted. In the cleanup, the filter is removed and the temporary copy of filtered result deleted.

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 276

    Const OutputRng As String = "F8"    ' change to suit

    Dim Rng         As Range            ' range to filter / display result
    Dim TmpRng      As Range            ' blank range for temporary use

    If Not Intersect(Target, [G5]) Is Nothing Then
        With Application
            .EnableEvents = False       ' prevent change to call this procedure
            .ScreenUpdating = False     ' freeze the monitor
        End With

        With ActiveSheet
            Set Rng = .Range(OutputRng)
            .Range(Rng, .Cells(.Rows.Count, Rng.Column).End(xlUp)).Resize(, 3).ClearContents
            If .AutoFilterMode Then .Cells.AutoFilter
            With .UsedRange
                Set TmpRng = Cells(1, .Column + .Columns.Count + 1)
            End With

            Set Rng = .Range("A3").CurrentRegion
            ' Field 2 = column B
            Rng.AutoFilter Field:=2, Criteria1:=.Cells(5, "G").Value
            Rng.Offset(1).SpecialCells(xlCellTypeVisible).Copy Destination:=TmpRng
            .Columns(TmpRng.Column + 1).Delete

            .Cells.AutoFilter
            With TmpRng.CurrentRegion
                .Copy Destination:=.Worksheet.Range(OutputRng)
                .Columns.EntireColumn.Delete
            End With
        End With

        With Application
            .EnableEvents = True            ' re-enable worksheet events
            .ScreenUpdating = True          ' update the display
        End With
    End If
End Sub

The code is all in the event procedure. A working copy is included in the attached workbook. Your previous code in Module1 is not used by this solution.

Discuss

Discussion

thanks
but  your  code has  a problem  when  select   cus1  it  doesn't  show  all data  
it's  repeated four  times , but  it  shows  three times .
all  of  the  CUS   shows  three times  but  I have  more  than three times
Alaa (rep: 28) Jul 4, '21 at 4:14 am
Yes, indeed. This happens because some of the copied lines are pasted to rows that are hidden by the filter. Therefore the filter must be released before copying the extracted result. I have moved the line of code that does this and posted the amended code and workbook above.
Variatus (rep: 4889) Jul 4, '21 at 6:32 am
thanks  very  much  for  this  solution 
Alaa (rep: 28) Jul 4, '21 at 6:42 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login