Solved

Excel VBA - Modification

Posted on 2013-02-06
2
317 Views
Last Modified: 2013-02-06
I have a button that clears out rows on a tab named 'Retrieval'.  It clears out entire row.   However now I need it to noly goto column AH and not clear anything past that as there are formulas in the columns after that.  I think this is the code.  Not sure how to make it stop at AH??

Sub clearRetrievalData(Optional bPrompt As Boolean = True)
Dim wksRetrieval As Worksheet
Dim lastRow As Long
Dim xMsg As Long

    If bPrompt Then
        xMsg = MsgBox("This will clear ALL data on the Retrieval Tab - Continue?", vbYesNo, "Press YES to continue NO to abort clear command")
        If xMsg = vbNo Then Exit Sub
    End If
   
    Set wksRetrieval = ThisWorkbook.Worksheets("Retrieval")
    'clear Retrieval tab of any data
    lastRow = wksRetrieval.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
   
    'clear move/keep switch, data, and color back to normal
    If lastRow >= 15 Then
        With wksRetrieval.Range("15:15", wksRetrieval.Cells(lastRow, 1).EntireRow)
            .ClearContents
            .Interior.Color = -4142
        End With
    End If
End Sub
0
Comment
Question by:elwayisgod
2 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 38860412
Try this update, adding an Intersect to your range definition:

intersect(wksRetrieval.Range("15:15", wksRetrieval.Cells(lastRow, 1).EntireRow),wksRetrieval.columns("A:AH"))


Sub clearRetrievalData(Optional bPrompt As Boolean = True)
Dim wksRetrieval As Worksheet
Dim lastRow As Long
Dim xMsg As Long

    If bPrompt Then
        xMsg = MsgBox("This will clear ALL data on the Retrieval Tab - Continue?", vbYesNo, "Press YES to continue NO to abort clear command")
        If xMsg = vbNo Then Exit Sub
    End If
   
    Set wksRetrieval = ThisWorkbook.Worksheets("Retrieval")
    'clear Retrieval tab of any data
    lastRow = wksRetrieval.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
   
    'clear move/keep switch, data, and color back to normal
    If lastRow >= 15 Then
        With intersect(wksRetrieval.Range("15:15", wksRetrieval.Cells(lastRow, 1).EntireRow),wksRetrieval.columns("A:AH"))
            .ClearContents
            .Interior.Color = -4142
        End With
    End If
End Sub

Open in new window

0
 

Author Closing Comment

by:elwayisgod
ID: 38861630
Perfect.  Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now