To multiply column with textbox value in vba - vba

I have textbox1 in excel sheet and list of values in column A. As textbox is user defined, I have to multiply given value with the all the values available in column A. How it can be done using command button if I am not using a form.
rng = Evaluate(rng.Address &"*TextBox1.Value")
But is giving an error #NAME? in excel.

I got the solution and it should be
Value = Textbox1.Value
rng = Evaluate("=" & rng.Address & "*" & value)
Hope it will be useful for someone
Thanks

Related

Intersection of row and column including if-else statement

So I am making a tolerance calculator for mechanical purposes. I created an Excel sheet with the specified tolerances. With this sheet Excel needs to check which column and which row the user inputs and than return the intersecting value.
Now checking the column isn't much of a problem since it exists of hole numbers. However, for the rows Excel needs to check if the value is between the value of B and C and use that row to intersect with.
My question is if it's possible to use normal Excel formula or do I have to create a macro? Does anyone know a solution?
Thanks in advance!
You might use CHOOSE to select columns D:E, F:H or I:K depending on a, b or c.
=INDEX(CHOOSE(MATCH(B2, {"a","b","c"}, 0), D6:E9, F6:H9, I6:K9), MATCH(A2, B6:B9, 1), MATCH(C2, CHOOSE(MATCH(B2, {"a","b","c"}, 0), D5:E5, F5:H5, I5:K5), 1))
The terminating nominal sizes in column C are wholly irrelevant.
You need to use the worksheet change event, to see when a user has made a change. The code below will start you off, and give you the general idea, it needs to go in the relevant sheet module, ie "Sheet1" in the VB editor:
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print "Value: " & Target.Value
Debug.Print "Row: " & Target.Row
Debug.Print "Col: " & Target.Column
Debug.Print "Col Header y: " & Cells(Target.Row, 1).Value
Debug.Print "Row Header x: " & Cells(1, Target.Row).Value
End Sub
You will need to use these values to decide if the user value is correct. You'll need to update the values of 1 for the row and columns with headers. You can then use simple operators to compare them

VBA Use one column from multi column combo box as variable

I have the following code that produces a multi column combo box in a userform showing the Client and ID. I had to produce both columns as the Client and be listed multiple times but the ID is unique.
i = 2
For Each c In Range("D2", Range("D" & Rows.Count).End(xlUp))
CB_Account.AddItem Cells(i, 4).Value & " : " & Cells(i, 3).Value
i = i + 1
Next
However I'm using the value selected filter a sheet so that I can copy client details to a new sheet and am using the selected value as a variable:
MyAccount = UF_Format.CB_Account.Value
The code is currently falling over because the value in the combobox in a concatenation of two columns. Really I need to pull through just the second value being the ID so my code can filter.
Any help would be much appreciated.
You can declare the variable MyAccount as an array, then split the combo box value using colon as the delimiter. You can then work with the second item of the array.
Sub second_value()
Dim MyAccount() As String
MyAccount = Split(UF_Format.CB_Account.Value, " : ")
Debug.Print MyAccount(1)
End Sub

Convert Excel array formula VBA

