Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Cant get my code work. Find file or create it

In the Creat_MyFile sub I try to find a file called
2003.xls . If it exists it only opens it and creates a
new worksheet. If it does not exist it is created and a
new worksheet made.

I think my problem is that I am not using the right way
to find my file. Can you please help me out? I would
really appreciate it.

Thank you.

Private Sub CommandButton1_Click()
Dim MiMyDir As String
MiMyDir = "c:\Prices\"
If Dir(MiMyDir, vbDirectory) < "" Then
Create_MyFile MiMyDir
Else
MkDir MiMyDir
Create_MyFile MiMyDir
End If
End Sub

Sub Create_MyFile(MyDir As String)
Dim MiMyFile As String
Dim MyDate, MyYear
MyDate = Now
MiMyFile = Year(MyDate) & ".xls"
If Dir(MiMyFile, vbDirectory) < "" Then
Open_MyFile
Else
Application.Workbooks.Add
ActiveWorkbook.SaveAs MyDir & MiMyFile
ActiveWorkbook.Close
Open_MyFile
End If
End Sub
Sub Open_MyFile()
Dim MyDate
MyDate = Now
Application.Workbooks.Open ("C:\Prices\" & Year
(MyDate) & ".xls")
Dim ShName As String
Dim nChr As Integer
ShName = CStr(Date)
' Replace /
For nChr = 1 To Len(ShName)
If Mid(ShName, nChr, 1) = "/" Then Mid(ShName,
nChr, 1) = "-"
Next
Worksheets.Add(After:=Worksheets
(Worksheets.Count)).Name = ShName
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default Cant get my code work. Find file or create it

Here is a sub I use to open a workbook or activate if already open.
Sub GetWorkbook()
If ActiveCell.Value = "" Then Exit Sub
workbookname = ActiveCell.Value
On Error GoTo OpenWorkbook
Windows("" & workbookname & ".xls").Activate
Exit Sub
OpenWorkbook:
Workbooks.Open(workbookname & ".xls").RunAutoMacros xlAutoOpen
'Workbooks.Open("" & workbookname & ".xls").RunAutoMacros xlAutoOpen
Exit Sub
End Sub
====
Here is a sub I use to create a backup file in the current directory. If the
file exists the error handler takes over.

Sub Backup() 'kept in personal.xls & assigned to toolbar button
On Error GoTo BackupFile
MkDir CurDir & "\Backup"
BackupFile:
With ActiveWorkbook
MyWB = .Path & "\BACKUP\" & .Name
.SaveCopyAs MyWB
.Save
End With
End Sub


"Poseilus" wrote in message
...
In the Creat_MyFile sub I try to find a file called
2003.xls . If it exists it only opens it and creates a
new worksheet. If it does not exist it is created and a
new worksheet made.

I think my problem is that I am not using the right way
to find my file. Can you please help me out? I would
really appreciate it.

Thank you.

Private Sub CommandButton1_Click()
Dim MiMyDir As String
MiMyDir = "c:\Prices\"
If Dir(MiMyDir, vbDirectory) < "" Then
Create_MyFile MiMyDir
Else
MkDir MiMyDir
Create_MyFile MiMyDir
End If
End Sub

Sub Create_MyFile(MyDir As String)
Dim MiMyFile As String
Dim MyDate, MyYear
MyDate = Now
MiMyFile = Year(MyDate) & ".xls"
If Dir(MiMyFile, vbDirectory) < "" Then
Open_MyFile
Else
Application.Workbooks.Add
ActiveWorkbook.SaveAs MyDir & MiMyFile
ActiveWorkbook.Close
Open_MyFile
End If
End Sub
Sub Open_MyFile()
Dim MyDate
MyDate = Now
Application.Workbooks.Open ("C:\Prices\" & Year
(MyDate) & ".xls")
Dim ShName As String
Dim nChr As Integer
ShName = CStr(Date)
' Replace /
For nChr = 1 To Len(ShName)
If Mid(ShName, nChr, 1) = "/" Then Mid(ShName,
nChr, 1) = "-"
Next
Worksheets.Add(After:=Worksheets
(Worksheets.Count)).Name = ShName
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
Code to create dbase file from text file? Hilton Excel Discussion (Misc queries) 0 October 9th 08 10:37 AM
Why does this code not work? rk0909 Excel Discussion (Misc queries) 12 September 4th 08 10:42 PM
HOW DO I FIND A FILE WHEN SHORTCUT DOES NOT WORK pat Excel Discussion (Misc queries) 1 August 2nd 07 06:16 PM
How to create a form to insert a hyerlink.VBA code to create a for karthi Excel Discussion (Misc queries) 0 July 5th 06 11:26 AM
Code does not work on emailed file EFWY Excel Discussion (Misc queries) 3 October 24th 05 02:52 PM


All times are GMT +1. The time now is 02:14 AM.

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

About Us

"It's about Microsoft Excel"