Apply VLOOKUP on next visible cell - excel

The below code does vlookup then autofills the data then applies the filter to #N/A. Here I need to do another VLOOKUP in the same column with the filter as #N/A but I am not sure about this as how do we select the cells below F1 and apply VLOOKUP on the visible data. Could you help me out with this?
Sub Vlookup()
Worksheets("error rate").Activate
Range("F2") = "=Vlookup(B2,'sales'!B:C,2,0)"
Range("F2").Select
Range("F2").AutoFill Range("F2:F" & Range("B" & Rows.Count).End(xlUp).Row)
Range("F:F").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("B1").AutoFilter Field:=6, Criteria1:="#N/A"
Range = Rng.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 6)
End Sub

Few things
Avoid using Activate/Select. You may want to see How to avoid using Select in Excel VBA
Define and work with objects. Becomes much easier to work with your code.
Instead of entering formula in one cell and then autofilling it, simply enter the formula in the entire range in one go as shown below.
I see the objective is to get all the values. Then there is no need to enter a formula, filter and renter the formula. Use a single nested formula using IFERROR and IF. For example, if the formula "=Vlookup(B2,'sales'!B:C,2,0)" doesn't give you the result and you want to pull up the values from say column D then use the formula =IFERROR(VLOOKUP(B2,Sales!B:C,2,0),VLOOKUP(B2,Sales!B:D,3,0)). I have simply nested VLOOKUP(B2,Sales!B:D,3,0) inside IFERROR(). What the formula does is checks if there is an error with VLOOKUP(B2,Sales!B:C,2,0) and if there is, then it attempts to find the value using VLOOKUP(B2,Sales!B:D,3,0)
CODE
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Set this to the relevant sheet
Set ws = ThisWorkbook.Sheets("error rate")
With ws
'~~> Find last row
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
'~~> Work with the relevant range
With .Range("F2:F" & lRow)
'~~> Enter the formula in the entire range in one go
.Formula = "=IFERROR(VLOOKUP(B2,Sales!B:C,2,0),VLOOKUP(B2,Sales!B:D,3,0))"
'~~> OPTIONAL
'~~> Instead of copy and paste as values use this.
'.Value = .Value
End With
End With
End Sub

Related

VBA in Excel - Copy past values of a range of cells to other cells

Looking for some help,
I have created a command button and attached the following macro:
Private Sub CommandButton1_Click()
Range("J2:J3000").Copy
Range("G2:G3000").PasteSpecial xlPasteValues, xlNone, SkipBlanks
End Sub
The task im doing is very simple, copy values only from cells from range J2:J3000 to G2:G3000 but skip blanks when pasting to the corresponding row cell. (J2 copy, paste to G2) This isnt working as its overriding data in the range G2:G3000 with blanks from J2:J3000 instead of pasting the data only
To give context to the ranges.
J2:J3000 has a function
=IF(ISNA(VLOOKUP(A2,H:H,1,FALSE)),"","Yes")
which is checking data in the sheet that is manually pasted into column H:H and deleted with different data daily.
G2:G3000 has blank text cells where the results from J2:J3000 is then manually pasted to the corresponding row under column G.
(Basically, checking the value returned from the lookup, if it says yes then its manually copied to the cell in column G on the matching row.)
I am trying to introduce a button macro that can paste value returned the lookup and automate this process.
Private Sub CommandButton1_Click()
Range("J2:J3000").Copy
Range("G2:G3000").PasteSpecial xlPasteValues, xlNone, SkipBlanks
End Sub
Your current method is overwriting everything in Column G
To correct this you can work with filtered ranges or utilize loops to conditionally update values in Column G. A loop solution is below:
Sub Test()
'Update the sheet name
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim i As Long
For i = 2 To 3000
If ws.Range("J" & i).Value = "Yes" Then
ws.Range("G" & i).Value = ws.Range("J" & i).Value
End If
Next i
End Sub
For now this is manually hardcoded to stop at row 3,000. It is likely better to change this to be dynamic. To do this you just need to add a new variable (lr for 'last row') to store value of last row and modify the loop to iterate up until lr instead of the hardcoded value 3,000. Relevant bits of code are below
Dim i As Long, lr As Long
lr = ws.Range("J" & ws.Rows.Count).End(xlUp).Row
For i = 2 To lr
'If .......
Next i

Creating a dynamic autofill range in recorded code

