Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default IF Command to Stop Macro

I have this code. It generates a list of files in the c:\ Directory.
then opens these files and copies the information to Book1.xls.

It only does this for the firt file listed. At most there are going t
be 8 xls files in the root directory at all time. I could repeat thi
code 8 time which would be no problem. But If i did this and there wer
only 3 files in the directory, it would come up with an error messag
asking if I want to debug.

Could I therefore use an IF command that states that is ther is no fil
name in Cell A(Whatever) then the macro will stop?

I know its a bit long whinded but any help would be much appreciated!

Sub ListFiles()
Range("A:A").Select
Selection.Delete
Range("A1").Select
F = Dir("C:\*.XLS")
Do While Len(F) 0
ActiveCell.Formula = F
ActiveCell.Offset(1, 0).Select
F = Dir()
Loop
Range("B1").Select
ActiveCell.FormulaR1C1 = "=""C:\"""
Range("C1").Select
ActiveCell.FormulaR1C1 = "=RC[-1]&RC[-2]"
Workbooks.Open Filename:=Range("C1").Value
ActiveWindow.WindowState = xlNormal
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Befo=Workbooks("Book1.xls").Sheets(2)
Sheets("Sheet1").Select
ActiveWindow.WindowState = xlMaximized
MsgBox ("Completed Copying")
End Su

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default IF Command to Stop Macro

Option Explicit
Private root As String
Sub ListFiles()
Dim sFileName As String
Dim index As Long
Dim ws As Worksheet

Set ws = ActiveSheet

root = "C:\"
ws.Range("A:A").Clear

sFileName = Dir(root & "*.XLS")
Do While sFileName < ""
index = index + 1
ws.Cells(index, 1) = sFileName
CopySheets sFileName
sFileName = Dir()
Loop

MsgBox ("Completed Copying")
End Sub
Sub CopySheets(sFileName As String)
Dim wb As Workbook
Set wb = Workbooks.Open(root & sFileName)
ActiveWindow.WindowState = xlNormal
wb.Sheets(1).Copy Befo=ThisWorkbook.Sheets(1)
wb.Close False
End Sub


Patrick Molloy
Microsoft Excel MVP
-----Original Message-----
I have this code. It generates a list of files in the

c:\ Directory. I
then opens these files and copies the information to

Book1.xls.

It only does this for the firt file listed. At most

there are going to
be 8 xls files in the root directory at all time. I

could repeat this
code 8 time which would be no problem. But If i did this

and there were
only 3 files in the directory, it would come up with an

error message
asking if I want to debug.

Could I therefore use an IF command that states that is

ther is no file
name in Cell A(Whatever) then the macro will stop?

I know its a bit long whinded but any help would be much

appreciated!

Sub ListFiles()
Range("A:A").Select
Selection.Delete
Range("A1").Select
F = Dir("C:\*.XLS")
Do While Len(F) 0
ActiveCell.Formula = F
ActiveCell.Offset(1, 0).Select
F = Dir()
Loop
Range("B1").Select
ActiveCell.FormulaR1C1 = "=""C:\"""
Range("C1").Select
ActiveCell.FormulaR1C1 = "=RC[-1]&RC[-2]"
Workbooks.Open Filename:=Range("C1").Value
ActiveWindow.WindowState = xlNormal
Sheets("Sheet1").Select
Sheets("Sheet1").Copy Befo=Workbooks

("Book1.xls").Sheets(2)
Sheets("Sheet1").Select
ActiveWindow.WindowState = xlMaximized
MsgBox ("Completed Copying")
End Sub


---
Message posted from http://www.ExcelForum.com/

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default IF Command to Stop Macro

Thats great. How did you know all that cool code

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default IF Command to Stop Macro

Would it be possible to make the macro name the sheets
1,2,3,4,5,6,7,8......

How would you then make a macro that delete's the newly copied sheets
from the current worksheet?


---
Message posted from http://www.ExcelForum.com/

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default IF Command to Stop Macro

change:

index = index + 1
ws.Cells(index, 1) = sFileName
CopySheets sFileName
activesheet.Name = "Sheet_" & Index ''NEW LINE
sFileName = Dir()


The new line simply changes th ecactive sheet's name to
Sheet_nn
where nn is the index numbe. You might want to use a
function for this in case a sheet with the same name
already exists.

Application.DisplayAlerts=False
worksheets(n).Delete
Application.DisplayAlerts=True

where n can be a number or a sheet name

regards

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
Would it be possible to make the macro name the sheets
1,2,3,4,5,6,7,8......

How would you then make a macro that delete's the newly

copied sheets
from the current worksheet?


---
Message posted from http://www.ExcelForum.com/

.



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
Use Macro To Change Which Macro Assigned To Command Button CVinje Excel Discussion (Misc queries) 0 May 25th 10 09:55 PM
Is there a COMMAND to stop a current process in Excel(Copy/Delete/ Stegra Excel Discussion (Misc queries) 1 January 13th 09 02:52 PM
Macro command djungst Excel Discussion (Misc queries) 3 November 11th 08 05:01 AM
How do I get the"Hour" command to stop rounding? Julie Owen Excel Worksheet Functions 3 March 13th 06 05:00 PM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 11:58 PM.

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"