I have an excel sheet like this :
Also I have a userform like this :
So if a user selects say a from side and aa from top, The form should display all items corresponding to it from 1,2,3,4 in a textbox under this combo box
ie, there will be four textboxes with values s,qr,q,eef
How can i do this??
Here's some pseudo-code that you can use as a starting point. I'm assuming that the top dropdown returns an integer from 0 to 3, and the side dropdown returns integers such as 1 for a, 2 for b, etc... Then the code will use the offset function to position the cell properly so that each textbox gets the text from the correct cell. For example, if side = 1 and top = 0 then the cell for Textbox1 will be the one offset from A2 by 1 row, 1 columns, and if side = 2 and top =1 then the cell for Textbox1 will be the one offset from A2 by 2 rows and and 5 columns. Let me know if you have questions.
Option Explicit
Sub test()
Dim r As Range, side As Integer, top As Integer
Set r = Range("A2")
Textbox1 = r.Offset(side, top * 4 + 1)
TextBox2 = r.Offset(side, top * 4 + 2)
TextBox3 = r.Offset(side, top * 4 + 3)
TextBox4 = r.Offset(side, top * 4 + 4)
End Sub
Related
In a spreadsheet, I have five columns with the following headings
"Product Code" in cell B3
"Product Description" in cell C3
"Dozens per case" in cell D3
"Cases per pallet" in cell E3
"UOM" in cell F3 (which means Unit of Measure)
On my user form, I have a combo box that the user will select the product from, a command button and a text box. The values of that combo box are populated from the spreadsheet Product Code column.
Once the user selects the product from the combo box and enters a value in the text box (called "txtbxdz") and clicks the command button, a formula will be performed.
Currently the values are hard coded into the program as shown in the formula below.
Private Sub cmdbtnPrint_Click()
Dim textValUp As Long
Dim textValDown As Long
Dim txtUOM As String
Dim txtCs As Long
Dim txtDz As Long
Case Is = "4120-5-01 (ALLERGY 180MG 5CT)"
txtDz = 2
txtCs = 200
txtUOM = "DZ"
End Select
textValUp = ((txtbxdz.Value) / txtDz / txtCs) + 0.5 - 1E-16
textValDown = ((txtbxdz.Value) / txtDz / txtCs) - 0.5 + 1E-16
End Sub
Yes, if I understand your answer correctly. If I recall correctly what a VLookup does similar to that. Let me give an example. If the user selects the 4120-5-01 (Allergy 180mg 5ct) from the combo box , an unknown line of code at this point, will loop through the product description until the code finds the product, then I guess offsets to the column that holds the values that corresponds to that product and assigns that value to the appropriate variable.
So if Product name is located in cell C3
The code will offset 1 to the right get
the value of "Dozens per case" in cell D3
and assign it to txtDz and offset it again by 1 and assigns the value in E3 to txtCs and so on.
I have the following:
A form control on sheet 3 conditioned to cell B1.
A drop-down list of data validation on sheet 1 that has 3 options (A, B, C)
A dropdown list of data validation on sheet 2 that has 2 options (C, D)
I need a macro that does the following:
If cell B1 = 1 then choose the value A from the list on sheet 1 and the value C from sheet 2.
If cell B1 = 2 then choose the value B from the list on sheet 1 and the value C on sheet 2
If cell B1 = 3 then choose the C value from the list on sheet 1 and the D value from sheet 2.
The general idea that I have reviewed is this:
Private Sub Worksheet_Change (ByVal Target As Range)
If [B1] = 1 Then
Call Macro_1
End If
If [B1] = 2 Then
Call Macro_2
End If
If [B1] = 3 Then
Call Macro_3
End If
End Sub
But I do not know how to make a macro that I choose the specific values of each list.
I want to find and sum qty value of searched id in data grid view column am using this code
Dim tqty As Double
For Each row As DataGridViewRow In dgv.Rows
If row.Cells.Item(0).Value = cmbItemCode.Text Then
tqty += row.Cells.Item(4).Value
Textbox1.text=tqty
Exit For
End If
Next
The problem is that Textbox1 shows only one top searched row value. For example
id item name qty
1 abc 4
2 xyz 10
1 abc 10
Textbox1 shows the Result only 4.
As soon as you hit the first value, you exit the for statement. Therefore you never get pass the first value. Erase the Exit For it should work.
If DataGridView2.RowCount > 1 Then
Dim tqty As Integer = 0
'if you have the other column to get the result you could add a new one like these above
For index As Integer = 0 To DataGridView2.RowCount - 1
amount += Convert.ToInt32(DataGridView2.Rows(index).Cells(2).Value)
'if you have the other column to get the result you could add a new one like these above (just change Cells(2) to the one you added)
Next
TextBox1.Text = tqty
I've a listview that dynamically populates values between two columns in this fashion:
column 1 | column 2
value1 | value 2
value3 | value 4
however I'm unable to select any values in column 2 - is there a property of the listview that's preventing me from doing this or is it the way I populate these columns? Here's my code to populate the column:
For k = 0 To UBound(tempValues)
Dim itm As New ListViewItem(tempValues(k))
If k Mod 2 = 0 Then
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
itm.SubItems.Add(tempValues(k + 1))
listview1.Items.Add(itm)
End If
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Next
Any ideas?
The closest I can see for this is to set listView1.FullRowSelect = true (I assume you have listView1.View = View.Details?)
This however will only give you full row selecting - Remember the 2nd column represents the 1st Sub Item of the listview's items.
If you want multiple columns of data, you might be better off setting listView1.View = View.Details = View.List, which will cause it to wrap a single list of items onto multiple columns when it runs out of vertical space.
Edit:
If you use listView1.View = View.List, your population would need to change to the following:
For k = 0 To UBound(tempValues)
listview1.Items.Add(new ListViewItem(tempValues(k))
Next
listview1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
But it would mean you end up with the list like so:
Value 1
Value 2
Value 3
Value 4
And if ListView was made too short to display, all these, it would wrap them:
Value 1 Value 4
Value 2
Value 3
I have a form which is 1 list view and a text box.
* list view has a total of 100 rows of data
* list view has 5 columns
* column 3 has only two possible words yes or no
I want to count the number of occurrence of the word yes in column 3
the total row can be count with this code:
''''''''''COUNT TOTAL ADMISSION''''''''''''''
Dim rowcount As Integer = 0
For Each item As ListViewItem In LVfeestatementBA_I.Items
rowcount = CInt(item.SubItems(0).Text) 'Considering every column always have some value
Next
txttotaladBA_I.Text = rowcount.ToString()
any help will be greatfull
EDIT 1
This is a school assignment. As I said my aim is to find out the number of occurrence of a word in column 3. I have database of MS access which is connected with code and provides the data for the list view. The list view has 5 columns and there are a total of 100 rows. The data in col-3 contains only three words gen, occ, and cc. Now in want to count col-3 for the words with code and show the number like (68) in textbox1
EDIT 2
I applied the function provided by thedarkspoon, but it's not showing the result. I just want the result to be shown in textbox1, ex. if total number of words are 78 then at the time of form_load it should show 78 in textbox1. I solved the problem by adding at last textbox1.text = numofyes and change variable from integer to string now its working
I did not quite understand your scenario (you have to be more clear).
Anyway, given a ListView that displays items that each have 3 subitems and we know that the third subitem will have values of either "yes" or "no" we can build a query like (using linq):
var collectionOfListViewItems = listView1.Items.Cast<ListViewItem>();
var numberOfrowsWithTheThirdSubItemTextEqualToYes = (from c in collectionOfListViewItems where c.SubItems[3].Text == "yes" select c).Count();
Without linq you could do a foreach:
var numberOfrowsWithTheThirdSubItemTextEqualToYes = 0;
foreach (ListViewItem item in listView1.Items)
{
if (item.SubItems[3].Text == "yes")
numberOfrowsWithTheThirdSubItemTextEqualToYes++;
}
Ok here you go, I made this a function but you could easily adapt this to a subroutine:
Function countyes()
'Set up a variable to count the number of yes:
Dim numofyes As Integer = 0
'Count the number of yes (Replace listview1 with the name of your listview):
For Each item As ListViewItem In ListView1.Items
'If the Yes/No is in column 3, you are looking in subitem 2:
If item.SubItems(2).Text = "Yes" Then
'Increment the variable by one if the value is yes:
numofyes = numofyes + 1
End If
Next
'Return our total number of Yes that we found:
Return numofyes
End Function
Hope this helps!