VBA Excel 2013 - Type Mismatch when Changing Listbox Value

Dalton325

New Member
Joined
Mar 18, 2015
Messages
10
Hey everyone,

I'm running Excel 2013 on a Windows 7 64-bit.

I have to make labels for my work and one customer in particular tends to multiple labels for their product every time we make it. To save me from having to manual enter them every time, I started making a program that will fill the cells with the correct labels.

Each label has three parts. The part number, the revision level, and the quantity needed. I have a userform on open that prompts you to enter that information and then add it to a list box and enter all the labels. Because there are (7) columns, you can check a check box and have it make even rows. So if you need two and it's checked, you'll get seven. If you need 14, you'll get 14, whether it's checked or not, because it's already a multiple of 7. If it's unchecked, you'll get exact quantities.

Because I may have already cut some labels off the sheet, there is a box, preset to a full sheets worth, where you can enter the number of cells left.

When you click 'Make', it tabs to the cell that is the cumulative quantity of all the labels combined. So if there are 133 labels to a sheet and you need to make 100 labels, then it goes to the 100th cell and fills the data, working backward to the first cell.

The problem I ran into is that it works with (1) sheet worth of labels, but I don't have a way to handle any remainders. So if I have 100 labels left and I need 75 of label1 and 29 of label2, then It'll either break at 100 or you'll just end up with those 100 and you'll have to figure out how many to make when you run it again.

My thought on fixing this was to have it run each label and subtract the quantity run successfully from they quantity that the user input to make. So if they needed 75 of label1, then when it ran 75, it would change that value in the list box to (0). Then it would start running the next label. Once it reached the quantity left number, it's subtract what's left from what's there. So it would run 75 labels and set that to (0). It would run the 29 labels, but when it hits 25 labels, it'll know that there were only 100 left, so it'll subtract and end up with (4) labels remaining in the listbox. Then it'll give the user the option to print and to choose how many labels are on the next sheet you'll print on. It'll go through the process again, adding the last (4) labels. Then you can print them out.

Because it spans multiple sub routines, I declared a few public variables. Everything seems to be working, but I keep getting a type mismatch when I go to change the value of the listbox.

I ended up declaring all of these as variants, because I think that lets them be what they need to be. I made k a public variable, because that's the letter in my For/Next loop that grabs each quantity and starts making the labels. When it goes to make the labels, it knows tells the fill data subroutine that k is the row. The column is always 2.

The problem comes when it goes to change the value of row (k) and column (2). I have:

Code:
intNum = lstParts(k, 2).Value
MsgBox ("Old Value: " & intNum) 'lstParts(k, 2).Value)
lstParts(k, 2).Value = lstParts(k, 2).Value - intQty
MsgBox ("New Value: " & lstParts(k, 2).Value)

I tried the bottom 3 lines first. You can see where I commented out the last part of the first message box and tried to replace it with a variable. It breaks above that with the intNum part. intNum is a variant. lstParts(k,2).value should return the value of whatever row it's currently working with, in this case, there is only one row, so it should return the value of lstParts row(0) and column(2), which is 140. Then it should subtract intQty, which is how many were available, from that and get (7). Then it should set that row/column to (7). However, it breaks and gives me a type mismatch with the intNum part.

Thanks for any help you can provide. I know it's not as clear as it can be, but it's starting to fuzz my brain, since I've been looking at it too long. I can post all my code, but it's a descent amount and I comment large blocks out till I know I don't need them anymore, so it isn't clean.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hey everyone,

I'm running Excel 2013 on a Windows 7 64-bit.

I have to make labels for my work and one customer in particular tends to multiple labels for their product every time we make it. To save me from having to manual enter them every time, I started making a program that will fill the cells with the correct labels.

Each label has three parts. The part number, the revision level, and the quantity needed. I have a userform on open that prompts you to enter that information and then add it to a list box and enter all the labels. Because there are (7) columns, you can check a check box and have it make even rows. So if you need two and it's checked, you'll get seven. If you need 14, you'll get 14, whether it's checked or not, because it's already a multiple of 7. If it's unchecked, you'll get exact quantities.

Because I may have already cut some labels off the sheet, there is a box, preset to a full sheets worth, where you can enter the number of cells left.

When you click 'Make', it tabs to the cell that is the cumulative quantity of all the labels combined. So if there are 133 labels to a sheet and you need to make 100 labels, then it goes to the 100th cell and fills the data, working backward to the first cell.

