Failed Excel 2007 Macro

The code below is a part of an Excel 2007 Macro. Its purpose is to Select Column D2:(Last populated Cell in Column D) and find the string "Admin Fee". If it is not found, the code ends and it goes on to the next instruction.

If it is found, the code CUTS the text from the cell populated by "Admin Fee". The code them MOVES two cells to the right and populates that cell with "1". Next, the code moves three cells to the left and pastes "Admin Fee" into the active cell. It then moves back one cell to the right and populates that cell with the value of =Mode(D2:D & LR & ")".

This code is stopping at "If Not AFFound is Nothing Then" and after a few moments it breaks. Debug shows that the last command It ran was the line before "If Not AFFound is Nothing Then". The Macro has worked fine up until today.

The only changes I made were in the Reference Libraries; I added a couple to the list. (See embedded image)

Any help will be appreciated.

 'Move "Admin Fee" to name field and populate field cell with current tax period
    
    Range("D2").Select
    Range(Selection, Selection.End(xlDown)).Select
    
     Set AFFound = Cells.Find(What:="Admin Fee", After:=ActiveCell, LookIn:=xlFormulas, _
         LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
         MatchCase:=False, SearchFormat:=False)
    If Not AFFound Is Nothing Then
    
    ' the code you want to have if you find it
    
    Cells.Find(What:="Admin Fee", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Selection.Cut
    ActiveCell.Offset(0, 2).Select
    ActiveCell.Value = "1"
    ActiveCell.Offset(0, -3).Select
    ActiveSheet.Paste
    LR = ActiveCell.row - 1
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Formula = "=MODE(D2:D" & LR & ")"
    ActiveCell.Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlTop
    End With
    End If

Open in new window


Access 2007 References
ergenbgrAsked:
Who is Participating?

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

x
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.

Saurabh Singh TeotiaCommented:
This works for me without any problem..

Dim AFFOUND As Range

 Range("D2").Select
    Range(Selection, Selection.End(xlDown)).Select
    
     Set AFFOUND = Cells.Find(What:="Admin Fee", After:=ActiveCell, LookIn:=xlFormulas, _
         LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
         MatchCase:=False, SearchFormat:=False)
    If Not AFFOUND Is Nothing Then
    
    ' the code you want to have if you find it
    
    Cells.Find(What:="Admin Fee", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Selection.Cut
    ActiveCell.Offset(0, 2).Select
    ActiveCell.Value = "1"
    ActiveCell.Offset(0, -3).Select
    ActiveSheet.Paste
    LR = ActiveCell.Row - 1
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Formula = "=MODE(D2:D" & LR & ")"
    ActiveCell.Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlTop
    End With
    End If

Open in new window


Saurabh...

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
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
one important thing is that you need to tell us what's the error message you received? and which line of codes is suspected to cause that error?
ergenbgrAuthor Commented:
As stated in my question:

"This code is stopping at "If Not AFFound is Nothing Then" and after a few moments it breaks. Debug shows that the last command It ran was the line before "If Not AFFound is Nothing Then"."

There is no error code, I use Step Into  (F8) to Debug my code. I get bo error message.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Roy CoxGroup Finance ManagerCommented:
The code could be written more efficiently., you don't need to select the ranges and searcing a specific range would be better.

Can you attach an example workbook
ergenbgrAuthor Commented:
Thank you Mr. Cox.

Unfortunately the data I work with is confidential tax information and I am forbidden by law to share it. However I will try to select 25 records that I can change the names and Tax ID numbers to show you what I am doing and why I am doing it.

May take me a day or two to get that done. Thanks again
ergenbgrAuthor Commented:
Saurabh,

Thanks you for providing the script. Not sure what happened to mine, but yours works so I am using it.

How could I incorporate a "Do While" to have it go through the process until no more "ADMIN FEE" records are found then skip to the next line of code?
Saurabh Singh TeotiaCommented:
Yes..You need to incorporate a DO while loop if you are looking for multiple search values of the same item..

And yours wasn't working because you didn't declared what AFFound was and it assumed to be variant which at times can be misleading for macro to calculate and in the macro i just told what it was and that fixed the problem..

Saurabh...
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.