Solved

I need a Macro to Clean Unwanted Data (Rows and Columns) from a Spreadsheet

Posted on 2013-12-18
4
319 Views
Last Modified: 2013-12-18
I have a spreadsheet, generated by Accounting, that I want to strip of its extra Excel subtotals and spaces and reduce to a data table for use in Access.

Essentially, I want to, via a macro, Delete Rows in a certain column that start with "%", Delete Rows in a certain column that are blank, and delete columns in a certain row that start with "Q"

Can anyone show me how to do this?

Thank you
0
Comment
Question by:Rex85
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
Comment Utility
Hi Rex,

Here you go, and you should be able to adapt it easily for any other future changes you have. Let me know if you have any questions.

Matt
Sub Rex85()
 Dim i As Long

 Application.ScreenUpdating = False 

'Change the row here accordingly (currently looking in row 3)
 For i = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
  If Left(Cells(3, i).Text, 1) = "Q" Then
   Columns(i).Delete
  End If
 Next
 
 'Change the column here according (currently looking at column D)
 For i = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
  If Left(Range("D" & i).Text, 1) = "%" Then
   Rows(i).Delete
  End If
 Next
 
 'look in the used cells in a column and delete blank columns
 For i = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
  If Application.WorksheetFunction.CountA(Intersect(Columns(i), ActiveSheet.UsedRange)) = 0 Then
   Columns(i).Delete
  End If
 Next

 Application.ScreenUpdating = True
End Sub

Open in new window

EDIT: toggled screen updating for less flicker, in case you didn't know to add it.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
Comment Utility
Not as elegant as mvidas' solution, but maybe you want to give it a shot ;-)

Option Explicit

Sub cleanup()

Dim i, row As Long
Dim colummn As Long
Dim str_array() As String
Dim range_str As String

    ReDim str_array(0)
    row = 1
    
    Do While Cells(row, 1) <> "" ' here column 1 is used -> should be a column that is not empty throughout the entire sheet
    
        For i = 1 To 3 ' assuming the columns are fixed length, otherwise max column count has to be calculated per row
        
            If Mid(Cells(row, i), 1, 1) = "%" Or Mid(Cells(row, i), 1, 1) = "Q" Or Cells(row, i) = "" Then
                
                str_array(UBound(str_array)) = row & ":" & row
                ReDim Preserve str_array(UBound(str_array) + 1)
                
                Exit For
            
            End If
        
        Next i
        
        row = row + 1
    
    Loop
    
    range_str = Join(str_array, ",")
    range_str = Mid(range_str, 1, Len(range_str) - 1)
    
    Range(range_str).Delete

End Sub

Open in new window

0
 

Author Comment

by:Rex85
Comment Utility
Thank you both. I had just successfully tried mvidas' solution and was getting ready to close.

Thanks

Rex
0
 

Author Closing Comment

by:Rex85
Comment Utility
Fantastic. Worked perfectly for what i asked.

Thank you!

Rex
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

763 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

6 Experts available now in Live!

Get 1:1 Help Now