Solved

Excel VBA - Modification

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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