help with code?/

cprince

Board Regular
Joined
Jul 4, 2006
Messages
67
Hi All ,

Can someone see where my code is going wrong? I m Stuck!

I think this is the line it doesnt like, but im not 100%...

For Each ws In Workbooks(filetoopen).Sheets

Thanks

Code:
Private Sub Workbook_Open()

    MsgBox "Please select a Sample tracking list to report on"
    
    Do
    filetoopen = Application _
    .GetOpenFilename("Workbooks (*.XLS), *.XLS")
    If filetoopen <> 0 Then
    Workbooks.Open Filename:=filetoopen
    procedure2 (filetoopen)
    Exit Sub
    Else
    MsgBox "You didnt select a file, Please try again"
   End If
   Loop
   
End Sub
Sub procedure2(filetoopen)
Dim ws As Worksheet
Dim LastR As Range
Dim rg As Range
Dim error1 As String

'grabs all the data from the worksheet and puts it in sheet1 of the template
For Each ws In Workbooks(filetoopen).Sheets
       Set LastR = Workbooks("SampleTracking1").Sheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1)
       If IsEmpty(Range("A1")) Then Set LastR = Range("A1")
        ws.Range("AG2:AG3000").Value = ws.Name
        ws.Range("A1:AG3000").Copy
        LastR.PasteSpecial xlPasteValues
        LastR.PasteSpecial xlPasteFormats
   
Next
end sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi

I think the variable filetoopen will contain the full path as well as the filename. You will have to remove that component to use in the workbooks(filetoopen).sheets statement.

Also, shouldn't the workbooks("SampleTracking1") be workbooks("sampletracking1.xls")


Tony
 
Upvote 0
Its a Template, so it will actually be called Sampletracking1 when the New template has started.

I will try and refer to only the filename. and see how I go.. Any ideas on how to do that?
 
Upvote 0
Hi

When it opens it should be the activeworkbook. You should then be able to extract the name (activeworkbook.name). As you use it a couple of times, probably best to put it into a variable.


Tony
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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