Click event of Combobox control - excel

I have hardcopies of 3000 invoices, in which the details of the product and the name of the dealer are available. The sales data from SAP has been exported to excel for analysis. But unfortunaltely the name of the dealer is not found in the exported data against each invoice. Hence I have decided to incorporate the dealer's name against each invoice.
For this I have embedded a Combobox Control on to the worksheet containing the sales data, to which I have loaded the names of all dealers.
To the left of the Combo control in a cell say Cells(1,1) I enter the invoice number and select the corresponding dealer as mentioned in hardcopy of the invoice from the Combo control so that with the click on the name of the dealer the name gets printed against the invoice number in the next column. To do this I have written the following macro in the Combobox1_ Click event.
Sub Combobox1_Click()
For i = 5 to 3000
If cells(1,1).value = Cells(i,4).value then
Cells(i,5).value = Combobox1.Text
End if
Next
This works fine as long as I select different dealers for each click event. But when I select the same dealer consecutively twice the click event is not triggered and the name of the dealer is not printed in the second instance.
To make the point clear say for Invoice 1233 I have selected the dealer X and the name X gets printed against invoice 1233.The next invoice 1244 also belongs to dealer X and after entering 1244 in Cells(1,1) if I select the same delaer X, the name X doesnot get printed against invoice 1244.
Please advise me resolve the problem

The click event isn't being triggered because the control hasn't detected a change. You can reset the control by changing its display value either by adding this code at the end of the click event:
ComboBox1.Text = "Select Dealer"
Or change the combobox display text only when you move away from the control, by adding this event:
Private Sub ComboBox1_LostFocus()
ComboBox1.Text = "Select Dealer"
End Sub

Related

How to calculate automatically the sum in the graph that will be added new rows?

When I click the button of the user form, the new row(Row7) will be added exactly under the graph(A2:E6), then goes again and again.
On the columns G, if column b is Debit Quantity * Rate will be sown, and if Credit it will be - value.
=IF(B2="Debit",-D2E2,D2E2)
In this case, column G7 should be filled by 144(D7*E7) automatically.
・What I want to do is that
When I click the button, Column G will also calculate automatically, not click the lower right and pull every time.
Is that possible to do it inside the Excel sheet? Or Set as a click event?
To be safe, I also put the click event codes below the photo
Public Sub btnAddtransaction_Click()
MsgBox "Example"
Dim newTransaction As New Transaction
newTransaction.TransactionDate = Now()
newTransaction.CryptCurrency = cmBoxCurrency.Value
newTransaction.Quantity = txtboxQuantity.Value
newTransaction.ExchangeRate = txtboxExchangerate.Value
If (optCredit) Then
newTransaction.TransactionType = Credit
End If
If (optDebit) Then
newTransaction.TransactionType = Debit
End If
save2 newTransaction
End Sub
Can we set this as a table?
Highlight Contiguous Area to be set as table - insert tab - table
Adding a New Row will bring formula with it

Find name of selected item in matrix and show in a list box by vba

i have a matrix (6*6) in sheet1 like below, and Inside its cells, I've written the countifs() formula for the number of correct items, Now i want a Listbox for Returning the name of items from sheet2
for example: by right-click in the cell (K10) Listbox show the name (USER 1)
sheet2.database;
matrix:
Create an ActiveX listbox on the page keeping the data (your matrix);
Access its Properties and set the ColumnCount to 6;
Set its ListFilRange as your 'matrix' address (A2:F7, for instance);
Now access its code (View Code in Developer Tab) and select your list box name from the upper left corner;
Then select ListBox_Change event from the top left corner;
Fill the next code inside it (supposing that your listbox name would be ListBox1):
Private Sub ListBox1_Change()
MsgBox ListBox1.Value
End Sub
Take care to exit Design Mode (from Developer Tab).
Now, changing the line in the listbox, a message with the first column content, for the selected range will appear...

Validating a text box of a User Form

I have two sheets named "In-house inventory" and "Maintenance Details". I am using a User Form to feed data into the Maintenance Details datasheet. There is a combo box to select the material type used for the maintenance and a text box to enter the quantity of the material used. I want to generate an error message if the material quantity entered by the user is greater than the available quantity of the material in the inventory. (For example, inventory has 15L of Lubricant oil, if the user selects Lubricant oil from the combo box and enter the quantity as 20L, I want to generate an error message saying that the Entered material quantity is greater than the available quantity in the inventory.)
This is what I did and this generates an error message for every value entered to the quantity text box.
I am very new to VBA and any help would be highly appreciated.
Thanks a lot in advance.
Private Sub MaterialQuantityTextBox_AfterUpdate()
If MaterialQuantityTextBox.Value > Application.WorksheetFunction.VLookup(Me.InhouseMaterialComboBox, Sheet6.Range("B:D"), 3, False) Then
MsgBox " Quantity is greater than the quantity available in the Inventory. Enter a valid quantity"
End If
End Sub

Copy data from rows in Worksheet to another worksheet based on quantity selected

