Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Open files with a variable name in a folder get name in B1 and

Hi Les,

Try replacing your code with the following
version:

'===========
Option Explicit

'--------------
Private Sub RenameFiles()
Dim WB As Workbook
Dim oFSO As Object
Dim oFolder As Object
Dim ofile As Object
Dim oFiles As Object
Dim sPath As String
Dim sName As String
Dim iLen As Long
Dim sStr As String
Dim sNewName As String
Dim sOldName As String
Dim sSheet As String '<<===== CHANGE
Const sCell As String = "A1" '<<===== CHANGE
Const sExt As String = ".xls" '<<===== CHANGE

sPath = "C:\Users\Norman\" _
& "Documents\Test" '<<===== CHANGE

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set oFiles = oFolder.Files

iLen = Len(sExt)
On Error GoTo XIT
Application.ScreenUpdating = False
For Each ofile In oFiles
With ofile
sName = .Name
If UCase(Right(sName, iLen)) = UCase(sExt) Then
sOldName = .path
Set WB = Workbooks.Open(Filename:=sOldName)
With WB
sStr = .Sheets(1).Range(sCell).Value
.Close SaveChanges:=False
End With
sNewName = Replace(sOldName, .Name, sStr & sExt)
Name sOldName As sNewName
End If
End With
Next ofile

XIT:
Set oFiles = Nothing
Set oFolder = Nothing
Set oFSO = Nothing
Application.ScreenUpdating = True
End Sub
'<===========

---
Regards.
Norman


"Les" wrote in message
...
Hi Norman, as per my last msg, there are definately values in A1 and the
sheets are not named Sheet1.

They one book's sheet 1 is named "Hauptseite-1" and the other is named
"Liste Befund-1".

These are all the worksheets(1)

--
Les


"Norman Jones" wrote:

Hi Les,

There are always only two files in this folder and the sheet1 are named
differently One file, sheet1 is named "Liste Befund-1" and the other
file
sheet1 is named "Hauptseite-1".


Given that there are only two files, we can
abbreviate the tests: open each of the files
and manually verify that both contain a sheet
named "Sheet1", without any unintentional
leading or trailing spaces, and that each
includes a valid filename in the A1 cell on
that sheet.



---
Regards.
Norman

"Les" wrote in message
...
Hi Norman,

I am the one that should be apologising and complementing you on your
language skills...

There are always only two files in this folder and the sheet1 are named
differently One file, sheet1 is named "Liste Befund-1" and the other
file
sheet1 is named "Hauptseite-1".

I sincerely apologiese for not seeing this before.

--
Les



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
Finding files in a folder. Variable not defined error. Ayo Excel Discussion (Misc queries) 1 March 14th 08 01:14 PM
User selection of folder and open all .xls files within folder Barb Reinhardt Excel Programming 4 April 14th 07 01:41 PM
Open files in folder - skip if already open Steph[_3_] Excel Programming 6 March 25th 05 06:49 PM
Opening files from a variable (todays' date) folder name Hari[_3_] Excel Programming 2 June 10th 04 04:03 PM
open all files in a folder and ... walt Excel Programming 5 August 7th 03 02:23 AM


All times are GMT +1. The time now is 08:00 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"