Error - can you see whats wrong with this - any suggestions for improvements - listbox

deadstack

New Member
Joined
Apr 26, 2016
Messages
37
hi All

The below code is giving me a mis-match error, this is something i have copied and tried to mod to fit for use

what i am trying to do is for ever row that has a -1 this is entered by another macro, i want just these rows to appear in my listbox

Code:
Private Sub UserForm_Initialize() 
 Dim wk As Workbook
 Dim ws1 As Worksheet
 
 Set wk = ThisWorkbook
 Set ws1 = wk.Sheets("Sheet1")
 
 Dim rCopy  As Range
    Dim c As Integer
    Dim r As Integer
    
    With Sheets("Sheet1").Select
         
        c = Range("a1").End(xlToRight).Column
        r = Cells(Rows.Count, c).End(xlUp).Row
        
       End With
        'Set rCopy = Range(Cells(1, 3), Cells(r, 8))
        
       For r = 1 To r
        'ws1.Cells(r, 37).Value = "-1"
        Me.ListBox1.RowSource = ws1.Cells(r, 37) = "-1" ' rCopy.Address
        
        
   
Next r




End Sub


When i have this up i want to highlight a number and i press the button below i want to fill data against this

Code:
Private Sub cmdOkay_Click()Dim wk As Workbook
Dim the_sheet As Worksheet
Dim uf6 As UserForm
Dim combob1 As ComboBox
Dim combob2 As ComboBox
Set the_sheet = Sheets("Sheet1")
Set uf6 = UserForm2
Set wk = ThisWorkbook
Set combob1 = Me.ComboBox1
Set combob2 = Me.ComboBox2




Range("AC" & (ActiveCell.Row)) = Right(combob1.Value, 4)
Range("AE" & (ActiveCell.Row)) = combob2.Value
Range("AL" & (ActiveCell.Row)) = "0"
Range("AK" & (ActiveCell.Row)).Clear


UserForm6.Hide
Call RouteVal3
     
End Sub

then re-run this again untill all the "-1" are all gone


any one got any idea's!! i'm going crazy looking at it!!!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
This will add Items in Column 1 to your list based on if Column 37 has a -1

