basic excel 2003 macro no longer works in excel 2007

umy

New Member
Joined
Sep 21, 2010
Messages
7
I always used the below simple macro for combining data from multiple sheets into one and it worked a treat. However, we have now upgraded to from office 2003 to office 2007 and it has stopped working. Can you let me know what I need to change and if possible, maybe even make the change for me as my excel is as good as my Klingon, not very good at all J
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Thank you
<o:p></o:p>
<o:p></o:p>
Sub Get_Value_From_All()
<o:p></o:p>
Dim wbSource As Workbook
Dim wbThis As Workbook
Dim rToCopy As Range
Dim rNextCl As Range
Dim lCount As Long
Dim bHeaders As Boolean
Dim n As Long
Dim i As Long
Dim wsFrom As Worksheet
Dim wsTo As Worksheet
<o:p></o:p>
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
<o:p></o:p>
On Error Resume Next
Set wbThis = ThisWorkbook

search:
With .FileSearch
.NewSearch
'Change path to suit
.LookIn = "C:\newtest\"
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'Workbooks in folder
For lCount = 1 To .FoundFiles.Count ' Loop through all.
'Open Workbook x and Set a Workbook variable to it
Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
For i = 1 To wbSource.Worksheets.Count - 1
Set wsFrom = wbSource.Worksheets(i)
Set wsTo = wbThis.Worksheets(i)
Set rToCopy = wsFrom.Range("B2", wsFrom.Range("B" & Rows.Count).End(xlUp)).Resize(, 20)
Set rNextCl = wsTo.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
If rNextCl.Row > 2 Then
rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, rToCopy.Columns.Count).Copy rNextCl
'no headers so copy
Else
rToCopy.Copy wsTo.Range("B2")
End If
Next i
wbSource.Close False 'close source workbook
Next lCount
Else
MsgBox "No workbooks found"
End If
End With
<o:p></o:p>
For i = 1 To wbThis.Worksheets.Count - 1
With wbThis.Worksheets(i)
n = .Cells(Rows.Count, 2).End(xlUp).Row
.Range("A2:A" & n).ClearContents
.Range("A3").Value = 1
.Range("A3:A" & n).DataSeries , Type:=xlLinear, Step:=1
End With
Next i
<o:p></o:p>
On Error GoTo 0
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
<o:p></o:p>
End Sub
 
Welcome to the Forum,

Have you checked your setttings for Enabling macros? Use teh Office Button top left of the screen, then Excel Options bottom right then

Then the Trust center then Trust Cneter settings then MACRO Settings you might have a default to disable all macros
 
Upvote 0
Excellent, thank you :-)

Can anyone help my editing the few lines in my code to do this? I have now idea i'm afraid :-(
 
Upvote 0
Try the untested:

Code:
Sub Get_Value_From_All()
    Dim wbSource As Workbook
    Dim wbThis As Workbook
    Dim rToCopy As Range
    Dim rNextCl As Range
    Dim strPath As String
    Dim FileName As String
    Dim bHeaders As Boolean
    Dim n As Long
    Dim i As Long
    Dim wsFrom As Worksheet
    Dim wsTo As Worksheet
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
        On Error Resume Next
        Set wbThis = ThisWorkbook
        strPath = "C:\newtest\"
        FileName = Dir(strPath & "*.xls")
        Do While FileName <> ""
'           Open Workbook x and Set a Workbook variable to it
            Set wbSource = Workbooks.Open(FileName:=strPath & FileName, UpdateLinks:=0)
            For i = 1 To wbSource.Worksheets.Count - 1
                Set wsFrom = wbSource.Worksheets(i)
                Set wsTo = wbThis.Worksheets(i)
                Set rToCopy = wsFrom.Range("B2", wsFrom.Range("B" & Rows.Count).End(xlUp)).Resize(, 20)
                Set rNextCl = wsTo.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
                If rNextCl.Row > 2 Then
                    rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, rToCopy.Columns.Count).Copy rNextCl
'                   no headers so copy
                Else
                    rToCopy.Copy wsTo.Range("B2")
                End If
            Next i
            wbSource.Close False 'close source workbook
            FileName = Dir    ' Get next entry.
        Loop
        For i = 1 To wbThis.Worksheets.Count - 1
            With wbThis.Worksheets(i)
                n = .Cells(Rows.Count, 2).End(xlUp).Row
                .Range("A2:A" & n).ClearContents
                .Range("A3").Value = 1
                .Range("A3:A" & n).DataSeries , Type:=xlLinear, Step:=1
            End With
        Next i
        On Error GoTo 0
        .ScreenUpdating = True
        .DisplayAlerts = True
        .EnableEvents = True
    End With
End Sub
 
Upvote 0
Hi

Thank you for that.

The good news is that it didn't cause any errors.

The bad news is that it didn't actually do anything. It only copied a random number "1" into the sheet which i ran the macro from, didn't bring across anything else at all. In fact, I'm not even sure where it got the "1" from? :-)

Please persevere... :-)
 
Upvote 0
Ok, did a bit more research after commenting out the line you suggested.

When I left the original xls files in the foler, I would get an object undefined error and no data would be copied across. It would be blank in the macro sheet, but i noticed that it was running in compatibility mode.

I then opened the data xls files and saved them as xlsx files. The data did now get copied across, but the first column, which contains numbers, is blank - I also noticed that it generates a "subscript out of range" error on the open sheet. It also loses all formatting etc.
I remember that the original guy who was helping me out with this had to do something special in the macro to autonumber the first column or something, maybe this is causing the error???


I am not able to upload any attachments here, but if you let me have your email, then I am happy to email you the test sheets to see if you can make sense of them?

Once again, many thanks for all your help! :-)
 
Upvote 0
Which line of code cause the subscript out of range error?

This is doing the numbering:

Code:
For i = 1 To wbThis.Worksheets.Count - 1
    With wbThis.Worksheets(i)
        n = .Cells(Rows.Count, 2).End(xlUp).Row
        .Range("A2:A" & n).ClearContents
        .Range("A3").Value = 1
        .Range("A3:A" & n).DataSeries , Type:=xlLinear, Step:=1
    End With
Next i

I would expect at least to see a 1 in A3. The numbers should extend to the last filled row in column B.
 
Upvote 0
Hi there

It doesn't actually show me the line which contains the error, i don't get a debug option, only the fact that it "Runtime error "9" - Subscript out of range". Also, the whole of column A is blank, absolutely nothing there.

It would be good to get some sort of robust solution again, as saving all of the sheets as xlsx will be very time consuming indeed, there are quite a few when we run the actual macro.

once again, thank you for all your help so far :-)

You can see the orginal posts from the guy who helped me create this at the below forum :
http://www.ozgrid.com/forum/showthread.php?t=145384&page=3
 
Upvote 0

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