Easiest way to link multiple cells to a combobox selection - vba

I have a combo box with 100 item numbers. I want my user to be able to select a item number, and have multiple cells from a table input into cells on a different worksheet. I could create a massive if/then statement but that would be exhausting. I was hoping someone knew of a more elegant solution.
For example, I could write a nested if/then statement like this:
If ItemNum.Value = "1001" Then
Sheets(10).Range("A2").Value = Sheets(11).Range("F2").Value
Sheets(10).Range("A3").Value = Sheets(11).Range("F3").Value
Sheets(10).Range("A4").Value = Sheets(11).Range("F4").Value
Sheets(10).Range("A5").Value = Sheets(11).Range("F5").Value
elseif ItemNum.Value = "1002" Then
Sheets(10).Range("B2").Value = Sheets(11).Range("G2").Value
Sheets(10).Range("B3").Value = Sheets(11).Range("G3").Value
Sheets(10).Range("B4").Value = Sheets(11).Range("G4").Value
Sheets(10).Range("B5").Value = Sheets(11).Range("G5").Value
Etc. 100 times

You don't need VBA. Use Excel formulas: look into MATCH/INDEX, in paticular.

Related

MS Access Retrieve Values from ListBox

UPDATE: 3/20/2019
After subthread conversation (below), I renamed the ListBox. ItemsSelected property WORKS. Value still returning NULL int he code
This is my first time dealing with multi-select lists in Access. I have a report form with some dropdowns, checkboxes, and a listbox. The listbox contains almost 70 items - Let's call them "Cities".
The ListBox allows multiple selections. In VBA, I'm taking each of the parameters - from the other controls on the form, to create a giant WHERE condition that feeds my report.
The problem is that Access is not reading the values selected from the ListBox. When I step through that line of code, the value is NULL.
So far:
Dim s As Variant
s = Me.City.Value & ""
This is where I know I wrong-turned, but, not having dealt with a multi-select ListBox before, I don't know the syntax to get the values read.
Next: Check for whether or not values are selected in List "s":
If s <> "" Then
Check for other parameters in the current WHERE condition. IF none exist, THEN
If c.WhereCondition = "" Then
c.WhereCondition =
Set WHERE Condition by comparing List values (which are Strings) to the Yes/No values of equivalent fields in Source table.
I have to compare the List values to the 70 fields in the table - to pull out those records that match.
No, there's not 1 field - say Cities, with 70 possible values. Instead, each of the 70 possible Cities is its own Yes/No field. I inherited this DB. It's how it was built.
Currently, my attempt at this looks like:
c.WhereCondition = "( City1 = -1 OR City2 = -1 OR City3 = -1 OR .....)
`IF there are parameters in the current WHERE clause, THEN compare values in List to Source table, AND APPEND result to WHERE condition with "AND"
ELSE
c.WhereCondition = c.WhereCondition & " AND (City1 = -1 OR City2 = -1, OR ...)
End If
End If
I hope I was able to explain this well enough. The 1st problem is getting the values read. I won't know if my attempt at comparison is right or wrong without that.
THIS took a LOT of breadcrumbs to get me here!
Solution:
Dim s As Variant
Dim i As Integer
Dim ctl As Control
Set ctl = Me.Counties
If ctl.ItemsSelected.Count <> 0 Then
For Each s In ctl.ItemsSelected
t.WhereCondition = ctl.ItemData(s) & " = -1"
Next s
End If
I had to rename the control from County to Counties. Looks like the former was part of the report, and screwing everything else up. I did this after initially deleting & re-adding the control.
The comments here really helped. I just needed to figure out how to work with the properties in order to get what I wanted.
I have to compare 70 yes/no fields to the data, pulling out only those that return True. Hence the -1.
It compiles. It runs. Fingers crossed for data accuracy.
Thanks!

If cell is blank then make variable equivalent to any value

I have a workbook with 3 List Boxes. Each List Box is populated by a list that is created, each List Box contains a "Blank" data point. I am trying to write a script that goes:
For lpRows = 1 to 100
For lpColumns = 1 to 8
If ListBox1 = "" and ListBox2 = "Val" And ListBox3 = "Var" Then
Sheets(RndmSheet).Cells(lprows,lpcolumns) = Sheets(DiffSheet).Cells(lprows,lpColumns)
else
end if
However, my code isn't working because VBA is simply searching for a blank cell, where I would prefer it to return any value that contains both ListBox2 and ListBox3, but Listbox1 would be irrelevant.
Is there an easy way to use a wildcard to achieve what I want or will I need to simply make a rather large nested if statements to handle this?
I just added in an if statement
if sheets(rndmsheet).cell(lpRows,lpColumns) = "" then
VarCell = Sheets(rndmsheet).cell(lprows,lpcolumns)
end if
Sorry if that doesn't really make sense.

Insert tables into word via vb.net

I'm making an addon for word. It must be possible to insert tables. It shall be possible to specify the dimensions and location. When I insert the first table it works fine, but if I insert another table, then the first table get removed and the new one inserted. I am still pretty new to vb.net so, the code may not be the best.
With Globals.WordAddIn.ActiveDocument.Tables.Add(Globals.WordAddIn.ActiveDocument.Range, 1, 1)
.TopPadding = 0
.BottomPadding = 0
.LeftPadding = 0
.RightPadding = 0
.Rows.WrapAroundText = True
.Rows.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage
.Rows.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage
.Rows.HorizontalPosition = dobHorizontal
.Rows.VerticalPosition = dobVertical
.Rows.Height = dobHeight
.Columns.Width = dobWidth
End With
Assuming you're using the code above for adding both tables (possibly in a loop) I think the issue is that you're overwriting the first table with the second one since you use the same range.
The documentation for Tables.Add says:
The range where you want the table to appear. The table replaces the
range, if the range isn't collapsed.
If you change the first line of your code from:
With Globals.WordAddIn.ActiveDocument.Tables.Add(Globals.WordAddIn.ActiveDocument.Range, 1, 1)
to something like
dim range = Globals.WordAddIn.ActiveDocument.Range;
With Globals.WordAddIn.ActiveDocument.Tables.Add(range, 1, 1)
And then after you added your first table you do:
range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
It should let you add both tables.
However, if you add two tables right after each other I think Word combines them into one table, so you need to add some space in between, for example by using something like:
range.InsertParagraphAfter();
range.Collapse(Word.WdCollapseDirection.wdCollapseEnd); ' need to collapse again to avoid overwriting
I think it might work.

Excel VBA Percentile using Application Function with multiple criteria

I have the below formula working fine over large ranges of data in Excel 2007.
In all cases Range1, Range2 and ArrayRange are the same start and end rows.
Working:
=PERCENTILE(IF(((Range1=Value)*(Range2=Value2)),ArrayRange),0.9)
When I'm updating the formula within a macro, I can't seem to figure out the correct way to formulate the above using VBA.
Can anyone assist with the below?
Not Working:
90thPercentile = Application.Percentile(((Range1 = Value) * (Range2 = Value2), ArrayRange), 0.9)
90thPercentile = Application.Percentile(If(Range1 = Value,IF(Range2 = Value2, ArrayRange))), 0.9)
many thanks
Nik
You can't use arrays like that in VBA. You'd have to use Evaluate and pass the formula string:
90thPercentile = activesheet.evaluate("PERCENTILE(IF(((Range1=Value)*(Range2=Value2)),ArrayRange),0.9)"

Find and count the number of occurrences in a sheet

I need to find out how many times a sequence of numbers occurs in a sheet using VBA. For example:
201-1-55-8799
301-5-55-8799
202-1-55-8799
201-1-55-8799
999-5-55-8799
001-2-55-8799
I want to find out how many times 201-1 occur in this sheet. When you do FindAll in Excel it tells you at the bottom how many cells found.
I have experimented with CountIf but that only works if the cell just contains exactly 201-1.
The answer to the above search should be 2 instances of 201-1 found.
Then I need to write the number of occurances in a differnt sheet.
Thanks
Use the COUNTIF formula
=COUNTIF(A2:A12,"201-1*")
You could do this, using VBA
Dim tab_input as Variant
tab_input = ~your range~
specific_counter = 0
For i = 1 to Ubound(tab_input)
If Left(tab_input(i,1),5) = "201-1" Then
specific_counter = specific_counter + 1
End If
Next
msgbox specific_counter
That will count how many cells that have left text starting with 201-1 and and show in a box the amount.
I would recommend using Excel's own find function programmatically. Something like this:
http://www.ozgrid.com/VBA/find-method.htm