Code:
[COLOR=#0000ff]Private Sub[/COLOR] UserForm_Initialize()


   [COLOR=#0000ff] Dim[/COLOR] r [COLOR=#0000ff]As Integer[/COLOR]
    
    Sheets(1).Activate
        
    r = Cells(Sheets(1).Rows.Count, "A").End(xlUp).Row
        
[COLOR=#0000ff]    For[/COLOR] r = 1[COLOR=#0000ff] To[/COLOR] r
    
      [COLOR=#0000ff]  If [/COLOR]Cells(r, 37) = -1 [COLOR=#0000ff]Then[/COLOR]
            Me.ListBox1.AddItem Cells(r, 1)
[COLOR=#0000ff]        End If[/COLOR]
    
    [COLOR=#0000ff]Next[/COLOR] r


[COLOR=#0000ff]End Sub[/COLOR]

Is this what your trying to do?
 
Upvote 0
kind of

im am using the list box to select a number of rows, and then using a button to apply values into the cell,

im not sure that what oyu posted is going to work for this project, though, anyother ideas??

thanks
 
Upvote 0
sorry to explain this better

1. when i call up this userform, i want it to populate with only the columns that have -1
2. i then want to be able to select multiple lines (Which has been done through the properties window
3. i have two combo boxes which i fill with information and then apply them to two different cells on each row
4. i also clear the -1
5. unload the userform
6 .load it again and the above rows will be gone and i can select and repeat till nothing is left in the listbox (because there is no "-1" left) and then move on to the next sub

anyone help based on the code above


if you haven't guessed i am a vba newb!! :)

thanks in advance
 
Upvote 0
Do you want to fill the list box with numbers signifying what row you are modifying or do you want the list box to pick up multiple columns with the (-1)....thus imitating your actual data row in full (If so how many columns have -1)? You will need to set the ColumnCount if this is the case.


Which procedure is erroring out? Does the first one work and the second one fail? or are they both not working?
 
Last edited:
Upvote 0
Thanks for the reply

sorry the -1 refers to rows not columns

yes i only want the rows with -1 on them, i have the code working now but showing all the rows

how would i rewrite the row count to do this

thanks again i never taught of row counting them !!


Code:
Private Sub UserForm_Initialize() 
 Dim wk As Workbook
 Dim ws1 As Worksheet


 Set wk = ThisWorkbook
 Set ws1 = wk.Sheets("Sheet1")


    Dim rCopy  As Range
    Dim c As Integer
    Dim r As Integer
    Dim rn As String
    
    With Sheets("Sheet1").Select
         
        c = Range("a1").End(xlToRight).Column
        r = Cells(Rows.Count, c).End(xlUp).Row
      Set rCopy = Range(Cells(1, 3), Cells(r, 8))
    
       
     End With
      
        
       For r = 1 To r
      
           
       Me.ListBox1.RowSource = rCopy.Address
         
   
Next r




End Sub
 
Upvote 0
You can't use RowSource unless you are populating the listbox from a contiguous range on a worksheet.

Are you sure Matt's code in post #2 doesn't populate, using AddItem, the listbox as required?
 
Upvote 0
If your data looks like this:


Excel 2010
ABCDEFGHI
1HeaderHeaderHeaderHeaderHeaderHeaderHeaderHeaderEvaluation
2a1b1c1d1e1f1g1h1-1
3a2b2c2d2e2f2g2h2
4a3b3c3d3e3f3g3h3-1
5a4b4c4d4e4f4g4h4
6a5b5c5d5e5f5g5h5-1
7a6b6c6d6e6f6g6h6
8a7b7c7d7e7f7g7h7-1
9a8b8c8d8e8f8g8h8
10a9b9c9d9e9f9g9h9-1
11a10b10c10d10e10f10g10h10
12a11b11c11d11e11f11g11h11-1
13a12b12c12d12e12f12g12h12
14a13b13c13d13e13f13g13h13-1
Sheet1


This code:
Code:
[COLOR=#0000ff]Private Sub[/COLOR] UserForm_Initialize()


 [COLOR=#0000ff]   Dim [/COLOR]wk [COLOR=#0000ff]As [/COLOR][COLOR=#000000]Workbook[/COLOR][COLOR=#0000ff][/COLOR]
 [COLOR=#0000ff]   Dim[/COLOR] ws1[COLOR=#0000ff] As [/COLOR][COLOR=#000000]Worksheet[/COLOR][COLOR=#0000ff][/COLOR]
 [COLOR=#0000ff]   Dim[/COLOR] rCopy  [COLOR=#0000ff]As [/COLOR][COLOR=#000000]Range[/COLOR][COLOR=#0000ff][/COLOR]
[COLOR=#0000ff]    Dim [/COLOR]c [COLOR=#0000ff]As Integer[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] r [COLOR=#0000ff]As Integer[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] cll [COLOR=#0000ff]As [/COLOR][COLOR=#000000]Range[/COLOR]


[COLOR=#0000ff]    Set[/COLOR] wk = ThisWorkbook
[COLOR=#0000ff]    Set [/COLOR]ws1 = wk.Sheets("Sheet1")


    c = Range("a1").End(xlToRight).Column
    r = Cells(Rows.Count, c).End(xlUp).Row
[COLOR=#0000ff]    Set[/COLOR] rCopy = Range(Cells(1, 3), Cells(r, 8))


       For r = 1 To r
       [COLOR=#0000ff]    If[/COLOR] Cells(r, 9) = -1[COLOR=#0000ff] Then[/COLOR]
          [COLOR=#0000ff]      With[/COLOR] Me.ListBox1
          
                    .AddItem Cells(r, 3)
                    .List(myCol, 1) = Cells(r, 4)
                    .List(myCol, 2) = Cells(r, 5)
                    .List(myCol, 3) = Cells(r, 6)
                    .List(myCol, 4) = Cells(r, 7)
                    .List(myCol, 5) = Cells(r, 8)
                    myCol = myCol + 1
                    
[COLOR=#0000ff]                End With[/COLOR]
[COLOR=#0000ff]           End If[/COLOR]
   [COLOR=#0000ff]    Next[/COLOR] r


[COLOR=#0000ff]End Sub[/COLOR]

Will give you data in your listbox that looks like this:


Excel 2010
CDEFGH
2c1d1e1f1g1h1
3c3d3e3f3g3h3
4c5d5e5f5g5h5
5c7d7e7f7g7h7
6c9d9e9f9g9h9
7c11d11e11f11g11h11
8c13d13e13f13g13h13
Sheet1



Is that what you want?
 
Upvote 0
Thanks for this

I think we are getting closer

i am getting a run time 70 error permission denied


I did change two things

1. i dim the myCol as an integer and set it to 1 for the first column, i tried it at -1 as i seen somewhere you had to start counting at that
i notice you have cll as a range, but are not using it in the code i left this

2. i changed the column to 38 in the if statement

Code:
Private Sub UserForm_Initialize() 
  Dim wk As Workbook
    Dim ws1 As Worksheet
    Dim rCopy  As Range
    Dim c As Integer
    Dim r As Integer
    Dim cll As Range
    Dim myCol As Integer


    Set wk = ThisWorkbook
    Set ws1 = wk.Sheets("Sheet1")




    c = Range("a1").End(xlToRight).Column
    r = Cells(Rows.Count, c).End(xlUp).Row
    Set rCopy = Range(Cells(1, 3), Cells(r, 8))




       For r = 1 To r
           If Cells(r, 38) = -1 Then
                With Me.ListBox1
          myCol = 1
                    .AddItem Cells(r, 3)
                    .List(myCol, 1) = Cells(r, 4)
                    .List(myCol, 2) = Cells(r, 5)
                    .List(myCol, 3) = Cells(r, 6)
                    .List(myCol, 4) = Cells(r, 7)
                    .List(myCol, 5) = Cells(r, 8)
                    myCol = myCol + 1
                    
                End With
           End If
       Next r




End Sub

any ideas??
 
Upvote 0
write i debugged this a little bit

i put -1 in the 6th cell and f8 in vba

it went down to the 6th cell and passed a 1 for myCol, then when it went to the nex line .list add it threw the error of premission error 70

edit just to note - before i moved onto that line i pointed the mouse at the cells etc and it was seeing the right infromation
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,598
Messages
6,125,748
Members
449,258
Latest member
hdfarid

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