I'm trying to do "vlookup" with 2 different criteria(Column A and G values) using "Index" and "Match" functions.
and here is the line i used for the Excel command.
=INDEX(Database!A:KG,MATCH(1,(Database!A:A='TempSheet'!A2)*(Database!G:G='TempSheet'!G2),0),10)
How would I do it with VBA? It's keep giving me the error message "Compile error: Expected end of statement".
Selection= _
"=Index(DB.Range("A:KG"), Match(1, (DB.Range("A" = Temp.Range("A" & i).Value)) * (DB.Range("G" = Temp.Range("G" & i).Value)), 0), 10)"
Thanks
Every time you use a spreadsheet formula inside a VBA code, you need to precede it with <Excel.WorksheetFunction.> or <Application.WorksheetFunction.>.
For example:
Application.WorksheetFunction.Match
instead of Match only.
I have had little luck getting array formulas to work correctly via VBA, and use the 'IFERROR' as a workaround like so:
=IFERROR(VLOOKUP(A1, Database!A:Z,1,FALSE),VLOOKUP(B1, Database!A:Z,1,FALSE))
This function will attempt to match A1, and in case of a #VALUE error, it will match B1.
To get this kind of formula populated on a sheet in VBA, you can loop down your sheet using the '.formula' approach.
' get length of source data
Dim RowCount As Long
RowCount = ThisWorkbook.Sheets("Database").Cells(Rows.Count, 1).End(xlUp).Row
' now starting from row 2 to preserve headings
For i = 2 To RowCount
ThisWorkbook.Sheets("Summary").Cells(i, 1).Formula = "=IF(ISERROR(SEARCH(""ISO"",V" & i & ")),""Order type not supported"",""Transit"")"
ThisWorkbook.Sheets("Summary").Cells(i, 6).Formula = "=IFERROR(TEXT(VLOOKUP(B" & i & ",Database!A:N,7,FALSE),""dd-mmm-yyyy HH:MM AM/PM""),"""")"
Next
Not exactly what you were after, but hope it helps!

How to find the last value with specific conditions?

Sheet 1 column A has the following values (it has around 3000 records. I’ve given the below sample values). I need to find the last value of a specific text.
RVT-01
RVT-02
RVT-03
RVT-04
RVT-05
RVT-06
RHT-01
RHT-02
RHT-03
RHT-04
RHT-05
ROI-01
ROI-02
ROI-03
SWO-01
SWO-02
SWO-03
SOR-01
SOR-02
SOR-03
SOR-04
SOR-05
SOR-06
SOR-07
Using VBA code
If enter short tex in sheet1.cells(2,2) = SWO , I need the last value in sheet1.cells(2,4)=SWO-03
If I enter sheet1.cells(2,2) = RHT , I need the last value in sheet1.cells(2,4)=RHT-05
If I enter sheet1.cells(2,2) = RVT , I need the last value in sheet1.cells(2,4)=RVT-06
If I enter sheet1.cells(2,2) = SOR , I need the last value in sheet1.cells(2,4)=SOR-07
What would be the VBA code for the above process?
As Skip Intro suggested, there is no need for VBA: in Column B, put a formula like this:
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,RIGHT(TRIM(A:A),2),"") (to get the just the max number):
or
=IF(IF(LEFT(A1,3)=LEFT(A2,3),1,0)=0,A:A,"") (to get the complete contents of the cell)
Both will show you the highest values. Then you could AutoFilter that column, hiding the blanks and voila :)
Or
=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"")
will enable you to use SpecialCells in VBA to get a range that you can interrogate for the maximum values in each group, as below:
Sub test()
Dim rng As Range
Dim cell
Range("B1:B" & Range("A65536").End(xlUp).Row).Formula = "=IF(IF(LEFT($A1,3)=LEFT($A2,3),1,0)=0,NA(),"""")"
Set rng = Range(Range("B1:B" & Range("A65536").End(xlUp).Row).SpecialCells(xlCellTypeFormulas, xlErrors).Offset(0, -1).Address)
For Each cell In rng
Debug.Print cell.Address & " =" & cell.Value
MsgBox cell.Address & " =" & cell.Value
Next
End Sub
For more information on the SpecialCells magic tricks, see How to delete multiple rows without a loop in Excel VBA.

get cell values of variable cells in messagebox

I have a table with names and skills where 1 means "you dont have this skill" and 3 "you have this skill". When I change it from 1 to 3 you get a msgbox saying: are you sure you want to do this. I want the names and the skills in the msgbox, but don't know how.
so for example I change cell D12 from 1 to 3. The message should say:
Are you sure you want to change (value of cell D1 / the skill) of (value of cell A12 / the name of the person)
That's ok. But it is applicable on the active cell though, so the D1 and A12 change depending on which cell is active. How do I do this?
You can use R1C1 Reference Style. In this case you'll have to switch the settings of Excel to R1C1 Reference style:
In Office 2007, Click the Office button and click Excel Options which you will find at the end near Exit Excel.
Go to Formulas tab and under Working with formulas, Check R1C1 reference style to use it.
If you specify formula like =R[2]C[1] in A1 cell, it will return you a value from B3 cell.
The second way is to use the following formula:
=OFFSET(INDIRECT(ADDRESS(ROW(), COLUMN())),2,1)
In this case you don't have to change any settings
EDIT:
I tried your example. If you want to achieve this by using VBA it's even easier:
Dim skillValue As String
skillValue = Range(Cells(1, Selection.Column), Cells(1, Selection.Column))
Dim nameValue As String
nameValue = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 1))
If MsgBox("Are you sure you want to change " & skillValue & " of " & nameValue & "?", vbOKCancel) = vbOK Then
...............