Solved

Clear work area of selected column

Posted on 2015-01-22
2
70 Views
Last Modified: 2015-01-22
Hello Experts

The macro below collates and copies data from one workbook to another based on the highlighted column fields. Before doing that work, it clears the work area:

cs.Range("A9:XFD99999").Value = ""

I need the code to only clear the data area of highlighted column starting at row 9, not the entire area A9:XFD99999.

This is a continuation of question :
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28588137.html#a40542987


Sub test()

Application.ScreenUpdating = False
Dim cw, nw As Workbook
Dim cs, ns As Worksheet

Set cw = ActiveWorkbook
Set cs = cw.Sheets(1)

cs.Range("A9:XFD99999").Value = ""

lastrow = cs.Cells(cs.Rows.Count, "A").End(xlUp).Row 'important for sheet names check
If lastrow < 10 Then
    lastrow = 10
End If
lastcol = cs.Cells(7, cs.Columns.Count).End(xlToLeft).Column 'to check how many files to open and read

    addedcounter = 0
'For cols = 2 To lastcol Step 1
    'open file
For Each col In Selection.EntireColumn.Columns
 cols = col.Column
 
    Set nw = Workbooks.Add(cs.Cells(7, cols).Value & "\" & cs.Cells(8, cols).Value)
    For Each foundsheet In nw.Sheets
        foundsheet_bool = False
        For foundrows = 10 To (lastrow + addedcounter) Step 1
            If foundsheet.Name = CStr(cs.Cells(foundrows, 1).Value) Then
                'found correct sheet, now find col_y last number
                foundsheet_bool = True
                lasty = foundsheet.Cells(foundsheet.Rows.Count, "y").End(xlUp).Row
                lasts = foundsheet.Cells(foundsheet.Rows.Count, "s").End(xlUp).Row
                If lasty > lasts Then
                    cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasty, 25).Value
                Else
                    cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasts, 19).Value
                End If
            End If
        Next
       
        If Not (foundsheet_bool) Then
               
                cs.Cells(lastrow + addedcounter, 1).Value = foundsheet.Name
                For foundrows = 10 To (lastrow + addedcounter) Step 1
                    If foundsheet.Name = CStr(cs.Cells(foundrows, 1).Value) Then
                        'found correct sheet, now find col_y last number
                        lasty = foundsheet.Cells(foundsheet.Rows.Count, "y").End(xlUp).Row
                        lasts = foundsheet.Cells(foundsheet.Rows.Count, "s").End(xlUp).Row
                        If lasty > lasts Then
                            cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasty, 25).Value
                        Else
                            cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasts, 19).Value
                        End If
                    End If
                Next
                addedcounter = addedcounter + 1
        End If
    Next
    nw.Close
       
Next

    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("A10:EW" & lastrow + addedcounter)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With

Application.ScreenUpdating = True
End Sub
0
Comment
Question by:bikeski
2 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 40564240
Ouch, let this last part slip through my hands. Here's the updated code:

Sub test()

Application.ScreenUpdating = False
Dim cw, nw As Workbook
Dim cs, ns As Worksheet

Set cw = ActiveWorkbook
Set cs = cw.Sheets(1)

lastrow = cs.Cells(cs.Rows.Count, "A").End(xlUp).Row 'important for sheet names check
If lastrow < 10 Then
    lastrow = 10
End If
lastcol = cs.Cells(7, cs.Columns.Count).End(xlToLeft).Column 'to check how many files to open and read

    addedcounter = 0
'For cols = 2 To lastcol Step 1
    'open file
For Each col In Selection.EntireColumn.Columns
 cols = col.Column
	cs.Range(cs.Cells(9, cols), cs.Cells(9999, cols)).Value = ""
    Set nw = Workbooks.Add(cs.Cells(7, cols).Value & "\" & cs.Cells(8, cols).Value)
    For Each foundsheet In nw.Sheets
        foundsheet_bool = False
        For foundrows = 10 To (lastrow + addedcounter) Step 1
            If foundsheet.Name = CStr(cs.Cells(foundrows, 1).Value) Then
                'found correct sheet, now find col_y last number
                foundsheet_bool = True
                lasty = foundsheet.Cells(foundsheet.Rows.Count, "y").End(xlUp).Row
                lasts = foundsheet.Cells(foundsheet.Rows.Count, "s").End(xlUp).Row
                If lasty > lasts Then
                    cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasty, 25).Value
                Else
                    cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasts, 19).Value
                End If
            End If
        Next
        
        If Not (foundsheet_bool) Then
                
                cs.Cells(lastrow + addedcounter, 1).Value = foundsheet.Name
                For foundrows = 10 To (lastrow + addedcounter) Step 1
                    If foundsheet.Name = CStr(cs.Cells(foundrows, 1).Value) Then
                        'found correct sheet, now find col_y last number
                        lasty = foundsheet.Cells(foundsheet.Rows.Count, "y").End(xlUp).Row
                        lasts = foundsheet.Cells(foundsheet.Rows.Count, "s").End(xlUp).Row
                        If lasty > lasts Then
                            cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasty, 25).Value
                        Else
                            cs.Cells(foundrows, cols).Value = foundsheet.Cells(lasts, 19).Value
                        End If
                    End If
                Next
                addedcounter = addedcounter + 1
        End If
    Next
    nw.Close
        
Next

    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("A10:EW" & lastrow + addedcounter)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With

Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Closing Comment

by:bikeski
ID: 40564839
Thanks Kimputer, that did it.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

746 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

12 Experts available now in Live!

Get 1:1 Help Now