Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
sgl sgl is offline
external usenet poster
 
Posts: 80
Default Copy all Range Names to Personal.xls

Hi All,
Trying to set up a Common Database which will update all users'
"Personal.xls" file so that Templates can be updated from this common
database and common Range Names can be applied to multiple Templates. The
code is in the Workbook_Open of Personal.xls and will automatically update
Range Names and Database.

Through the help from Ron de Bruin (thank you !!) site and from MS KB I have
managed to get what I want except the refreshing of the Range names. The code
stops while looping through the Defined Names and gives me a Run-Time error
1004 - Applicaton-Defined or object defined error. I have copied the listing
below. Can someone please help as going spare trying to find the error.

Private Sub Workbook_Open()

Dim Basebook As Workbook
Dim Mybook As Workbook
Dim MyFiles() As String
Dim SourceRange As Range
Dim DestRange As Range
Dim RNum As Long
Dim Fnum As Long
Dim SourceRcount As Long
Dim FNames As String
Dim MyPath As String
Dim FilesInPath As String
Dim SaveDriveDir As String
Dim ShtName As Variant
Dim N As Integer
Dim Str As String
Dim x
Dim Sht As Worksheet

'
--------------------------------------------------------------------------------
' Save current directory drive
SaveDriveDir = CurDir

'
--------------------------------------------------------------------------------
' Fill in the Path\Folder where the files are
MyPath = "\\Nt_Gulf\d\AlphaDB"

' Add a slash at the end if the user forgot it
If Right(MyPath, 1) < "\" Then
MyPath = MyPath & "\"
End If

'
--------------------------------------------------------------------------------
' If there is no Excel file in the folder exit sub
FilesInPath = Dir(MyPath & "*.xls")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'
--------------------------------------------------------------------------------
' On Error GoTo CleanUp
Application.ScreenUpdating = False
Set Basebook = ThisWorkbook

' Clear all cells in the first sheet
Basebook.Worksheets(1).Cells.Clear

RNum = 1
' -------------------------------------------------------------------------
' Fill the array (MyFiles) with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath < ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop
' -------------------------------------------------------------------------

Set Mybook = Workbooks.Open(MyPath & MyFiles(Fnum))

' -------------------------------------------------------------------------
' You want to copy the range from all sheets in this Array
ShtName = Array("Transactions", "Signatories", "Crew", "Banks", _
"Banks", "Departments", "BankManagementAccts", "AccountsDB", _
"Suppliers", "CurrencyAbrv", "VslsAndCompanies")

' -------------------------------------------------------------------------
' Loop through all files in the array (myFiles) and create the
Personal.xls workbook sheets contents

If Fnum 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)
Set Mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
For N = LBound(ShtName) To UBound(ShtName)
Str = ShtName(N)

'If the sheet does not exist in the workbook it will be created
in the basebook
'You can delete this line if you are sure all sheets exist in
the basebook
If Not SheetExists(Str, Basebook) Then
Basebook.Worksheets.Add.Name = Str

'Copy the range from the sheet in mybook into the sheet basebook
If SheetExists(Str, Mybook) Then

Set SourceRange = Mybook.Worksheets(ShtName(N)).UsedRange
Set DestRange = Basebook.Worksheets(ShtName(N)).UsedRange

DestRange.Cells.Clear

SourceRange.Copy DestRange

End If
Next N
Next Fnum
End If

' -------------------------------------------------------------------------
' Populate the array with all Workbook Names from AlphaDB and to be copied
' Personal.xls workbook

Mybook.Activate

' Loop through all of the defined names in the active
' workbook.
For Each x In Mybook.Names
' Add each defined name from the active workbook to
' the target workbook ("Book2.xls").
' "x.value" refers to the cell references the
' defined name points to.
Workbooks("Personal.xls").Names.Add Name:=x.Name, _
RefersTo:=x.Value
Next x

' -------------------------------------------------------------------------
' Windows("PERSONAL.XLS").Visible = True
Mybook.Activate
Debug.Print ActiveWorkbook.Name

Windows("PERSONAL.XLS").Visible = False

Mybook.Close False

CleanUp:

Application.ScreenUpdating = True

End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Carry range names into copy of template Don Rouse Excel Programming 4 June 6th 05 08:47 PM
copy range on every worksheet (diff names) to a master worksheet (to be created) Bernie[_2_] Excel Programming 2 September 22nd 04 03:30 PM
copy range on every worksheet (diff names) to a master worksheet (to be created) Bernie[_3_] Excel Programming 0 September 22nd 04 03:26 PM
copy sheet1 and name sheets using names from a range DL[_3_] Excel Programming 2 September 2nd 03 12:58 PM


All times are GMT +1. The time now is 09:18 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"