Edit existing working code if certain condition is met

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,975
Office Version
  1. 2007
Platform
  1. Windows
This is the working code in use but having issues with it so having to look at an alternative way for it to work.
The issue im trying to overcome is the values that the Listbox is populated with.

On my worksheet in column C are various makes BUT i am only interested in HONDA

Also in column K are various values BUT i am only interested in BLACK, CLEAR, GREY & RED
With the 2 conditions above then load listbox

So at present with the below im loading the Listbox with values that dont to be loaded BUT im not sure how i code it so only the above takes place

VBA Code:
Private Sub UserForm_Initialize()
    Me.StartUpPosition = 0
    Me.Top = Application.Top + 15 ' MARGIN FROM TOP OF SCREEN
    Me.Left = Application.Left + Application.Width - Me.Width - 330 ' LEFT / RIGHT OF SCREEN HIGHER NUMBER MOVES FORM TO THE LEFT
    
    TextBox1.Visible = False
    TextBox1.Value = "HONDA"
    
    
  Dim r As Range, f As Range, Cell As String, added As Boolean
  Dim sh As Worksheet
  Dim i As Long
  Set sh = Sheets("MCLIST")
  sh.Select
  With ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "100;170;70;10"
    
    Set r = Range("C8", Range("C" & Rows.Count).End(xlUp))
    Set f = r.Find(TextBox1.Value, After:=r.Cells(r.Count), LookIn:=xlValues, LookAt:=xlPart)
    
    If Not f Is Nothing Then
      Cell = f.Address
      Do
         If Len(Cells(f.Row, "K").Value) <> 0 Then
          .AddItem f.Value
          .List(.ListCount - 1, 1) = f.Offset(, 1).Value ' MODEL
          .List(.ListCount - 1, 2) = f.Offset(, 6).Value ' YEAR
          .List(.ListCount - 1, 3) = f.Offset(, 8).Value ' CONNECTOR USED
          .List(.ListCount - 1, 4) = f.Row
         End If
        Set f = r.FindNext(f)
      Loop While f.Address <> Cell
      .TopIndex = 0
    End If
    End With
    
    TextBox2.Text = ActiveSheet.Range("K1").Value ' BLACK
    TextBox3.Text = ActiveSheet.Range("K2").Value ' CLEAR
    TextBox4.Text = ActiveSheet.Range("K3").Value ' GREY
    TextBox5.Text = ActiveSheet.Range("K4").Value ' RED

End Sub
 
I think we can still use the above BUT need to add an IF statement which would only apply to BLACK CLEAR GREY & RED Then i believe it will work
 
Upvote 0
Without code, textboxes, listboxes or anything else
can you just state what your end goal is
what are you trying to discover or accomplish ?
 
Upvote 0
I want to load the listbox with specific values from my sheet.

Only load these values BLACK CLEAR GREY RED that live in column K

When the listbox is loaded it should also look the same as before.
Ive currently removed column L & at present EVERYTHING in column K is being loaded when its not needed.
 
Upvote 0
Because it shows Bike models years etc & also the colour info

What do you have in mind that will show me all that i want
 
Upvote 0
I dont need to see the N/A or BUNDLE

EaseUS_2025_02_28_16_30_23.jpg
 
Upvote 0
Can we not add an IF code so IF HONDA is present in column C & either BLACK CLEAR GREY RED in column K on the same row is true etc then add to listbiox.
If C is HONDA & K is N/A or BUNDLE then do not load listbox & move to next row
 
Upvote 0
Sure you can
try changing this line
VBA Code:
         If Len(Cells(f.Row, "K").Value) <> 0 Then
to
VBA Code:
         If Cells(f.Row, "K").Value = "BLACK" Or _
            Cells(f.Row, "K").Value = "CLEAR" Or _
            Cells(f.Row, "K").Value = "GREY" Or _
            Cells(f.Row, "K").Value = "RED" Then
 
Upvote 0
Solution

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