If value is in array then - excel

What I'm trying to achieve is a loop that will check if a cell (which will be two letters of the alphabet) is found in a array, if this is the case other stuff will happen.
So far my code looks like:
Sub Mortgagee()
Dim Symbol As Variant
Dim i As Long
Symbol = Range("C1:C11").Value
For i = LBound(Symbol, 1) To UBound(Symbol, 1)
If Symbol.contains("A1") Then
Range("G1").Copy
Range("A1").Select
ActiveSheet.Paste
End If
Next i
End Sub

In your code above, Symbol is only taking the value of the first cell in the range - in this case, it is just taking the value of whatever is in cell C1.
I'm going to assume that you what you are trying to do is check if the value of a cell exists in an array - not if the cell itself is (which would mean you had an array of cell, or Range, objects).
Sub Mortgagee()
Dim i as Long
Dim arrSymbol(1 to 11) as String
For i = 1 to UBound(arrSymbol)
If arrSymbol(i) = "value to match" Then
'Do work here
End If
Next i
End Sub
If you provide more information about the problem, specifically what value(s) you are checking for and also where Codes came from and what it is then I can help you out some more.

Related

finding cell value and delete it in excel vba

I want to return the value of the cell it's found in VBA and clear all of it's content with another 3 rows below it as well, but I'm currently stuck. I can find where the cell is coming from, but it's deleting the whole range instead of the specific range (I'm aware the range("A7:H20") was wrong). How do I select the correct range?
Sub Find_and_remove()
For Each cell In Range("A7:H20")
If cell.Value = Range("Q5") Then
'return the range of that cell and deleted it with 3 row below it'
Range("A7:H20").Clear
End If
Next cell
End Sub
Sub Find_and_remove()
Dim rng As Range
For Each rng In Range("A7:H20")
If rng.Value = Range("Q5") Then Range(rng, rng.Offset(3, 0)).Clear
Next cell
End Sub
Another solution:
I am using a sub to which you pass the parameters:
value to be found
range where to look in and clear contents
number of rows below the found value to be cleared.
Furthermore I am looking from the bottom to the top of the range - otherwise cells could be cleared that contain the string to be found - and then below values won't get cleared:
Option Explicit
'>>> example of how to call the findAndRemove-Sub <<<
Public Sub test_FindAndRemove()
With ActiveSheet ' adjust this to your needs
findAndRemove .Range("Q5"), .Range("A7:H20"), 3
End With
End Sub
'>>>> this is the sub that is doing the work <<<<<
Public Sub findAndRemove(strFind As String, _
rgLookup As Range, _
cntDeleteRowsBelow As Long)
Dim i As Long, c As Range
'start from the bottom and go up
'otherwise you could delete further strFind-cells unintentionally
For i = rgLookup.Rows.Count To 1 Step -1
For Each c In rgLookup.Rows(i).Cells
If c.Value = strFind Then
'ATTENTION:
'at this point there is no check if below row contains the strFind-value!
c.Resize(cntDeleteRowsBelow + 1).Clear
End If
Next
Next
End Sub
You could just use cell.Clear, or if you want the cell cleared and the next 3 below it use something like this
For i = 0 To 3
cell.Offset(i, 0).Clear
Next
I think you mean "return the address of that cell", no? Debug.Print(cell.Address) will get you this info. But you don't actually need it here. Instead of Range("A7:H20").Clear write cell.Resize(1 + i, 1).Clear with i = number of rows you want to clear along with cell itself (no need for a loop).

VBA Excel/ search & replace value in next column double variables

I have the feeling I am close, yet I feel really far.
I have one sheet called temp.
In M2 there's a unique identifier so I know which row I need to find in my other sheet which is called specs.
The value of G2 of the sheet temp needs to be pasted 3 columns to the right on the searched value (in the sheet specs).
I came this far but now I am stuck :/
Sub search()
Dim indexnr As String
Dim rngFind As Range, rngLookUp As Range
indexnr = Sheets("temp").Range("m2")
Data = Sheets("temp").Range("g2")
Set rngLookUp = Range("D1:D1000")
Set rngFind = rngLookUp.Find(indexnr, LookIn:=xlValues).Offset(0, 1)
End Sub
Instead of using the range.find method, a For Each Next Statement should do what you need.
The range.find method returns another range object which would not paste the values inside the cell.
Try something like this:
For Each i in [D1:D1000]
tempString = i.value2
if Instr(1,tempString,indexr,0) <> 0 then
i.offset(0,3).value2 = tempString
end if
next i

How to put all values in the first row into an array?

I am trying to set my array equal to all column headers of tables in a worksheet.
If I were doing this in Excel, I would click on A1 and do Control+Shift+Right Arrow. I've found some 15 year old code attempting to simulate this, shown below, but it doesn't recognize the last Column.
Sub Ls_List_Click()
'variables used in for each loops
Dim Column_Array() As Variant
Dim EndRange As Range
ThisWorkbook.Worksheets(Me.Ls_List.Value).Select
Range("A1", Range("A1").End(xlRight)).Select
EndRange = Range("A1").End(xlRight).Offset(2, 0)
Range(EndRange, EndRange.End(xlRight)).Select
EndRange = EndRange.End(xlRight).Offset(2, 0)
Column_Array() = EndRange
'... couple of for each loops
End Sub
I get error 1004 on the following line:
Range("A1", Range("A1").End(xlRight)).Select
If you replace xlRight with xlToRight you should be good
I believe you're looking for something more like:
Sub Ls_List_Click()
'variables used in for each loops
Dim Column_Array() As Variant
Column_Array() = Range(Cells(1,1),Cells(1, Cells(1, 1).End(xlToRight).Column)).value
'... couple of for each loops
End Sub
There didn't appear to be rhyme or reason for the .select components beyond stepping through to see where things were (nice for testing... i guess?).
Just one line of code gets your array generated for all the contiguous headers in your first row.
Edit:
Checking back, because EndRange is used for looping, you may want to write:
Sub Ls_List_Click()
'variables used in for each loops
Dim Column_Array() As Variant, EndRange as Long 'reference to column number, not a range, so watch referencing
EndRange = Cells(1, 1).End(xlToRight).Column
Column_Array() = Range(Cells(1,1),Cells(1, EndRange)).value
'... couple of for each loops
End Sub
Two main issues with your code:
You misspelled xlRight - the correct value is xlToRight. That's why it is recommended to use Option Explicit, so such errors are detected at compile time.
You shouldn't use Select - what for? If you want to reference particular cells/range, just use Cells(row, column) (row and column are integers) or Range("A1:B3") (just example).
Having said that, to get values into an array, you should use:
firstRowArray = Range(Cells(1, 1), Cells(1, 1).End(xlToRight)).Value2
Expalnation:
I used Range overload, accepting two cells as top-left and bottom-right cells to define the range. Top left is Cell(1, 1), i.e. A1 and bottom left is Cells(1, 1).End(xlToRight), which you are already familiar with :)
Then I use Value2 property, which return array of values (you could use Value but it's slower).
Note that, the result will be two-diensional array with number of rows and number of columns same as in the range.

Including a formula based on a function in a dynamic defined cell

I need to concatenate a dynamic range using VBA.
My best guess so far was to create a function and then calling the function via VBA to a specific cell. Here is the code i'm using:
Sub test()
I used this method to declare the variable just to shorten the code. Basically the last column that may contain data will always vary and is never the same. I already have the code working in order to determine the last column that contains data so let's just say in this case the last column is column G (or column 7)
Dim LASTCOLUMN As Integer: LASTCOLUMN = 7
What i need is to get the concatenated range in cell A1. My best guess is the following code but i also tried several other options and all of them failed. Can someone please help me with this line of code? It needs to be dynamic in order to incorporate the dynamic variable LASTCOLUMN.
Range("A1").Formula = "=ConcatRange(Cells(2, 1):Cells(LASTCOLUMN, 1)" End Sub
This is the function that concatenates a given range
Function ConcatRange(myrange As Range) As String
Dim CurrentRange As String
Dim r As String
CurrentRange = ""
For Each cell In myrange
If cell <> "" Then
r = cell
CurrentRange = CurrentRange & r
End If
Next cell
ConcatRange = CurrentRange
End Function

for each control variable must be variant or object

Really new to VBA here... I've looked around and tried to piece together some code to fulfil my need. Think it's almost there, but I'm getting errors that are likely easy to overcome and yet I don't know how.
The code looks at the current sheet (STOCK), and takes a 'target' text value from cell A2. It then searches a named range in another sheet 'Other'. If it determines one of the cells ('cand') in Other to be equal to the target value, then a value of "True" will be applied to column G in the STOCK sheet, on the same row of the original target.
Hopefully this makes sense. I've copied in the code which will maybe shed more light on things.
Dim target As String
Dim cand As String
Dim currentrow As Integer
Sub search_named_range()
' This range is hard coded; we can try A:A if hard code version works '
For Each target In Worksheets("STOCK").Range("A2:A1000")
' retrieve the row of the current range, for use when setting target values '
currentrow = Range(target).Row
' FOR loop to search range of part numbers in Mojave '
For Each cand In Worksheets("Other").Range("N9:N150")
If StrConv(cand.Value, 2) = StrConv(target, 2) Then
Worksheets("STOCK").Range("G" + currentrow) = "True"
GoTo FORend
End If
Next cand
' If part is not found, do nothing and return to find next target '
FORend: Next target
End Sub
Currently I'm getting the error 'For Each control variable must be Variant or Object', but can't find anywhere that explains why this is. I'm sure it's pretty obvious, but a steer would be really appreciated.
Thanks.
You can't use a String variable in a For Each. You're using tartget and cand as the control variables in your For Each loops but you have defined them as strings. They need to be an object, and specifically an object that is contained the collection of objects you're iterating. You're iterating over a range, which is a collection of ranges, so your control variables need to be Range objects.
Sub search_named_range()
Dim rCell As Range
Dim rCand As Range
For Each rCell In Worksheets("STOCK").Range("A2:A1000").Cells
For Each rCand In Worksheets("Other").Range("N9:N150").Cells
If StrComp(rCand.Value, rCell.Value, vbTextCompare) = 0 Then
rCell.Offset(0, 6).Value = "True"
Exit For 'exits the rCand For, but no the rCell one
End If
Next rCand
Next rCell
End Sub
Other changes that weren't correcting errors:
I'm not sure why you declared your variables outside the sub, but I put them inside.
You don't need to define .Cells at the end of the For Each line, but I like to. You could iterate over .Rows or .Columns or .Areas with a Range (although .Cells is the default).
There's nothing wrong with StrConvert, but you could also use LCase() or, as I do, StrComp.
Since I already have a reference to a cell on the current row (rCell), I use that and Offset to fill in a column I want.

Resources