How to perform a VB vlookup from a userform entry - vba

I am trying to create a script for a invoice, that can perform a vlookup based on the entry in a userform. I have a userform, which has a combobox with a named range. I would like four text boxes, which display the result of the vlookup.
I have a vlookup set up, which just goes into the normal cell in the worksheet. it is:
=VLOOKUP($A22,Products!$A$1:$B$1679,2,FALSE)
I would like it, so that rather than looking for cell A22, it performs the vlookup from the combobox, which is just named test. I would then like to repeat it for:
=VLOOKUP($A47,Products!$A$2:$A$1679,1,FALSE)
=VLOOKUP($A47,Products!$A$2:$A$1679,1,FALSE)
=VLOOKUP($A47,Products!$A$2:$C$1679,3,FALSE)
Thanks for reading, and I hope that you can help. If you could

Refer to the combobox control's .Value property, pseudo-code:
=VLOOKUP(UserForm1.ComboBox1.Value,Products!$A$2:$A$1679,1,FALSE)
Etc.
Actual code:
Dim lookupRange as Range
Set lookupRange = Worksheets("Products").Range("A2:A1679")
Product = Application.Vlookup(UserForm1.Selectprodcutcombo.Value‌​, _
lookupRange, 1, False)
Of course, you will need to modify the name of your form and combobox, based on your form's design.

Related

Vlookup vba automatisation

I'm a beginner in VBA programmation, I want to create a button to help me search the price of a product from column A of sheet1 and to search for the price of that product from sheet2, column D of the same workbook.
The vlookup formula I use is:
=VLOOKUP(A2;sheet2!A2:G712;4)
My issue is that I have more than 100000 products and I want to use a button to simplify the process.
Change your top row formula to =VLOOKUP(A2;sheet2!$A$2:$G$712;4), select the cell you enetered the formula into, and then drag the formula down by using the little square in the bottom right hand corner of the cell you entered the formula into.
If you want to do it entirely in VBA, you can autofill using
Dim source As Range("A1")
Dim destination As Range("A1:A10")
source.AutoFill Destination:=destination
This will autofill from A1 to A10, as an example. You can also specify the autofill type with Type:
Dim source As Range("A1")
Dim destination As Range("A1:A10")
source.AutoFill Destination:=destination Type:=xlFillLinearTrend
The default type is xlFillDefault, which tries to find a pattern automatically and use the according fill type. Other types can be looked up here.

(Excel VBA) - Draw from cell text in a macro

I'm trying to build a small macro that allows the user to format multiple different documents at once.
I would like for the user to be able to enter into a particular cell within the document containing the macro a particular piece of text.
I then want for this piece of text to be able to be drawn upon in the macro while affecting a different document.
For instance, a code to add another column might say
Worksheets(1).Range("A1").EntireColumn.Insert
Instead of specifying the column (A), I would like it to draw on a value in the host document. For instance, the user types "G" into the particular cell, and then clicks a button to run the macro, and the macro will dynamically know to affect column G in all excel documents it targets based off of the value in the host document.
I hope this makes sense.
Any suggestions for the sort of functions I should be looking at to make this work?
"Any suggestions on the sort of functions I should be looking at?"
Here's a few...
To get the value which is entered...
If the cell will always be in the same address, say A1:
' Define a string variable and set it equal to value in A1
Dim cellText as String
cellText = ThisWorkbook.ActiveSheet.Range("A1").Value
or instead of using Range you can also use Cells which takes a row and column number.
cellText = ThisWorkbook.ActiveSheet.Cells(1, 1).Value
If the cell changes then you may need to look into the Find function to look for a label/heading next to the input cell. Then you can use it (easily with Cells) to reference the input...
Once you have this variable, you can do what you like with it.
To put this value into cell B3 in another (open) workbook named "MyWorkbook", and a sheet named "MySheet" you can do:
Application.Workbooks("MyWorkbook").Sheets("MySheet").Range("B3").Value = cellText
To insert a column at cellText, do
Application.Workbooks("MyWorkbook").Sheets("MySheet").Range(cellText & "1").EntireColumn.Insert
Notably here, the & concatonates the strings together, so if
cellText="B"
then
cellText & "1" = "B1"
Further to your comment about moving values between sheets, see my first example here, but always refer to the same workbook. If you find yourself repeatedly typing something like
ThisWorkbook.Sheets("MySheet").<other stuff>
then you can use the With shorthand.
With ThisWorkbook.Sheets("MySheet")
' Starting anything with a dot "." now assumes the with statement first
.Range("A1").Value = .Range("A2").Value
.Range("B1").Value = .Range("B2").Value
End With
Important to note is that this code has no data validation to check the cell's value before using it! Simply trying to insert a column based on a value which could be anything is sure to make the macro crash within its first real world use!

