Creating a textbox to trigger a VBA formula - vba

Recently I created Macros which searched through column B using a text match and populated column H with "Y" or "N" depending on whether there was a match or not. The code used was as follows.
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("H2:H" & lRow).FormulaR1C1 = "=IF(C[-6] = ""Commodities Ags/Softs"", " & _
"(IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y""," & _
"(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y""," & _
"(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y""," & _
"(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y""," & _
"(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")"
Range("H2:H" & lRow).Select
Selection.Copy
ActiveSheet.Range("H2:H" & lRow).PasteSpecial xlPasteValues
I had to write 7 different macros because of the 7 possible matching texts in column B and the data I was matching it to comes from 7 different sources. I.E. If I received data from Commodities Ags/Softs, I would run the Commodities Ags/Softs macro (the other macros are identical, just swapping the text).
Now I've been told the data will be expanding to 70 different sources with 70 potential matching texts, rendering my specific macro to specific data approach pretty useless.
I was wondering if anyone knows how I could generalise the macro and in doing so, create a textbox which would tell the macro what text to match in column B.
Basically, I was hoping that if I received data from a particular source, I could run the macro, a textbox would appear in excel and whatever I typed into it would be the text I'm trying to match in column B, effectively altering the generalised macro.
Any help would be greatly appreciated, I'm new to VBA,
Cheers!