I'm a Graphic Designer by trade and while I know basic Excel this is getting into foreign territory for me.
So I have been tasked with putting together our company's product price list (with +800+ products that are all broken into categories/tabs) into a version that users can select quantities per item and see a running list on the page of their total. No problem, I have this figured out.
Each item has a column for a part number, description, price, and quantity...with the quantity defaulted to zero. I need to get this so that if user wants an item they can change the quantity to whatever they want, and press the add/update purchase order button and have this values of that item's row add to a separate sheet. Additionally, if they were to change value to zero and press the button again, it would remove the item.
So right now each worksheet in the book is like this:
Ideally it would be great to just have one macro/button that once a user enters whatever quantity of item(s) they want, it would copy the row (but as column-order G B C F H) to a sheet in that book named "Purchase Order". If the user changes a quantity of an item to 0, and reclicks the button, it removes said row from the other worksheet. This would need to work through the 30+ other tabs, and append the rows below already entered values as the user continues to add products to their purchase order (starting on B22 on the Purchase Order worksheet)
My concerns are adding/deleting products in the future/other staff having to update this and not needing to do crazy changes to the macro if, say I need to add 5 more products to a tab.
I will take ANY help, insight, direction, or guidance anyone can shed on this issue. I greatly appreciate it!
Ok I don't know if I understood well your question, but here's what I'm thinking: You could create 2 buttons on the Worksheet, one with a big "+" on it, the other with a "-" and link them to 2 different subroutines. Those items could be positioned at the end or beginning of a product row. Right click on the buttons, go to properties and be sure that those are dependant on cell position and size. The "+" one could check if your "Purchase Order" is currently open (in case it isn't, could create a new one), and add all the content on the same row in the new worksheet (you can use Application.Caller to address the buttons and .Top / .Left to address its row/column). Naturally you'll want to search if the name of the product is already in the "Purchase Order", in that case increase quantity, otherwise copy all the row. Same thing with the "-" button eliminating the product row if quantity is 0. The fun part: you can create 2 buttons and the macros, and then copy the buttons for each row, since they each address rows on their right/left!
EDIT:
Sub Add()
Dim Button_cell_row As Integer
Dim ws As Worksheet, Target As Worksheet
Dim ProductName As String
Dim i As Integer
Set ws = ThisWorkbook.ActiveSheet
Set Target = Workbooks("Purchase_Order.xls").worksheets(1) ' Here you need to choose the file (Workbook) and the Worksheet (Tab) you want
Button_cell_row = ws.Shapes(Application.Caller).TopLeftCell.Row
Do Until IsEmpty(Target.Cells(i,4)) ' for example your product name is in the 4th column
If Target.Cells(i,4) = ProductName Then
Target.Cells(i,7) = cInt(Target.Cells(i,7)) + 1 ' say that the quantity is stored in the 7th cell
Exit Do
End If
i = i+1
Loop
If IsEmpty(Target.Cells(i,4)) Then ' meaning that the whole sheet has been searched but there's no product with a similar name on the whole "i" loop-range
' in this case "i" is now the index of your first empty row
ws.Range(Cells(Button_cell_row,3), Cells(Button_cell_row,8)).Copy _ ' For example you need to copy from 3rd column to 8th column...
Destination:=Target.Cells(i,1) ' ... on the first empty row, from column 1
End If
Set ws = Nothing
Set Target = Nothing
End Sub
This wasn't tested and may not work. But you get the idea :) For the "Remove" Sub, it is gonna be very similar.

How to select next item in Data Validation list using VBA

I have a Data Validation list with dates in it. When you change the date it effects what data is shown in the rest of the worksheet.
I would like to to create 2 command buttons,
"Next" - when clicked will move to the next date in the list, when it reaches the end of the list it goes back to the beginning of the list
"Previous" - when clicked will move to the previous date in the list, when it reaches the beginning of the list it will go to the end of the list
Is this possible?
I did look at List Box and Combo Box but got highly confused with the coding!!!
Any help would be great!
You have to distinguish 3 cases:
data validation with in-line list elements (e.g. Source = "1;2;3;4;5")
data validation with list elements in a range
List/Combo box
Case 1 is maybe the most difficult, because you can access the list elements only in a string and have to split them up into an array, get the index of the current selection and move the index with two buttones in order to get the wanted result
Case 2 is a bit simpler, but again you need to somehow keep track of the current position within the range defining your dates
Case 3 is maybe the most easiest to implement ... but still requires a certain coding effort, like
load list of dates into the list/combo box before first display (OnLoad or OnActivate
create code for the UP and DOWN buttons to increment/decrement the list box index, with automatic wrap-around
I suggest a 4th case to you ... use an ActiveX Spin Button ... this provides up and down functions in one element:
create your list of dates in a vertical named range DateList
reserve one more cell for the index of the Spin Button and name it DateIndex
using Developer ribbon, insert an ActiveX Spin Button (default name is SpinButton1)
set the LinkedCell property in SpinButton1 (be sure to be in Developer / Design mode; right click the Spin button and select "properties") to DateIndex
create the following code (still in design mode, right click the SpinButton and select "view code")
code
Private Sub SpinButton1_SpinDown()
If SpinButton1 = 0 Then
SpinButton1 = Range("DateList").Rows.Count
End If
End Sub
Private Sub SpinButton1_SpinUp()
If SpinButton1 = Range("DateList").Rows.Count + 1 Then
SpinButton1 = 1
End If
End Sub
At the cell where you want to display the selected date, enter formula =INDEX(DateList,DateIndex)
since you are working with named ranges, DateList and DateIndex may be in a different sheet (even a hidden one) than the user sheet.
if you want to create a copy of the currently chosen date at the cell where the user has currently put the cursor, add the following statement to the end of the SpinDown/Up Sub's (after End If: Selection = Range("DateList").Cells(SpinButton1, 1)

Resources