Macro to find and delete column based on cell value

I have two sheets in an Excel workbook. I would like a VBA code to search for the content of cell J19 in Sheet2 and delete the column with the matching cell in Sheet1. I have been searching all around Google to look for some VBA codes, but I haven't found anything yet that works.
Does anybody here know how to do this? I have zero experience in VBA.
You could try this code to perform such a task.
Sub FindMatchAndThenDeleteColumn()
FindContent = Worksheets("Sheet2").Range("J19")
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
If Cell.Value = FindContent Then Columns(Cell.Column).Delete
Next
End Sub
Put the code above in your workbook's module. Note that FindContent and Cell are randomly chosen here, you can use any names. I use
For Each Cell In Worksheets("Sheet1").UsedRange.Cells
to loop through every cell that is in use in Sheet1. It will check everything in the range from cell A1 to the last cell with data (the bottom right-most cell). If the content of cell J19 is a text, you can declare the variable FindContent as a String type, i.e. Dim FindContent As String. If it's a number, you can declare it as a Long type or a Single type or any number type that fits the content. Here I don't declare it which means it defaulting to a Variant type. And since you're a beginner in VBA, you may learn it from Excel Easy. Hope this helps.

with a VBA userform, how do you populate a combobox with a different text and value?

I am trying to populate a VBA userform combobox with a named range, and I want the text value to display in the combobox for the user to select, but the value I want returned is the cell reference to the initial named range. That way it would point back to the named range, and if I change that text, it would automatically update all the references I have on the sheet that were entered by the form.
Right now I can get the named range to populate the combobox by iterating through the range, and using an .AddItem to give it the textual value, but then it just dumps that textual value into my spreadsheet, when instead I would like it to have the cell reference from the range that populated it.
The easiest way to do this is to create an Array(X,2) X being the number of items in the array. Then you put the String you want to display in Array(1,1) and the data you want in Array(1,2) then simply populate the combo box like normal. When you and use the array to reference which item they choose. They choose item 8, you know it is Array(8,2) If you need more help please post some code.
So, after more research it would seem that I am unable to directly accomplish what I want with the combobox. I ended up just looping through the range to fill the combobox with the textual values, and then when I submit the form, I looped through the same range of values, checked it if the submitted value matches one from the range of values, and if it does, then return the .Address instead of the .Value, and that gave me the cell reference I was looking for. Too bad the combobox doesn't work more like a real html select menu, it would have made my life a little easier, but in the end I was able to make it work, it just isn't as clean as I would have liked.

VBA change cell's value if it has a user defined style

I want to change all cells in Excel spreadsheet of specific, user defined style (let's say 'Beauty' style) to value "Beast".
Sub BulkChangeValeOfStyle()
Dim TheCell As Range
For Each TheCell In ActiveSheet.UsedRange.Cells
If TheCell.Style = "Beauty" Then
TheCell.Value = "Beast"
End If
Next
End Sub
This code is too slow. I got lots of those Beauty cells scattered round the spreadsheet. Is it possible to make it like this:
ActiveSheet.AllCellsWithaStyle="Beauty".value="Beast"
Update
This is just an idea:
ActiveSheet.Cells.SpecialCells(xlCellTypeSameFormatConditions).Activate
or alternatively
ActiveSheet.Cells.SpecialCells(xlCellTypeSameValidation).Activate
but I do not know how to set up the criteria which determine xlCellTypeSameFormatConditions. Or criteria for xlCellTypeSameValidation. Anybody knows?
I don't think it is possible with a better solution than your For Each loop. But you should create a new Style for the cells you want to modify, and modify the format properties of that style when needed like this:
ThisWorkbook.Styles.Item("Good").Interior.ColorIndex=4