The problem I ran into is that it works with (1) sheet worth of labels, but I don't have a way to handle any remainders. So if I have 100 labels left and I need 75 of label1 and 29 of label2, then It'll either break at 100 or you'll just end up with those 100 and you'll have to figure out how many to make when you run it again.

My thought on fixing this was to have it run each label and subtract the quantity run successfully from they quantity that the user input to make. So if they needed 75 of label1, then when it ran 75, it would change that value in the list box to (0). Then it would start running the next label. Once it reached the quantity left number, it's subtract what's left from what's there. So it would run 75 labels and set that to (0). It would run the 29 labels, but when it hits 25 labels, it'll know that there were only 100 left, so it'll subtract and end up with (4) labels remaining in the listbox. Then it'll give the user the option to print and to choose how many labels are on the next sheet you'll print on. It'll go through the process again, adding the last (4) labels. Then you can print them out.

Because it spans multiple sub routines, I declared a few public variables. Everything seems to be working, but I keep getting a type mismatch when I go to change the value of the listbox.

I ended up declaring all of these as variants, because I think that lets them be what they need to be. I made k a public variable, because that's the letter in my For/Next loop that grabs each quantity and starts making the labels. When it goes to make the labels, it knows tells the fill data subroutine that k is the row. The column is always 2.

The problem comes when it goes to change the value of row (k) and column (2). I have:

Code:
intNum = lstParts(k, 2).Value
MsgBox ("Old Value: " & intNum) 'lstParts(k, 2).Value)
lstParts(k, 2).Value = lstParts(k, 2).Value - intQty
MsgBox ("New Value: " & lstParts(k, 2).Value)

I tried the bottom 3 lines first. You can see where I commented out the last part of the first message box and tried to replace it with a variable. It breaks above that with the intNum part. intNum is a variant. lstParts(k,2).value should return the value of whatever row it's currently working with, in this case, there is only one row, so it should return the value of lstParts row(0) and column(2), which is 140. Then it should subtract intQty, which is how many were available, from that and get (7). Then it should set that row/column to (7). However, it breaks and gives me a type mismatch with the intNum part.

Thanks for any help you can provide. I know it's not as clear as it can be, but it's starting to fuzz my brain, since I've been looking at it too long. I can post all my code, but it's a descent amount and I comment large blocks out till I know I don't need them anymore, so it isn't clean.

Where did the intQty come from. I see where intNum has a value but cannot associate intQty with what you are doing there.
 
Upvote 0
Hey, you got me thinking and I went back to check my code. IntQty was how many labels it was supposed to be making pulled from elsewhere in the code, so even if it had worked, it would have basically been subtracting itself from itself and leaving you with zero. I told you my brain was fuzzy. :)

So I fixed that and changed it to subtract it from the number of labels that was left. That's probably only going to work when one label is being made in larger quantities than are left on the sheet. I'll have to compensate by having the starting number of labels as it's own variable and have it subtract the quantities of labels it's already made, but I just wanted to get it to function any way before I fixed that. Here is the new code:

Code:
intNum = intNum + lstParts.List(k, 2)
MsgBox ("Old Value: " & intNum)
lstParts.List(k, 2) = intNum - intNumLeft
MsgBox ("New Value: " & lstParts.List(k, 2))

You can see that I wasn't doing it correctly at all. When I copied and pasted a similar line from somewhere else in the program, I saw that I left out the .list after the list box name and didn't need the .value at all. So I got it working at the most basic level. I'll keep fiddling with it and let you guys know if I run into any more issues.
 
Upvote 0
Hey, you got me thinking and I went back to check my code. IntQty was how many labels it was supposed to be making pulled from elsewhere in the code, so even if it had worked, it would have basically been subtracting itself from itself and leaving you with zero. I told you my brain was fuzzy. :)

So I fixed that and changed it to subtract it from the number of labels that was left. That's probably only going to work when one label is being made in larger quantities than are left on the sheet. I'll have to compensate by having the starting number of labels as it's own variable and have it subtract the quantities of labels it's already made, but I just wanted to get it to function any way before I fixed that. Here is the new code:

Code:
intNum = intNum + lstParts.List(k, 2)
MsgBox ("Old Value: " & intNum)
lstParts.List(k, 2) = intNum - intNumLeft
MsgBox ("New Value: " & lstParts.List(k, 2))

You can see that I wasn't doing it correctly at all. When I copied and pasted a similar line from somewhere else in the program, I saw that I left out the .list after the list box name and didn't need the .value at all. So I got it working at the most basic level. I'll keep fiddling with it and let you guys know if I run into any more issues.
Good to hear,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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