VBA finding text and assigning variable

cmefly

Well-known Member
Joined
May 13, 2003
Messages
683
Hi,

For the life of me i can't figure this one out...and it seems easy..(or so i think)...blah!

using VBA - i'm trying to find every cell in column B that is equal to "AAA". Then, i'd like to assign a variable its location (for example, if B9 = "AAA", then variable A = 9, if the next time it finds "AAA" is in cell B400 then variable B = 400....etc in that order). The maximum number of "AAA" that can be found is 25.

is this possible? I'm having problems looping through the alphabet....

thanks,

Marc
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Just to get you started, if you have the same idea as me
your code should look like
Code:
Sub Test
    Dim LR As Long, i As Long
    LR = Range("B" & Rows.count).End(xlUp).Row
 
    For i = 1 To LR
         If Range("B" & i).Value = "AAA" Then
             'Store position of the "AAA" occurrence
         End If
    Next i 
End Sub
 
Upvote 0
kpark...thank you but that's what i have...i can find the cell...but i'm uncertain how to record the location of each find as a different variable.....

suggestions?
 
Upvote 0
thanks!!

i just found this online that basically takes what you mentioned...check it out...

Sub TestStaticArray()
' stores 10 names in the workbook in the array variable MyNames()
Dim MyNames(1 to 10) As String ' declares a static array variable
Dim iCount As Integer
For iCount = 1 To 10
MyNames(iCount) = ThisWorkbook.Names(iCount)
Next iCount
Erase MyNames() ' deletes the varible contents, free some memory
End Sub
 
Upvote 0
Use Array

Dim store(25)

And then store it as store(i) as your variable for 25 time

Exactly except make sure you're dimming with lowerbound in mind as well.
As excel can be annoying with lowerbound base if you don't specify it

Code:
Dim store(0 To 24)
'or
Dim store(1 To 25)

Then you could use count variable to store these variables.

Might as well give you the whole code for better understanding
Code:
Sub Test
    Dim LR As Long, i As Long, store(0 To 24) As Long, count As Integer
    LR = Range("B" & Rows.count).End(xlUp).Row
    count = 0
    For i = 1 To LR
         If Range("B" & i).Value = "AAA" Then
             store(count) = Range("B" & i).Row
             count = count + 1
         End If
    Next i 
End Sub
 
Upvote 0
hi..my macro isn't working and i can't figure out why...can some please run this mini macro and help me ? :( (the values in the hidden1 worksheet are strings..names of people)

my error is at the selection.formula part.......

Sub replacenames()

Dim LR As Long, i As Long, store(0 To 24) As Long, count As Integer
Dim j As Integer

LR = Range("B" & Rows.count).End(xlUp).Row - 1
count = 1
For i = 1 To LR
If Range("B" & i).Value = "AAA" Then
store(count) = Range("B" & i).Row
count = count + 1
End If
Next i


For j = 1 To count
Range("B" & store(j)).Select
Selection.Formula = "=UPPER('hidden1'!A" & j & ") is great"
Next j

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,256
Members
452,901
Latest member
LisaGo

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