View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Ctech[_41_] Ctech[_41_] is offline
external usenet poster
 
Posts: 1
Default error when sheet doesn't exist...


Can't get it to work..

Sub GetCellsFromWorkbooks()
'
' Macro1 Macro
' Macro recorded 31/10/2005 by Taylor Nelson Sofres plc
'

'

Dim Mnumb
Dim Aworkbook
Dim ActiveWorkbook
Dim SFilename

ActiveWorkbook = Application.ActiveWorkbook.Name
Mnumb = 101


Range("A9").Select

On Error GoTo Errorhandler

For i = 1 To 850

Application.Workbooks.Open Filename:= _
"X:\Users\Shared\GENERAL\Christian S\05.10.28 - Budget packs
Capital expenditure - comments\LBUD2\BFR " & Mnumb & " bud v2.1.xls" _
, UpdateLinks:=0

Aworkbook = Workbooks("BFR " & Mnumb & " bud v2.1.xls").Name

' Taken out the save without password bit

'Application.DisplayAlerts = False
'
' ActiveWorkbook.SaveAs FileName:= _
' "X:\Users\Shared\GENERAL\Christian S\05.10.28 - Budget packs
Capital expenditure - comments\" & Aworkbook _
' , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
' ReadOnlyRecommended:=False, CreateBackup:=False

' Set cost center name


Application.Workbooks(ActiveWorkbook).Activate
ActiveCell = Mnumb



' Copy Capital expenditure numbers

SFilename = "X:\Users\Shared\GENERAL\Christian S\05.10.28 - " & _
"Budget packs - Capital expenditure - comments\Test\BFR" & Mnumb &
bud v2.1.xls"

Application.Workbooks.Open Filename:=SFilename, UpdateLinks:=0

If Not SheetExists("Sch 20") Then GoTo Errorhandler





Application.Workbooks(Aworkbook).Sheets("Sc
20").Range("A11:G25").Copy

' Activate the workbook which the cells are saved in

Application.Workbooks(ActiveWorkbook).Activate
ActiveCell.Offset(0, 2).PasteSpecial Paste:=xlValues
Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveCell.Offset(0, -2).Select



' Select cell for next i + 1

ActiveCell.Offset(14, 0).Select

Application.CutCopyMode = False
Application.Workbooks("BFR " & Mnumb & " bud v2.1.xls").Close
Application.CutCopyMode = False


Mnumb = Mnumb + 1
Next i


Errorhandler:

Mnumb = Mnumb + 1

Resume


End Sub

Function SheetExists(Sh As String, _
Optional wb As Workbook) As Boolean
'-----------------------------------------------------------------
Dim oWs As Worksheet
If wb Is Nothing Then Set wb = ActiveWorkbook
On Error Resume Next
SheetExists = CBool(Not wb.Worksheets(Sh) Is Nothing)
On Error GoTo 0
End Functio

--
Ctec
-----------------------------------------------------------------------
Ctech's Profile: http://www.excelforum.com/member.php...fo&userid=2774
View this thread: http://www.excelforum.com/showthread.php?threadid=48386