Basically:
Dim sMatch as String
sMatch = InputBox("Enter match data")
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
With .Range("H2:H" & lRow)
.FormulaR1C1 = "=IF(C[-6] = """ & sMatch & """, " & _
"(IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y""," & _
"(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y""," & _
"(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y""," & _
"(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y""," & _
"(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")"
.Value = .Value
End With

Related

Automated tool for VLOOK UP

I need to perform many VLookup in a set of excel files, and for that I built a tool that can make automated VLookup from a file to another but I need to be make it scalable and adaptable.
For that, I want to input in some cells of the tool (which is an Excel file) the parameters for the VLookup:
Position of Key Column
Position of "Returned Value" Column
Number of Columns in the range
Do you know how to change my tool in order for it to include these entry parameters ?
a sample of the code here:
For myrow = 3 To lastrow
Range("b" & myrow).FormulaR1C1 = _
"=VLOOKUP(RC[-1], Input!C[-1]:C[2],2,FALSE)"
Next myrow
I'm not well versed in R1C1 notation, but if you were using regular .Formula notation:
The following code assumes that A1 = Key column, A2 = Returned value column, A3 = Number of columns in the range (which is really just your return column).
lastrow = 10 just for the example
Also note - you must be missing a field... since you should have 4 variables - key column for first parameter, 2 column letters for second parameter, and the number of columns for 3rd parameter.
Sub Test()
lastrow = 10
For myrow = 3 To lastrow
'Range("B" & myrow).Formula = "=VLOOKUP(" & Range("A1").Value & myrow & ",Input!$" & Range("A1").Value & ":$" & Range("A2").Value & "," & Range("A3").Value & ",FALSE)"
Debug.Print "=VLOOKUP(" & Range("A1").Value & myrow & ",Input!$" & Range("A1").Value & ":$" & Range("A2").Value & "," & Range("A3").Value & ",FALSE)"
Next myrow
End Sub
Values on ActiveSheet:
Immediate window returns:

Create visible formula through VBA

My challenge is to code a visible formula into cells that end-users can read the reference. Highlighted yellow line of code causes error "Application Defined or Order defined error". The requirement is that in cells are simple formula like below, which takes first number from workbook where actual result will be and other one comes from different workbook. Actual code locates in a third excel.
=5/78
Variables
Private AR As New Dimension
Private UR As New Dimension
UR.KeySheet --> Sheet 1
AR.KeySheet --> DivederNumbers
UR.Wb --> myWorkbook.xlxs
AR.Wb --> myOtherWorkbook.xlxs
.
Dim test As String
If AR.Wb.Sheets(AR.KeySheet).Cells(Cell3.Row, Cell2.Column) > 0 Then
test = AR.Wb.Path & "\" & AR.Wb.Name
'**THIS LINE CAUSES ERROR:
UR.Wb.Sheets("RESULT").Cells(Cell1.Row, Cell1.Column).Formula = _
"='" & UR.KeySheet & "'!" & Cells(Cell1.Row, Cell1.Column).Address & "/" _
& "'" & [test] & AR.KeySheet & "'!" & Cells(Cell1.Row, _
Cell1.Column).Address(External:=True)
Exit For
End If
You can't reference a sheet like that. AR.KeySheet is only valid as part of the Sheets() collection.
Here is a solution for referencing the sheet name: Get a worksheet name using Excel VBA
You would need to use:
"='" & Application.Caller.Worksheet.Name & "'!" & Cells(Cell1.row, Cell1.column).Address & "/" _

How to apply vlookup on the different workbook range?

I am trying to apply VLOOKUP to get the data from a workbook named LPDwith sheet RAS(Offshore) having my lookup range. I want to look for the column A which is present in a different workbook named fresher with sheet as DestSh.So I want the results after applying VLOOKUP in the sheet DestSh in column z.
I have tried
iSCount = 2
For Each cell In DestSh.Range("Z2:Z" & lstRowofIpSheet)
With DestSh
sFormula = "=VLOOKUP(DestSh!$A" & iSCount & ",'" & sLPDFileName & "]RAS(Offshore)'!$B:$F,5,false)"
.Range("Z" & iSCount).Formula = sFormula
iSCount = iSCount + 1
End With
Next cell
But I am getting the error as: Application defined or Object defined error.
Any help or suggestions shall be highly appreciated.
In this line:
sFormula = "=VLOOKUP(DestSh!$A" & iSCount & _
",'" & sLPDFileName & "]RAS(Offshore)'!$B:$F,5,false)"
Missing an opening square bracket:
sFormula = "=VLOOKUP(DestSh!$A" & iSCount & _
",'[" & sLPDFileName & "]RAS(Offshore)'!$B:$F,5,false)"

Excel VBA trying to use a cell to store a variable calculation

I don't know very well how to word this so I apologize if this doesn't make a whole lot of sense :)
I want to be able to place the information for a variable into a cell in excel and then reference that in VBA. This is part of a find and replace macro I've created, so i am going to be looping through many rows. I want to put the data in a cell so that I can expand on what I loop through via the excel sheet.
this is the sample code that is successful:
Dim VarArray(1 To 3) As String
For i = 1 to 3
VarArray(i) = "ABC"
Next
Set rng = Range("D:D")
Varlookups = Application.WorksheetFunction.CountIf(rng, "<>")
For j = 1 To 3
For k = 2 To Varlookups
Findtext = "take " & VarArray(j) & " tablet"
Replacetext = "insert " & VarArray(j) & " tablet"
Columns("B").Replace What:=Findtext, Replacement:=Replacetext, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Next
Next
I would like to be able to manually place these pieces in cells within excel:
Cell D2 = "take " & VarArray(j) & " tablet"
Cell E2 = "insert " & VarArray(j) & " tablet"
And then change my VBA formula to reference those cells:
Findtext = Range("D2").value
Replacetext = Range("E2").value
I've tried using evaluate with no success. I am really drawing a blank on how to accomplish this. Any help provided would be greatly appreciated. :)
You have to mention the range value from which sheet like this..
Findtext = Sheets("Sheet1").Range("D2").Value
Replacetext = Sheets("Sheet1").Range("E2").value
Sheets("Sheet1").Range("D2").Value = "take " & VarArray(j) & " tablet"
Sheets("Sheet1").Range("E2").Value= "insert " & VarArray(j) & " tablet"
Hope this helps you :)

Run-time error '1004' when applying formula to Range.FormulaR1C1 in VBA

I'm trying to apply a big nested formula to a range using the code below. Basically, if the value in cell A of the active row exists in the column A of another workbook and if the cell in column E of the active row is not empty, I want the active cell to display the cells to display the value of the equivalent cell in a separate workbook.
This needs to be applied to several worksheets so I'm using the variables lrow (which is an int with the last row of the active worksheet in workbook#1) and tlrow (which is an int equal to the last row of the active worksheet in workbook#2). When I step through the sub, these variables both return the numbers I would expect them to.
Likewise, this is inside of a for loop so I also use Worksheets(i).Name where I is an int.
When I run the code, I get the run-time error "'1004': Application-defined or object-defined error".
I'm assuming it's a syntax issue.
Code:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & _
"'!A15:D" & tlrow & ",3,FALSE)),""0"",VLOOKUP(RC1,'[temp.xlsx]" & _
Worksheets(i).Name & "'!A15:D" & tlrow & ",3,FALSE))))"
Try using this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IF(ISERROR(VLOOKUP(RC1," & _
Worksheets(i).Range("A1:D" & lrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)),""0"",VLOOKUP(RC1," & _
Worksheets(i)..Range("A1:D" & tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & _
",3,FALSE)))"
What version of Excel are you running? In more recent versions you can use the Iferror function in this formula to really chop down the size.
It would be something like this:
Range("B15:B" & lrow).FormulaR1C1 = _
"=IF(OR(RC1="""",RC5=""""),"""",IFERROR(VLOOKUP(RC1," & " & Worksheets(i).Range("A1:D" & _
tlrow).Address(ReferenceStyle:=xlR1C1, External:=True) & ",3,0),""0"")"
Thanks for your help. I was able to resolve the problem by defining my vlookup range in a Range variable and then inputting the variable name in L42's equation in place of
worksheets(i).Range("A1:D" & lrow)
Really apprecaite the responses! Thanks again.