Selecting all rows with a certain text within a certain range

NichoD

Board Regular
Joined
Jul 31, 2022
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello, I am creating a userform where depending on which option selected in the combobox different ranges should be selected. The ranges are taken from a long list with columns from A to E.
A is filled with dates.
It looks something like this:

Skärmavbild 2022-08-02 kl. 15.15.45.png

If i am choosing option "Hello" in the combobox, I want rows with the text "hello" in the range B5:E8 to be selected, that is from the last row A is filled including all rows with "hello".
Down below is my current code. Do you have any suggestions?





Dim lrA As Long
Dim lrB As Long
Dim rng As Range

lrA = Cells(Rows.Count, "A").End(xlUp).Row
lrB = Cells(Rows.Count, "L").End(xlUp).Row
Set rng = Range(Cells(lrA, "B"), Cells(lrB, "L"))


Dim rngB As Range
Dim cell As Range
Dim col As Range
Dim copiedRange As Range
Dim r As Integer

Set col = Cells(Rows.Count, "B").End(xlUp)
r = 0
Set rngA = Range("B:B", col)

If cbDep.Text = "Hello" Then

For Each cell In rngA
If cell.Value = "Hello" Then
If r = 0 Then
Set copiedRange = cell.EntireRow
r = 1
Else
Set copiedRange = Union(copiedRange, cell.EntireRow)
End If
End If

Next cell

If r = 1 Then
copiedRange.Copy

End If
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Your code is somewhat confusing. It would be easier to help if you could upload a copy of your file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. This way we can see what your userform and code look like. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data.
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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