Update based on column contents VBA

I have a excel page with 40k plus records
Using VBA I would like to

I need to look in column H and find where the FIRST  7 characters are a :

4 digit number space - space
Example:  "9021 - "

When that is found place just the 4 digits (General formatting) in column A

ColA          ColH
                 9012 - auhviwbedfvivbfpwebfr
                 9012 - (*&^(^&RUO&*)HJOGUO

gets me this
ColA          ColH
9012           9012 - auhviwbedfvivbfpwebfr
 9016          9016 - (*&^(^&RUO&*)HJOGUO
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Open in new window

I am assuming your data starts at "H2", if not change it to "H1" or whichever cells it starts

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Glenn RayExcel VBA DeveloperCommented:
I agree with a non-programmatic solution like Shanan212 suggested.  

I would further refine the function to test specifically for a four-digit value followed by the "space-hyphen-space" string as follows:

In cell A2 (presuming data starts in row 2) and copied on down:

=IF(AND(ISNUMBER(VALUE(LEFT(H2,4))),MID(H2,5,3)=" - "),VALUE(LEFT(H2,4)),"")

Sub sample()

Dim iNum As Long, Count As Long, sa As String
iNum = ActiveSheet.Cells(Rows.Count, 8).End(xlUp).Offset(0, 0).Row

Count = 1

Do While Not Count = iNum + 1
    If (Mid(Cells(Count, 8).Value, 6, 1) = "-") Then
        Cells(Count, 1).Value = Left(Cells(Count, 8), 4)
    End If
    Count = Count + 1

End Sub

Open in new window

Larry Bristersr. DeveloperAuthor Commented:
I came up with a non-progrogramatic approach with this...

=RIGHT(LEFT(h3, SEARCH(" - ",H3,1)),5)

So you guys put me on the right track.

Want to split points?
Larry Bristersr. DeveloperAuthor Commented:
Put me on the right track.  Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.