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!!!
 
ok,

would you have any examples of how this is done, i can work away with it then,

i assume i am trying to do much at the same time i should

select the first column ie routing code

pass in the value for all the cells selected

then select pass the value for the the next column

then clear the -1 out of the other field

then reload userform

how does that sound??


that row index any examples of it


thanks again for all your help! your a gent!!
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
So let's start small....

Try fiddling around with the column width property with the data you currently have. This way you can see how it's interacting with the form.
Set values....
then change them....
Then change them with one set to 0...

Should be getting the hang of things now...

Once you see what's going on with that. Try adding the additional column you have created on the worksheet into the userform. It's in the .additem block of code that your going to look to do this. Remember you'll have to adjust the values to incorporate one additional column.

Troubleshooting from my phone at this point....sorry for the lack of examples. The good news is this should help you understand the code better and build your skill set.
 
Upvote 0
hey i got that working very well

set x as the first line at the .additem and set x = to 1 then just added to it at the end of the code block, it worked well

i also got the coloumns looking well set the first column to 40px the second to 200 px and the rest to 150px


so now i have to write the code to paste in values in cells that i have selected

how can we use x for this though
 
Upvote 0
Data Like this:


Excel 2010
ABCDEFGHI
1IDHeaderHeaderHeaderHeaderHeaderHeaderHeaderTRUE?
21A1B1C1D1E1F1G1-1
32A2B2C2D2E2F2G2
43A3B3C3D3E3F3G3
54A4B4C4D4E4F4G4-1
65A5B5C5D5E5F5G5
76A6B6C6D6E6F6G6-1
87A7B7C7D7E7F7G7
98A8B8C8D8E8F8G8-1
109A9B9C9D9E9F9G9
1110A10B10C10D10E10F10G10-1
1211A11B11C11D11E11F11G11
1312A12B12C12D12E12F12G12-1
1413A13B13C13D13E13F13G13
1514A14B14C14D14E14F14G14-1
1615A15B15C15D15E15F15G15
1716A16B16C16D16E16F16G16-1
Sheet1


Code Like this:

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


   [COLOR=#0000ff] Dim [/COLOR]lstLp [COLOR=#0000ff]  As Integer[/COLOR]
  [COLOR=#0000ff]  Dim[/COLOR] myID    [COLOR=#0000ff]As String[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] fndID [COLOR=#0000ff]  As[/COLOR] Range


   [COLOR=#0000ff] With [/COLOR]Me.ListBox1
     [COLOR=#0000ff]  For [/COLOR]lstLp = 0 To .ListCount - 1
      [COLOR=#0000ff]    If[/COLOR] .Selected(lstLp) = True [COLOR=#0000ff]Then[/COLOR]
[COLOR=#008000]                'Define ID[/COLOR]
                myID = .List(lstLp, 0)
[COLOR=#0000ff]                Exit For[/COLOR]
[COLOR=#0000ff]          End If[/COLOR]
   [COLOR=#0000ff]    Next[/COLOR] lstLp
[COLOR=#0000ff]    End With[/COLOR]


   [COLOR=#0000ff] Set [/COLOR]fndID = Columns(1).Find(myID, , xlValues, , xlByRows, xlNext, False) 'This finds the ID in Column A and Sets You up to update your values....
    
[COLOR=#008000]    'Then you just need to update them like this[/COLOR]
    fndID.Offset(, 2) = "MyValue" [COLOR=#008000]'Updates the Value B10 on the spreadsheet two rows to the right of my ID of 10.....[/COLOR]
    fndID.Offset(, 8) = 0[COLOR=#008000] 'Replaces -1 with 0[/COLOR]
[COLOR=#0000ff]    [/COLOR]
[COLOR=#0000ff]End Sub[/COLOR]


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


 [COLOR=#0000ff]   Dim [/COLOR]wk [COLOR=#0000ff]As [/COLOR]Workbook
   [COLOR=#0000ff] Dim[/COLOR] ws1 [COLOR=#0000ff]As [/COLOR]Worksheet
  [COLOR=#0000ff]  Dim [/COLOR]rCopy  [COLOR=#0000ff]As [/COLOR]Range
 [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]Range


[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))


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

Column Count = 7
MultiSelect = fmMultSelectSingle
 
Upvote 0
thanks for this

have put this in i am aving some issues

i am getting a run time error 424 on this line

Code:
[COLOR=#333333]fndID.Offset(, 2) = "MyValue" [/COLOR]

it only completes 1 line and it is pasting onto the line below it when it works
 
Upvote 0
What column is your ID in?

After myID is established.... See what the value is:

Code:
Debug.Print myID

Is it what you expect?
After the fndID is established see what the cell address is:
Code:
Debug.Print fndID.Address

Is it what you expect....

Sounds like something isn't jiving based on your description. Those would be my first guesses as to what's going wrong.
 
Upvote 0
Ok so a little more debugging on this

it will work on the first line of the listbox but then paste the values into the correct columns on the second row ( I dont know if it is looking at the second row its self or its just set to it)

when i try it on the second or third row it throws an object error

when i debug print these on the third line the Myid var is 3 and the finID is 10508393
 
Upvote 0
Try changing this line:
Code:
    Set fndID = Columns(1).Find(myID, , xlValues, xlByRows, xlNext, False)
To this:
Code:
    Set fndID = Columns(1).Find(myID, , xlValues,[COLOR=#ff0000][B] xlWhole[/B][/COLOR], xlByRows, xlNext, False)
I think your getting hits on partial matches.

check the Address for fndID not the value. Since fndID is a range.... it will have an address like $A$1. This should validate if the code is finding the correct row for your updates.
 
Upvote 0
i am getting an run time error 91 now object variable or block variable not set

it happens on the debug print line of fndID = when i hover the mouse it says fndID = Nothing

edit sorry that is with the new line of code, this was happening on the old line as well
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,613
Messages
6,125,834
Members
449,266
Latest member
davinroach

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