can you please help me with a vlookup in a different sheet only for certain values?
I have tried for each, for i, different if's and so on.
I need the vba to bring in Sheet2 values from a Sheet1, but only for rows that have the value "De rediscutat" in a different column in Sheet2.
Below you have the code i wrote for this function:
Dim result As String
Dim Sheet2 As Worksheet
Set Sheet2 = ActiveWorkbook.Sheets("Sheet2")
Set MyRange = Range("X2:X800")
If MyRange = "De rediscutat" Then
Worksheets("Sheet2").Range("N2:N694").Formula = Application.WorksheetFunction.VLookup(Sheet2.Range("I2:I694"), Sheet1.Range("G2:M500"), 7, False)
End If
End Sub```
Thanks!
You will need a loop to do this. Your code also has much redundancy. This will hopefully get you started:
Dim cel As Range, myrange As Range
Set myrange = Sheet2.Range("X2:X800")
For Each cel In myrange
If cel.Value = "De rediscutat" Then
'Grab whichever value you need and put it where you need it
End if
next cel
I suggest using Cel.Row to get the row number of the current cell in the loop, or Cel.Offset(,) to get to a cell on the same row.
For Each and For i will both work for looping through MyRange but you have not included either in your code.
The problem may not have been your loop, but rather the way you test the cell. If MyRange = "De rediscutat" will only work when MyRange` is a single cell.
If you Dim cell As Range recreate a For Each cell In MyRange loop and use `If cell = "De rediscutat" then it should work.
Related
I am hoping someone can help, I need to clear cells when then value of is less that a value in another cell. I did use conditional formatting but this messes up calculations further into the sheet.
I used a guide and was able to remove cells when I inputted the fixed integer into the module but am unsure how I adapt this to refer to a cell instead of a fixed number.
Thank you.
Ed
I believe this is what you are looking for below, this will take a value from cell B1 and compare against values in Column A, and if the values are less than the value in B1, it will clear the contents of that cell:
Sub ClearLowerThan()
Dim c As Range, Rng As Range
Dim LastRow As Long
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare you worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
CompareVal = ws.Range("B1").Value
'get the value to compare against, in this case from cell B1
Set Rng = ws.Range("A1:A" & LastRow)
'set the range to compare against the value from B1
For Each c In Rng 'for each cell in the given range
If c.Value < CompareVal Then c.ClearContents
'if value of cell is less than the value to compare against, clear the cell contents.
Next
End Sub
According to this website.
I think this should work:
Dim cell As Range
For Each cell In xxxSheet.Range("B:B").SpecialCells(xlCellTypeFormulas, xlNumbers)
'Do sth.
Next
which does not work. Is there something missing?
This should be working solution:
For Each cell In xxxSheet.Range("B:B")
If Not IsEmpty(cell) Then
'do sth
End If
Next
Also, if you want to loop until last filled cell, you could use following:
xxxSheet.Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row)
instead of
xxxSheet.Range("B:B")
It does not work, because you do not have formulas on column B. Put some formulas and some constants and try this:
Option Explicit
Public Sub TestMe()
Dim myCell As Range
Dim myRange As Range
Set myRange = Worksheets(1).Columns("B:B").SpecialCells(xlCellTypeFormulas, xlNumbers)
For Each myCell In myRange
Debug.Print myCell.Address
Next
Set myRange = Worksheets(1).Columns("B:B").SpecialCells(xlCellTypeConstants, xlNumbers)
For Each myCell In myRange
Debug.Print myCell.Address
Next
End Sub
The first loop would print the addresses of the formula cells, the second the addresses of the constants.
This is the ozgrid explanation about SpecialCells:
http://www.ozgrid.com/VBA/special-cells.htm
The problem is SpecialCells(xlCellTypeFormulas, xlNumbers) is returning only cells with formulas that make numbers (ie. =1+2).
To keep things efficient, you only need to check up to the last filled row
For Each cell In xxxSheet.Range("B1", Cells(Rows.Count, 2).End(xlUp))
If Not IsEmpty(cell) Then
'Do sth.
End If
Next
If you really want you can use SpecialCells() to have a range containing no blanks to loop through. If you only have formulas or only constants, you could use SpecialCells(xlFormulas) or SpecialCells(xlConstants) respectively, but for a more general use case you will have to do do a combination of the two.
Dim cell As Range
Dim searchRange As Range
' SpecialCells errors when there aren't cells instead of giving a useful value
On Error Resume Next
Set searchRange = xxxSheet.Range("B:B").SpecialCells(xlFormulas)
Set searchRange = xxxSheet.Range("B:B").SpecialCells(xlConstants)
Set searchRange = Union(xxxSheet.Range("B:B").SpecialCells(xlConstants), _
xxxSheet.Range("B:B").SpecialCells(xlFormulas))
On Error GoTo 0
If searchRange Is Not Nothing Then ' Only continue if no blanks
For Each cell In searchRange
'Do sth.
Next
End If
Suppose there is only one non-empty cell in a sheet and you want a macro to find it. So far I have the following code but am struggling to adjust it so it searches all columns, not only one:
Sub FirstNonEmpty()
Dim ws As Worksheet
Set ws = ActiveSheet
For Each cell In ws.Columns(1).Cells
If Not IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
End Sub
Perhaps there is a way to use a variable equal to all columns instead of "1" in "ws.Columns(1).Cells"?
You could use the top formulation if the non-blank cell does not contain a formula, or use xlcelltypeformulas if it's a formula. If you know there is a non-blank, you don't need the On Error. In fact if there is definitely only one you don't need the (1) either.
On Error Resume Next
ActiveSheet.Cells.SpecialCells(xlCellTypeconstants)(1).Select
'or
'ActiveSheet.Cells.SpecialCells(xlCellTypeformulas)(1).Select
Or you may give this a try...
Dim Rng As Range
Set Rng = Cells.Find(what:="*", LookIn:=xlValues)
If Not Rng Is Nothing Then
Rng.Select
End If
You can change the For statement to:
For Each cell In ws.UsedRange.Cells
which has the benefit of not scanning the rows/columns at the end of the worksheet as well.
This problem may seems pretty simple but I have been dealing with it for 2 days now and cant get it done. I searched other forums to see if they have this same problem but didnt find close to tyhis one.
So I have range K:T starting from row 2 and need to clear the cells that contains #N/A. I wrote the code below but cant seem to make it work.
Sub Clear_cells()
Dim rng As Range, i As Integer
'Set the range to evaluate to rng.
Set rng = Range("K:T")
'Loop backwards through the rows
'in the range that you want to evaluate.
For i = rng.Rows.count To 1 Step -1
'If cell i in the range contains an "N/A", delete cell content
If rng.Cells(i).Value = "#N/A" Then rng.Cells(i).CellRange.ClearContents (1)
Next
End Sub
If your cells contain an error value (i.e. #N/A) rather than the string "#N/A" then you need to check for an error value instead of checking for a string.
Replace
For i = rng.Rows.count To 1 Step -1
'If cell i in the range contains an "N/A", delete cell content
If rng.Cells(i).Value = "#N/A" Then rng.Cells(i).CellRange.ClearContents (1)
Next
with
Dim cel As Range
For Each cel in rng
If IsError(cel.Value) Then
If cel.Value = CVErr(xlErrNA) Then cel.ClearContents
End If
Next
or, if you want to check for all error conditions (such as #DIV/0!), just use
Dim cel As Range
For Each cel in rng
If IsError(cel.Value) Then cel.ClearContents
Next
use the function isna to check for #N/A
If Application.WorksheetFunction.IsNa(rng.Cells(i)) Then rng.Cells(i).CellRange.ClearContents (1)
I am very new to VBA. I am trying to isolate a particular customers transactions on a spread sheet.
Column "A" contains the Customer payment method (prepaid or various other methods). Column "D" contains the customer we are shipping too, Column "L" contains the customer we are shipping from.
If the cell in column "A" has 'prepaid' I want to search column "D" for the customer name (jaba). I will then delete all rows which do not contain this customer. IF the cell in column "A" is not 'prepaid' I want it to search column "L" for the customer name, and delete all rows which do not contain this string. When I run the code provided below I get an 1004 error on the following script 'If cell3.Find(ContainWord) Is Nothing Then'. Any help would be much appreciated.
Sub DoNotContainClearCells()
Dim rng As Range
Dim rng2 As Range
Dim rng3 As Range
Dim cell As Range
Dim cell2 As Range
Dim cell3 As Range
Dim ContainWord As String
'What range do you want to search?
Set rng = Range("A:A")
Set rng2 = Range("D:D")
Set rng3 = Range("L:L")
'What phrase do you want to test for?
ContainWord = "jaba"
For Each cell In rng
If cell.Value = "prepaid" Then
'Loop through each cell in range and test cell contents
For Each cell2 In rng2.Cells
If cell2.Find(ContainWord) Is Nothing Then EntireRow.Delete
Next cell2
Else
'Loop through each cell in range and test cell contents
For Each cell3 In rng3.Cells
If cell3.Find(ContainWord) Is Nothing Then EntireRow.Delete
Next cell3
End If
Next
End Sub
Without discussing the logic of your code, which is not part of the question, I can tell you that the
Run-time error '1004': Object required
in this line:
If cell3.Find(ContainWord) Is Nothing Then EntireRow.Delete
is because the EntireRow.Delete is missing a range (see Range.EntireRow Property (Excel))
Solution: Replace these lines:
If cell2.Find(ContainWord) Is Nothing Then EntireRow.Delete
If cell3.Find(ContainWord) Is Nothing Then EntireRow.Delete
with these:
If cell2.Find(ContainWord) Is Nothing Then cell2.EntireRow.Delete
If cell3.Find(ContainWord) Is Nothing Then cell3.EntireRow.Delete
Suggest to always have at the top of the modules\classes\userforms:
Option Explicit
This would have given a Compile error: Variable not defined highlighting the error earlier at compiling time (see Option Explicit Statement)