cycling through listboxes

SeanWise

New Member
Joined
Dec 2, 2016
Messages
17
`enter code here`Ok, so basically what this code is supposed to do is;


-start a loop**
-look up values and store them as variables
-put all variables into the 1st listbox
-loop back and
-get new variables and...
-...put in the 2nd listbox
-loop 4 times until all 4 listboxes are complete.


For simplicity I have named the listboxes ListBox1, Listbox2, ListBox3 & Listbox4.


In the With Statement, the Code gives me a 1004 error: unable to get the OLEObjects property out of the worksheet class, which means it can't find it, I guess.


Can anyone see what I'm missing?


Note:



Code:
With ActiveSheet.OLEObject("ListBox" & i).Value

also doesn't work. Is OLEObject only for ActiveX? How would I do this if I'm using form controls (if that's the problem).


Code:
   'current year predictions
    Dim ForecastYear As Double
    ForecastYear = Year(Now) + 0.1


    Dim i As Integer
    Dim Forecast As Double
    Dim wForecast As Double
    Dim wDemand As Double


    For i = 1 To 4 'loop information to place into listboxes for current year's quarterly predictions
    
            'lookup info to place into 1st row of listbox
            On Error Resume Next
                Forecast = Application.VLookup(ForecastYear, Sheets(ProdCode).Range("A1:S5000"), 10)
            On Error GoTo 0
                If IsError(Forecast) Then
                    MsgBox "couldn't find '" & ForecastYear & "' in Sheets '" & ProdCode & "'"
                    Exit Sub
                End If
            Forecast = Round(Forecast, 2)
        
            'lookup info to place into 2nd row of listbox
            On Error Resume Next
                wForecast = Application.VLookup(ForecastYear, Sheets(ProdCode).Range("A9:o5000"), 15)
            On Error GoTo 0
            
            wForecast = Round(wForecast, 2)


            'lookup info to place into 3rd row of listbox
            On Error Resume Next
                wDemand = Application.VLookup(ForecastYear, Sheets(ProdCode).Range("A9:m5000"), 13)
            On Error GoTo 0
           
            wDemand = Round(wDemand, 2)
 


                ForecastYear = ForecastYear + 0.1 'add, so that next loop finds next quarterly value
        
    '-----------------------------------------------------------------------------
                With ActiveSheet.OLEObjects("ListBox" & i).Object  'add current quarterly info to the current listbox
                    .AddItem ""
                    .AddItem Forecast
                    .AddItem ""
                    .AddItem ""
                    .AddItem wForecast
                    .AddItem ""
                    .AddItem ""
                    .AddItem wDemand
                End With
                
         Next i 'go to next listbox loop

Cheers Anyone!
 
I added false to the end if the vlookups, and there is no change, so I guess they are finding an exact match.

Interesting, When I move the code
Code:
ForecastYear = ForecastYear + 0.1 'add, so that next loop finds next quarterly value
from the very end of the loop (just before next i) to the very beginning (just after For i) , Forecast, wForecast and wDemand all return zero. So I think the problem must be with this line.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
By the way, here is a layout of the worksheet, maybe it helps? I'm finding the number 2016.1 (2016.2, 2016.3, 2016.4) in column C, and printing relevant data from the corresponding row (Ex, column J) on a userform.

A B C D E F G H I J K L M O
201212012.18.00 7.651.05
22012.210.009.651.049.659.852.083.530.788.96
32012.37.0011.0011.650.6011.658.070.6911.652.009.46
42012.415.0013.002.0013.651.1013.6518.351.3413.652.0021.03
201312013.115.0013.600.6015.650.9615.6515.811.0115.652.0017.83
22013.218.0017.804.2017.651.0217.6518.001.0217.652.0020.04
32013.313.0019.802.0019.650.6619.6513.610.6919.652.0015.00
42013.428.0022.002.2021.651.2921.6529.101.3421.652.0031.78
201412014.125.0022.400.4023.651.0623.6523.891.0123.652.0025.91
22014.226.0028.205.8025.651.0125.6526.161.0225.652.0028.20
32014.320.0028.800.6027.650.7227.6519.150.6927.652.0020.54
42014.442.0030.601.8029.651.4229.6539.851.3429.652.0042.53
201512015.131.0031.000.4031.650.9831.6531.971.0131.652.0033.99
22015.234.0038.807.8033.651.0133.6534.321.0233.652.0036.36
32015.328.0035.650.7935.6524.700.6935.652.0026.08
42015.459.0037.651.5737.6550.601.3437.652.0053.28
201612016.1 39.6540.051.0139.652.0042.07
22016.2 41.6542.481.0241.659.9352.61
32016.3 43.6530.240.6943.6510.3337.39
42016.4 45.6561.351.3445.6510.7375.76
201712017.1 47.6548.131.0147.6511.1359.37
22017.2 49.6550.641.0249.6511.5362.40
32017.3 51.6535.780.6951.6511.9344.04
42017.4 53.6572.091.3453.6512.3388.66

<colgroup><col><col><col><col><col><col><col><col><col span="6"></colgroup><tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
Upvote 0
It's definitely a problem with ForecastYear and the Vlookup.

Similarly, I have a msgbox to debug and see what ForecastYear is at the beginning of each iteration of the For-loop. It updates normally from 2016.1 through to 2016.4

Then, I tried copying out the code 4 times, removing the For-Loop completely, and changed the ForecastYear manually in each instance. It works perfectly.

So I can fall back on that, but it still doesn't explain the problem with the vlookups not updating in the loop.
 
Upvote 0
Post the updated version of your code (the one you copied out 4 times and is working) and we'll take a look it.

Also, for your information, you don't need to include "On Error Resume Next/On Error GotTo 0" with VLOOKUP. When the VLOOKUP method of the Application object is used, a non-breaking error occurs when there's no match. One only needs to check for an error using IsError, as you've done for your first lookup.
 
Last edited:
Upvote 0
Hi Domenic
I ended up replacing all my VLOOKUPS with INDEX MATCH, and now they work.

Forecast = Application.Evaluate("=INDEX('" & ProdCode & "'!J:J,MATCH(" & ForecastYear & ",'" & ProdCode & "'!C:C,0))")

Thank you for all the help.
 
Upvote 0

Forum statistics

Threads
1,216,221
Messages
6,129,585
Members
449,520
Latest member
TBFrieds

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