?
Solved

Clear work area of selected column

Posted on 2015-01-22
2
Medium Priority
?
80 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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.

771 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