0
Hello! I am going through great difficulty with a problem using Vlookup in Excel VBA. First of all, I am not an expert in VBA but I managed to use Vlookup but I am not having the effect I would like. I will explain below in more detail the situation with images:
- I have a table that associates letters to numbers in a separate spreadsheet. I did this because I need to quantify letters and then be able to compare them.
- With this table, I intend to allow the user, in the main spreadsheet, to enter values that will correspond to codes that contain a combination of numbers and letters (Ex: 91V). And then, with a macro, choose a code value (Ex: 89H). Thus, I would like to create a macro that would delete table rows that in this code have numbers larger than the chosen and letters with smaller values. Example of the inserted table:
- I was trying to gather information and put together a code that allows the user to enter a value (Ex: 91V) uses Vlookup to look for the value of V. But I don’t go much further than that.
Could you help me?
EDIT:
On request, this was the code I had until then. However, it was a work in process that logically is not even close to what I need.
Sub DeletarIndices()
indice = InputBox("Digite o Valor Desejado", "Teste")
Set planilhaV = Sheets("IV")
Dim vValores As String
sResult = Application.VLookup(indice, planilhaV.Range("A2:B11"), 2)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim i As Long
For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
If Not (Range("A" & i).Value > sResult) Then
Range("A" & i).EntireRow.Delete
End If
Next i
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
EDIT 2
My code is now like this:
Option Explicit
Sub DeletarIndices()
Dim indice As String ' To hold our user input letter
Dim indiceValue As Long ' To hold the numeric value of our user input letter
Dim rowLetter As String ' To hold the current row letter
Dim rowLetterValue As Long ' To hold the numeric value of the current row letter
Dim firstRow As Long ' First row of your data
Dim lastRow As Long ' Last row of your data
Dim currentRow As Long ' Current row for your loop counter
Dim sht As Worksheet ' To hold the worksheet you're working on
Dim planilhaV As Worksheet ' To hold your lookup worksheet
Set sht = ThisWorkbook.Worksheets("Plan1") ' Use the name of your worksheet
Set planilhaV = ThisWorkbook.Worksheets("IV") ' As in your original example
firstRow = 1
lastRow = sht.Range("A" & Rows.Count).End(xlUp).Row
indice = UCase(InputBox("Digite o IC/IV Desejado", "Teste")) ' Convert user input to upper case and store
indiceValue = CLng(Application.VLookup(indice, planilhaV.Range("A2:B11"), 2, False)) ' Creating numeric indice value with lookup table
For currentRow = lastRow To firstRow Step -1
rowLetter = UCase(Right(sht.Range("A" & currentRow).Value, 1)) ' Get letter from code in column A, converted to upper case
rowLetterValue = CLng(Application.VLookup(rowLetter, planilhaV.Range("A2:B11"), 2, False)) ' Creating numeric value for current row letter with lookup table
If rowLetterValue < indiceValue Then ' Compare the numeric letter values, and if smaller than user input...
sht.Rows(currentRow).EntireRow.Delete ' Delete the row
End If
Next currentRow
End Sub
Now I would just need a help to adapt this code with an increment. I need to allow the user to enter not just a letter, but a code (Ex: 91T). To finish, I need that when inserting the example "91T", the code excludes from the table all lines that include Minor Letters and Larger Numbers.
Add the code you put in. Vlookup is PROCV in ptbr. A tip for those who do not know much of VBA is to make the function work while writing a macro, so you can better understand how excel works.
– Celso Marigo Jr
Hello Celso. I added the code until then. However in VBA, Vlookup is used. And what I’m looking for is very complex to do via macro recording.
– Leon Freire
Hello Leon. Your question is a bit confusing because of the examples. In the main table you exemplify (first image) there is no 91V code. Will this code be there or will it be a concatenation of "91" + "V" (the "code" being "V")? Now, assuming that this is the case, what does he do after he finds "the value of V"? What is this value for in selecting or deleting the original data? It’s not clear.
– Luiz Vieira
Hello @Luizvieira! I will try to pass you a step-by-step of what should happen. -- 1. The client inserts a code after running the macro (Ex: 91V). -- 2. This code must be broken between the number and the letter (Ex: 91 and V). -- 3. The letter will have its value removed from the table. (Ex: V = 7). -- 4. The value of the number (Ex: 91) and the value of the letter (Ex: 7) shall be compared with the list in the original table. Numbers with numbers and letters with letters, based on their numerical values. -- 5. Table rows with smaller numbers AND those with smaller letters should be cut.
– Leon Freire
By "original table" you mean the table of the first figure? There also has no number 91...
– Luiz Vieira
Here’s what I’m getting: your code is made up of two parts, a numeric with two digits (ex: "91") and an alphabetic with one digit (ex: "V"). You have several of these codes in any table, and you want to build a function that automatically deletes lines whose code is in a user-given range.
– Luiz Vieira
The range will be given with a numerical part that will be the limit inferior and an alphabetical part that will be the limit superior of the exclusion. That is, if the user informs "89H", what he wants is to delete the lines with codes ranging from "90[A,B,C,...,G]" ==> "[91,92,93,...]G". That’s it?
– Luiz Vieira
@Luizvieira Exactly! The values of the letters are in the first table that are on a separate sheet! For now with the name of "IV". The reason is because the letters don’t follow an alphabetical order, so I had to quantify them.
– Leon Freire
Okay, I responded with a suggested implementation. I didn’t take advantage of your code because I had so many suggestions to pass that it was easier to redo. In the future, try to explain your problem better (give easier examples to follow) and create a [mcve] that someone can download and test or build on top. If not, it is difficult to get help in such cases. Good luck!
– Luiz Vieira