Solved

Excel VBA - Modification

Posted on 2013-02-06
2
316 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

708 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

11 Experts available now in Live!

Get 1:1 Help Now