Does cell contains value from array
once again an issue i cant really find quite the right answer through searching even though it feels like iam close.
My goal is to check in one column if each cell contains (besides other values) a letter from my array.
So the cells look something like 123A.
My array conatains value A,C,D,X,Y,Z.
Both just example values.
No my If statment should be true if the cell contains any of the letters, so for the example it should be true.
For the cell Value 123B it should be wrong (no B in array).
So far i have found a "IsinArray" Function that appears to be working but checks for specific values but what i would need is closer to ---> "*" & IsinArray & "*"
The function i found looks like this:
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
Also my For statement to loop through the column is working (I'd say ^^)
Thank you very much in advance, Iam also open to "creative" solutions if you have any new ideas how to do it better.
arrays excel vba excel-vba if-statement
add a comment |
once again an issue i cant really find quite the right answer through searching even though it feels like iam close.
My goal is to check in one column if each cell contains (besides other values) a letter from my array.
So the cells look something like 123A.
My array conatains value A,C,D,X,Y,Z.
Both just example values.
No my If statment should be true if the cell contains any of the letters, so for the example it should be true.
For the cell Value 123B it should be wrong (no B in array).
So far i have found a "IsinArray" Function that appears to be working but checks for specific values but what i would need is closer to ---> "*" & IsinArray & "*"
The function i found looks like this:
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
Also my For statement to loop through the column is working (I'd say ^^)
Thank you very much in advance, Iam also open to "creative" solutions if you have any new ideas how to do it better.
arrays excel vba excel-vba if-statement
add a comment |
once again an issue i cant really find quite the right answer through searching even though it feels like iam close.
My goal is to check in one column if each cell contains (besides other values) a letter from my array.
So the cells look something like 123A.
My array conatains value A,C,D,X,Y,Z.
Both just example values.
No my If statment should be true if the cell contains any of the letters, so for the example it should be true.
For the cell Value 123B it should be wrong (no B in array).
So far i have found a "IsinArray" Function that appears to be working but checks for specific values but what i would need is closer to ---> "*" & IsinArray & "*"
The function i found looks like this:
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
Also my For statement to loop through the column is working (I'd say ^^)
Thank you very much in advance, Iam also open to "creative" solutions if you have any new ideas how to do it better.
arrays excel vba excel-vba if-statement
once again an issue i cant really find quite the right answer through searching even though it feels like iam close.
My goal is to check in one column if each cell contains (besides other values) a letter from my array.
So the cells look something like 123A.
My array conatains value A,C,D,X,Y,Z.
Both just example values.
No my If statment should be true if the cell contains any of the letters, so for the example it should be true.
For the cell Value 123B it should be wrong (no B in array).
So far i have found a "IsinArray" Function that appears to be working but checks for specific values but what i would need is closer to ---> "*" & IsinArray & "*"
The function i found looks like this:
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function
Also my For statement to loop through the column is working (I'd say ^^)
Thank you very much in advance, Iam also open to "creative" solutions if you have any new ideas how to do it better.
arrays excel vba excel-vba if-statement
arrays excel vba excel-vba if-statement
asked Nov 13 '18 at 7:45
G.MG.M
136
136
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try,
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim s As String, i As Integer
Dim a As Variant
For i = 1 To Len(stringToBeFound)
s = Mid(stringToBeFound, i, 1)
For Each a In arr
If s = a Then
IsInArray = True
Exit Function
End If
Next a
Next i
End Function
add a comment |
Sorry to answer my own question (corrections/feedback to this solution is welcome of course)
I tried it like this and i guess it should work (cant really test because other parts of my makro arent working)
It is kinda unecessary complicated and maybe slow but id say it could work:
For i = 1 To VarAnzahlZeilen
Set rng = Worksheets("Filter").Range(Cells(i, VarNutzerSpalte), Cells(i, VarNutzerSpalte))
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
Worksheets("Import").Rows(j).Copy
Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Uses this function by QHarr (with only the array name changed)
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Thanks alot @ QHarr and also @ Dy.Lee !
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
add a comment |
You could swop it around
Option Explicit
Public Sub Test()
Dim testArray(), cellValue As String, rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1") '<== contains
testArray = Array("A", "C", "D", "X", "Y", "Z")
Debug.Print IsInArrayValue(testArray, rng)
End Sub
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
If using as an UDF you could either pass the array in as shown above or if array doesn't change you could move into the function itself. Personally, I prefer passing the array as an argument to the function as more flexible. I can't work out where your row to copy is coming from. Your comment posted as an answer uses a j variable that doesn't appear to be involved in the shown loop and the row is copied from another sheet. So below will not work directly but gives you a framework.
Public Function IsInArrayValue(ByVal rng As Range) As Variant
Dim i As Long, testString As String, testArray()
testArray = Array("A", "C", "D", "X", "Y", "Z")
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Call:
The comment below is looks like a new question but you probably want something like:
Dim loopRange As Range, rng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
rng.EntireRow.Copy '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
A union version might looks like:
Dim loopRange As Range, rng As Range, unionRng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
End If
Set unionRng = rng '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
If Not unionRng Is Nothing Then
unionRng.EntireRow.Copy 'destination for paste
End If
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276118%2fdoes-cell-contains-value-from-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try,
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim s As String, i As Integer
Dim a As Variant
For i = 1 To Len(stringToBeFound)
s = Mid(stringToBeFound, i, 1)
For Each a In arr
If s = a Then
IsInArray = True
Exit Function
End If
Next a
Next i
End Function
add a comment |
Try,
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim s As String, i As Integer
Dim a As Variant
For i = 1 To Len(stringToBeFound)
s = Mid(stringToBeFound, i, 1)
For Each a In arr
If s = a Then
IsInArray = True
Exit Function
End If
Next a
Next i
End Function
add a comment |
Try,
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim s As String, i As Integer
Dim a As Variant
For i = 1 To Len(stringToBeFound)
s = Mid(stringToBeFound, i, 1)
For Each a In arr
If s = a Then
IsInArray = True
Exit Function
End If
Next a
Next i
End Function
Try,
Public Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim s As String, i As Integer
Dim a As Variant
For i = 1 To Len(stringToBeFound)
s = Mid(stringToBeFound, i, 1)
For Each a In arr
If s = a Then
IsInArray = True
Exit Function
End If
Next a
Next i
End Function
answered Nov 13 '18 at 8:11
Dy.LeeDy.Lee
3,4721510
3,4721510
add a comment |
add a comment |
Sorry to answer my own question (corrections/feedback to this solution is welcome of course)
I tried it like this and i guess it should work (cant really test because other parts of my makro arent working)
It is kinda unecessary complicated and maybe slow but id say it could work:
For i = 1 To VarAnzahlZeilen
Set rng = Worksheets("Filter").Range(Cells(i, VarNutzerSpalte), Cells(i, VarNutzerSpalte))
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
Worksheets("Import").Rows(j).Copy
Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Uses this function by QHarr (with only the array name changed)
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Thanks alot @ QHarr and also @ Dy.Lee !
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
add a comment |
Sorry to answer my own question (corrections/feedback to this solution is welcome of course)
I tried it like this and i guess it should work (cant really test because other parts of my makro arent working)
It is kinda unecessary complicated and maybe slow but id say it could work:
For i = 1 To VarAnzahlZeilen
Set rng = Worksheets("Filter").Range(Cells(i, VarNutzerSpalte), Cells(i, VarNutzerSpalte))
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
Worksheets("Import").Rows(j).Copy
Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Uses this function by QHarr (with only the array name changed)
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Thanks alot @ QHarr and also @ Dy.Lee !
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
add a comment |
Sorry to answer my own question (corrections/feedback to this solution is welcome of course)
I tried it like this and i guess it should work (cant really test because other parts of my makro arent working)
It is kinda unecessary complicated and maybe slow but id say it could work:
For i = 1 To VarAnzahlZeilen
Set rng = Worksheets("Filter").Range(Cells(i, VarNutzerSpalte), Cells(i, VarNutzerSpalte))
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
Worksheets("Import").Rows(j).Copy
Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Uses this function by QHarr (with only the array name changed)
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Thanks alot @ QHarr and also @ Dy.Lee !
Sorry to answer my own question (corrections/feedback to this solution is welcome of course)
I tried it like this and i guess it should work (cant really test because other parts of my makro arent working)
It is kinda unecessary complicated and maybe slow but id say it could work:
For i = 1 To VarAnzahlZeilen
Set rng = Worksheets("Filter").Range(Cells(i, VarNutzerSpalte), Cells(i, VarNutzerSpalte))
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
Worksheets("Import").Rows(j).Copy
Worksheets("Filter").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Next i
Uses this function by QHarr (with only the array name changed)
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Thanks alot @ QHarr and also @ Dy.Lee !
answered Nov 13 '18 at 10:21
G.MG.M
136
136
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
add a comment |
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Hi! This area is for answers. Also, in the above your are not changing the row j at last where I can see anyway. How is j related to your loop?
– QHarr
Nov 13 '18 at 10:27
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
Yeah, you are right, its and copy/paste error ^^
– G.M
Nov 13 '18 at 11:18
add a comment |
You could swop it around
Option Explicit
Public Sub Test()
Dim testArray(), cellValue As String, rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1") '<== contains
testArray = Array("A", "C", "D", "X", "Y", "Z")
Debug.Print IsInArrayValue(testArray, rng)
End Sub
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
If using as an UDF you could either pass the array in as shown above or if array doesn't change you could move into the function itself. Personally, I prefer passing the array as an argument to the function as more flexible. I can't work out where your row to copy is coming from. Your comment posted as an answer uses a j variable that doesn't appear to be involved in the shown loop and the row is copied from another sheet. So below will not work directly but gives you a framework.
Public Function IsInArrayValue(ByVal rng As Range) As Variant
Dim i As Long, testString As String, testArray()
testArray = Array("A", "C", "D", "X", "Y", "Z")
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Call:
The comment below is looks like a new question but you probably want something like:
Dim loopRange As Range, rng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
rng.EntireRow.Copy '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
A union version might looks like:
Dim loopRange As Range, rng As Range, unionRng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
End If
Set unionRng = rng '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
If Not unionRng Is Nothing Then
unionRng.EntireRow.Copy 'destination for paste
End If
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
add a comment |
You could swop it around
Option Explicit
Public Sub Test()
Dim testArray(), cellValue As String, rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1") '<== contains
testArray = Array("A", "C", "D", "X", "Y", "Z")
Debug.Print IsInArrayValue(testArray, rng)
End Sub
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
If using as an UDF you could either pass the array in as shown above or if array doesn't change you could move into the function itself. Personally, I prefer passing the array as an argument to the function as more flexible. I can't work out where your row to copy is coming from. Your comment posted as an answer uses a j variable that doesn't appear to be involved in the shown loop and the row is copied from another sheet. So below will not work directly but gives you a framework.
Public Function IsInArrayValue(ByVal rng As Range) As Variant
Dim i As Long, testString As String, testArray()
testArray = Array("A", "C", "D", "X", "Y", "Z")
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Call:
The comment below is looks like a new question but you probably want something like:
Dim loopRange As Range, rng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
rng.EntireRow.Copy '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
A union version might looks like:
Dim loopRange As Range, rng As Range, unionRng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
End If
Set unionRng = rng '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
If Not unionRng Is Nothing Then
unionRng.EntireRow.Copy 'destination for paste
End If
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
add a comment |
You could swop it around
Option Explicit
Public Sub Test()
Dim testArray(), cellValue As String, rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1") '<== contains
testArray = Array("A", "C", "D", "X", "Y", "Z")
Debug.Print IsInArrayValue(testArray, rng)
End Sub
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
If using as an UDF you could either pass the array in as shown above or if array doesn't change you could move into the function itself. Personally, I prefer passing the array as an argument to the function as more flexible. I can't work out where your row to copy is coming from. Your comment posted as an answer uses a j variable that doesn't appear to be involved in the shown loop and the row is copied from another sheet. So below will not work directly but gives you a framework.
Public Function IsInArrayValue(ByVal rng As Range) As Variant
Dim i As Long, testString As String, testArray()
testArray = Array("A", "C", "D", "X", "Y", "Z")
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Call:
The comment below is looks like a new question but you probably want something like:
Dim loopRange As Range, rng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
rng.EntireRow.Copy '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
A union version might looks like:
Dim loopRange As Range, rng As Range, unionRng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
End If
Set unionRng = rng '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
If Not unionRng Is Nothing Then
unionRng.EntireRow.Copy 'destination for paste
End If
You could swop it around
Option Explicit
Public Sub Test()
Dim testArray(), cellValue As String, rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1") '<== contains
testArray = Array("A", "C", "D", "X", "Y", "Z")
Debug.Print IsInArrayValue(testArray, rng)
End Sub
Public Function IsInArrayValue(ByVal testArray As Variant, ByVal rng As Range) As Variant
Dim i As Long, testString As String
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue = CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
If using as an UDF you could either pass the array in as shown above or if array doesn't change you could move into the function itself. Personally, I prefer passing the array as an argument to the function as more flexible. I can't work out where your row to copy is coming from. Your comment posted as an answer uses a j variable that doesn't appear to be involved in the shown loop and the row is copied from another sheet. So below will not work directly but gives you a framework.
Public Function IsInArrayValue(ByVal rng As Range) As Variant
Dim i As Long, testString As String, testArray()
testArray = Array("A", "C", "D", "X", "Y", "Z")
testString = rng.Text
If rng.Cells.Count > 1 Then
IsInArrayValue CVErr(xlErrNA)
Exit Function
End If
For i = LBound(testArray) To UBound(testArray)
If InStr(testString, testArray(i)) > 0 Then
IsInArrayValue = True
Exit Function
End If
Next
IsInArrayValue = False
End Function
Call:
The comment below is looks like a new question but you probably want something like:
Dim loopRange As Range, rng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
rng.EntireRow.Copy '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
A union version might looks like:
Dim loopRange As Range, rng As Range, unionRng As Range
With ThisWorkbook.Worksheets("Filter")
Set loopRange = .Range(.Cells(1, VarNutzerSpalte), .Cells(VarAnzahlZeilen, VarNutzerSpalte))
End With
For Each rng In loopRange
If IsInArrayValue(ArrAuswahlNutzer, rng) Then
If Not unionRng Is Nothing Then
Set unionRng = Union(unionRng, rng)
End If
Set unionRng = rng '<= use Union to gather range if all being pasted in a block somewhere
End If
Next
If Not unionRng Is Nothing Then
unionRng.EntireRow.Copy 'destination for paste
End If
edited Nov 13 '18 at 10:29
answered Nov 13 '18 at 8:11
QHarrQHarr
30.8k81941
30.8k81941
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
add a comment |
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
Hi There, thanks alot but i have a small issue If the function is true then i want to copy the whole row. Normally i would work with a for loop that and say row(i).copy but well, how to implement that? Here is my current Code ` 'The range should be fine (need it variable) Set rng = ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) 'For i = 1 to VarAnzahlZeilen If IsInArrayValue(ArrAuswahlNutzer, rng) Then 'Here I want to copy the Current Row End If 'Next i` I guess this version wont work
– G.M
Nov 13 '18 at 10:11
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
sorry i cant get the format to work :/
– G.M
Nov 13 '18 at 10:13
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
That becomes a new question really. rng needs to be a single cell so you would be looping all cells in ThisWorkbook.Worksheets("Filter").Range(Cells(1, VarNutzerSpalte), Cells(VarAnzahlZeilen, VarNutzerSpalte)) . See edit above in case helps.
– QHarr
Nov 13 '18 at 10:16
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Thanks alot, i tried something different (and ugly) but i guess it could work :)
– G.M
Nov 13 '18 at 10:23
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
Are you copying all the rows to the same place? It is possible and efficient, in that case, to copy all rows into a Union range object and paste in one go.
– QHarr
Nov 13 '18 at 10:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276118%2fdoes-cell-contains-value-from-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown