Avatar of Ray Ergenbright
Ray Ergenbright
Flag for United States of America asked on

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
Microsoft ExcelVB ScriptMicrosoft Office

Avatar of undefined
Last Comment
Saurabh Singh Teotia

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Saurabh Singh Teotia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

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?
Ray Ergenbright

ASKER
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.
Roy Cox

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ray Ergenbright

ASKER
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
Ray Ergenbright

ASKER
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 Teotia

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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.