Code to select all used cells on a sheet

richanor

Active Member
Joined
Apr 8, 2006
Messages
291
Hi, I've combined a few bits of code from this board to come up with a macro that will combine a number of workbooks from the same directory into one master workbook. I just need help with selecting the range.

I am trying to select all used cells on the active sheet, except the column headings (Column Headings are rows 1-10, main data starts at row 11). At the moment my code for selecting the range is:

ws.Range("A11:E17").Copy

because in that worksheet there happend to be 7 rows of data. It will always be columns A:E, starting at row 11, but the number of rows will vary. Can somebody help me out with the code I need?

Thanks

Rich
 
I tried to tidy up your code a bit. See if this works for you:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim counter As Long
    counter = 0
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Admin\Annual Leave"
    ChDir strPath
    strExtension = Dir("*.xl*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            .Sheets("Tracker").Range("A11:E" & .Sheets("Tracker").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row).Copy
            wkbDest.Sheets("Annual Leave Tracker").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
            counter = counter + 1
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
    wkbDest.Sheets("Annual Leave Tracker").Columns.AutoFit
    MsgBox counter & " workbooks consolidated. ", , "Consolidation Complete"
End Sub
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
wow thanks very much. i'm getting an error (run-time error 76 Path not Found)

at this line:

ChDir strPath
 
Upvote 0
That seems to work great. A couple of things: each time it closes a file is pauses to ask if I want to keep the data on the clipboard. Is there a way to say "no" automatically?

Also - when it opens the third file, it asks: Attendee Tracker.xlsm is already open. Reopening will cause any changes you made to be discarded. Do you want to reopen Attendee Tracker.xlsm?

(Attendee Tracker is the master workbook which contains the sheet "Annual Leave Tracker)
 
Upvote 0
Try:
Code:
Sub CopyRange()
    Application.ScreenUpdating = False
    Dim counter As Long
    counter = 0
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Admin\Annual Leave"
    ChDir strPath
    strExtension = Dir("*.xl*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        If wkbSource.Name <> "Attendee Tracker.xlsm" Then
            With wkbSource
                .Sheets("Tracker").Range("A11:E" & .Sheets("Tracker").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row).Copy
                wkbDest.Sheets("Annual Leave Tracker").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
                Application.CutCopyMode = False
                counter = counter + 1
                .Close savechanges:=False
            End With
        End If
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
    wkbDest.Sheets("Annual Leave Tracker").Columns.AutoFit
    MsgBox counter & " workbooks consolidated. ", , "Consolidation Complete"
End Sub
 
Upvote 0
Hi Mumps, thanks you so much for your help and persistence! I'm still getting an error as it opens the third worrkbook in the folder - Attendee Tracker.xlsm is already open. Reopening will cause any changes you made to be discarded. Do you want to reopen Attendee Tracker.xlsm? If I click No, I get the error:

Run-Time error '1004': Method 'Open' of object 'Workbooks' failed

If I click Yes, it reopens a fresh version of the file.
 
Upvote 0
Ah - it's OK. I have just worked out that it was trying to combine the master sheet into the master sheet! I've amended it now - working a dream, thanks very much for all your help Mumps

Rich
 
Upvote 0

Forum statistics

Threads
1,215,926
Messages
6,127,731
Members
449,401
Latest member
TTXS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top