VBA Code to Clear Cell Content After Appearance of String

pynergee

New Member
Joined
Apr 29, 2013
Messages
12
Good morning everyone.

I have several workbooks, similar to below, and I am hoping to delete some costing information out of the worksheets. As shown below, the information in Red is what I hope to clear. However, this information does not always originate in the same cell (otherwise I could just do a Range.clear). I am looking for a way to find "Unit Cost" (shown in Blue) in the worksheet, and select the cell ([X-3][Y+1]), where cell XY is the location of "Unit Cost". (The X-3, Y+1 is standard among all worksheets). Then I would like to select a range +6 columns and +15 rows, and clear such, to ensure all the costing information has been removed.

The main problem I am having is locating the "Unit Cost" cell and then moving the selection to the appropriate starting position.

Thank you,
Marc


Please see example below:

Sheet 1

Company Information
Cost Analysis# of Contacts25Lot Size 50025.16 Assembly Rate
Part # XXXXXXXXXXXC-Clip28
April 17, 2013Hand Load15
Set UpRun Burdened Cost per Total
Set Up/HrsRun rate HoursHrsRate Piece Cost
Molding00per hour0.000.000.00 - -
Deflashing180per hour2.7825.16 0.140 69.89
Load Contacts36per hour13.8925.16 0.699 349.44
C-Clip67per hour7.4625.16 0.376 187.76
Stamping0.25480per hour0.251.0425.16 0.065 32.50
Inspection400per hr1.2525.16 0.063 31.45
Handling/Packagin/Shipping/Misc800per hr 0.6325.16 0.031 15.73
TotalXXXXXXXXX XXX XXX
CommentMat'l CostXXX XXX XXX
1. ASSEMBLY INFORMATION 1
2. ASSEMBLY INFORMATION 2 Total CostXXX XXX XXX
STAMP "XXXXXXXX"
3. RECODED BY XXXLast Sold: XXX XXX
4. RECODED XXX
PREFERRED WAY: AUTOMATIC C-CLIPPERActual MarginXXXXXX
600 SERIES INSPECTION PRIOR TO CONTACT LOADING
REVIEWED XXXBreakeven Selling Price XXX
Current
TypeNo.Description QtyU/MCost Unit Cost
Item03214-001(E) C3214-1 MLDG, 600-1-25SD111EA0.54980.5498 0.54982
Item04232-001P-5004232-001P CONT,SOC, 50AU/5AU/25EA0.06481.6188 0.06475
Item03203-001-5003203-001 SOC GDE - 50AU/100CU2EA0.18740.3748 0.18738
Item04203-000-FL04203-000 C-CLIP - AUFL/(50-1025EA0.00320.0810 0.00324
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
Item0.0000#N/A
2.6243

<colgroup><col><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Code:
Cells.Find(What:="Unit Cost", After:=[A1], LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

so something like this:

Code:
Sub Macro1()


Dim uCost As Range


Set uCost = Cells.Find(What:="Unit Cost", After:=[A1], LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        
        Range(Cells(uCost.Row + 1, uCost.Column - 3), Cells(uCost.Row + 16, uCost.Column)).ClearContents
End Sub
 
Last edited:
Upvote 0
Comfy, this worked as intended. I am trying to modify it to apply to all the worksheets in a workbook, and all the workbooks in a folder:

Code:
Option Explicit

Sub Macro1()


    Dim sh As Worksheet
    Const fPath As String = "C:\Users\m.raby\Desktop\Continental Acquisition\Cost Analysis\"
    Dim sName As String
    Dim uCost As Range
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    sName = Dir(fPath & "*.xls*")
    
    Do Until sName = ""
    
    For Each sh In ActiveWorkbook.Worksheets


        Set uCost = Cells.Find(What:="Unit Cost", After:=[A1], LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)


[B]        Range(Cells(uCost.Row + 1, uCost.Column - 3), Cells(uCost.Row + 16, uCost.Column)).ClearContents[/B]
        On Error Resume Next
        sh.Range("A1") = sh.Name
    
    Next sh
    Loop
    
    With Application
        .Calculation = xlAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    
End Sub

When I try to run this, I am getting the error: uCostColumn= < Object Variable or With block variable not set>. Also, is there a way to ignore a worksheet if the cell "Unit Cost" does not exist, since I am trying to perform this in a loop?
 
Upvote 0
Note:

I got the code to run for all worksheets that have "Unit Cost" in it, but it breaks when it does not encounter a cell that contains "Unit Cost", and it seems that's why I was getting the error previously.

Can I ignore a worksheet and move on to the next if it does not encounter that specific "Unit Cost"?

Code:
Sub ClearCosting()Dim sh As Worksheet
Dim uCost As Range
For Each sh In ActiveWorkbook.Sheets
    sh.Activate
    Set uCost = Cells.Find(What:="Unit Cost", After:=[A1], LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)


    Range(Cells(uCost.Row + 1, uCost.Column - 3), Cells(uCost.Row + 16, uCost.Column)).ClearContents
Next sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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