VBA simple array question

Scotaidh

New Member
Joined
Nov 26, 2008
Messages
19
hi,

I would like to have a static array, then have a string in which the only permitted characters are elements in my array.

Here is an example of what I would like without using an array:

Code:
Sub makemeanarray()
Str = "AGCTHC"
 For i = 1 To Len(Str)
    If Mid(seq, i, 1) = "A" Then
        ElseIf Mid(Str, i, 1) = "C" Then
        ElseIf Mid(Str, i, 1) = "G" Then
        ElseIf Mid(Str, i, 1) = "T" Then
        Else: MsgBox "problemo you have an unpermitted character!"
    End If
 Next
End Sub

Thus the only permitted characters in the string are A, G, C or T anything else should flag an error message.

Thank you for looking.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hello Scotaidh,

Here is an alternate method using the Regular Expressions script. It is more flexible and offers more features than an array.
Code:
Function CheckCharacters(ByVal Text As String) As Boolean

     Dim RegExp As Object
     Dim T As Boolean

    Set RegExp = CreateObject("VBScript.RegExp")
    RegExp.IgnoreCase = False
    RegExp.Pattern = "[^AGTHC]+"

    CheckCharacters = Not RegExp.Test(Text)

End Function
Example:
Code:
  result = CheckCharacters("HAT")    'TRUE

  result = CheckCharacters("SAT")    'FALSE
 
Last edited:
Upvote 0
Maybe like this

Code:
Sub makemeanarray()
Const Str As String = "AGCTHC"
Dim MyArray(1 To 10)
Dim i As Long
For i = 1 To Len(Str)
    If IsError(Application.Match(Mid(Str(i, 1), Array("A,G,C,T"), 0))) Then MyArray(i) = Mid(Str, i, 1)
Next i
End Sub
 
Upvote 0
I think you have an extra character and need a $ in the pattern, Leith:

Code:
Function ACGT(ByVal sInp As String) As Boolean
    With CreateObject("VBScript.RegExp")
        .IgnoreCase = False
        .Pattern = "^[[COLOR=red]ACGT[/COLOR]]+[COLOR=red]$[/COLOR]"
        ACGT = .Test(sInp)
    End With
End Function
 
Upvote 0
hi,

I would like to have a static array, then have a string in which the only permitted characters are elements in my array.

Here is an example of what I would like without using an array:

Code:
Sub makemeanarray()
Str = "AGCTHC"
 For i = 1 To Len(Str)
    If Mid(seq, i, 1) = "A" Then
        ElseIf Mid(Str, i, 1) = "C" Then
        ElseIf Mid(Str, i, 1) = "G" Then
        ElseIf Mid(Str, i, 1) = "T" Then
        Else: MsgBox "problemo you have an unpermitted character!"
    End If
 Next
End Sub

Thus the only permitted characters in the string are A, G, C or T anything else should flag an error message.

Thank you for looking.
I would not use Str as a variable name since that is the name of a built-in function in VB, so I'll use MyStr in my example. You can use the Like operator to do your testing...

Code:
Sub ContainsACGT()
MyStr = "AGCTHC"
  If MyStr Like "*[!ACGT]*" Then
    MsgBox "problemo you have an unpermitted character!"
  Else
    MsgBox "good set of characters
  End If
End Sub
 
Upvote 0
Code:
If MyStr Like "*[!ACGT]*" Then
We have a winner ...
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top