I'm trying to get a dynamic range in a recorded macro.
The data in column "O" is variable.
When I used the macro, VBA set it at the range of the specific worksheet.
Is there a way to make the autofill option variable to the amount of rows?
I used this code:
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O188")
Range("O2:O188").Select
I want the range to be dynamic.
Try the next code, please. It uses column N:N as reference for the number of rows to be filled. No need to select anything:
Sub testAutoFill()
Dim sh As Worksheet, lastRow As Long
Set sh = ActiveSheet
lastRow = sh.Range("N" & Rows.Count).End(xlUp).row
sh.Range("O2").AutoFill Destination:=sh.Range("O2:O" & lastRow)
End Sub
The code can use the maximum rows of the used range, but in such a case, if the value to be filled down is a formula, the referenced column is good to be used. Otherwise, one of the neighbor column.

Debug countif and autofill in excel VBA, criteria doesn't work

I will need a function to count the number of each item in column J and show the result on column K. But this code I show below keep saying that the criteria part RC[-2] is wrong. After the countif function, I will need it to be able to autofill in whatever lines are given so that I can apply this code to other files as well.
I used Macro to generate some code to start. And also try this earlier:
paste_countPTD = Worksheetfunction.CountIf(paste_conPTD,RC[-2]).
The criteria part seemed wrong.
Dim paste_conPTD As Range
Set paste_conYTD = Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
Range("K1").Select
ActiveCell.FormulaR1C1 = "Count_PTD"
Range("K2").Worksheetfunction.countif(paste_conPTD,RC[-2])
I appreciate any suggestion to make this code works. To do the countif for a column and autofill the formula.
You can try this code
Dim paste_conPTD As Range
Set paste_conYTD = Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
Range("K1") = "Count_PTD"
Dim iRng as Range
For each iRng in paste_conPTD
iRng.Offset(0,1) = Worksheetfunction.Countif(paste_conPTD, iRng)
Next iRng
To give you some notes, we need to iterate over each cells in the paste_conYTD range, that is where iRng comes in. We can't tell Excel like paste_conYTD = <some formula> and assume Excel knows we want it to compute for each cells using the formula. Excel iteration comes in a few ways, we can choose one that is easiest to apply based on scenario.
For each ... in ... Next
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/for-eachnext-statement
For ... Next
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/fornext-statement
Do... Loop
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/doloop-statement
While... Wend
https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/whilewend-statement
If you want the actual formula in the cells try this.
Dim paste_conPTD As Range
Set paste_conYTD = Range("J2:J" & Range("A" & Rows.Count).End(xlUp).Row)
Range("K1").Value = "Count_PTD"
paste_conYTD.Offset(, 1).FormulaR1C1 = "=COUNTIF(" & paste_conYTD.Address(ReferenceStyle:=xlR1C1) & ",RC[-2])"

Autofill from cell above if adjacent cell is not empty + match

I'm not that familiar with VBA, so please bear with me.
My question is related to this link: Double-click autofill - dynamic based on adjacent cell
enter image description here
However, the attributes are located in another column (for example Column E). I've tried tweaking the code, but to no avail.
Does this work for you?
Sub FillBlanks()
Dim lr As Long
lr = Cells(Rows.Count, "E").End(xlUp).Row 'Assuming Attributes are in Column E
On Error Resume Next
'Assuming you want to fill blanks in column A
With Range("A1:A" & lr).SpecialCells(xlCellTypeBlanks)
.Formula = "=R[-1]C"
.Value = .Value
End With
End Sub

How to concatenate 2 cells at the top of a filtered list? (EXCEL macros)

So i've searched for hours now, and can't seem to find a solution to my problem.
I have a list of 13000+ rows, but I have auto filtered it down to about 200, now what I want is that, lets assume, the top of the list now shows as A1 then straight to A28 because of the filter.
I want the text in cell A28 and B28 to be concatenated and put in cell J28.
I am able to do this manually easily, but when I record the actions for a macro this code comes up
Sub concat()
'
' concat Macro
'
'
ActiveCell.FormulaR1C1 = "=RC[-9]&RC[-8]"
Range("J28").Select
Selection.FillDown
End Sub
Now I'm not sure what
"=RC[-9]&RC[-8]"
means but when I run the macro it does not result in what I want.
If I am unclear on this problem, I apologize in advance but I really do need help.
Thanks!
Varun
This will work, but you have to turn off the filter first:
Sub FillDown()
Dim ws As Excel.Worksheet
Dim LastRow As Long
Set ws = ActiveSheet
With ws
LastRow = ws.Range("A" & .Rows.Count).End(xlUp).Row
.Range("J2:J" & LastRow).FormulaR1C1 = "=RC[-9]&RC[-8]"
End With
End Sub
I'm not clear why you don't just turn off the filter and manually copy down the formula as far as necessary.

Resources