Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Macro that adds records

I haven't made any macro's in Excel but I was wondering if the following is
possible.

I would like to do the following:

1. Open Excel
2. Run a macro that:
3. Opens a particular file called c:\MyFile.xls, and
4. Opens every Excelfile in a particular folder called C:\ToBeProcessed\
5. For every Excelfile it should copy only the 2nd row (the 1st one with
data), and
6. Paste the whole row after the last record of c:\MyFile.xls

Thanks in advance for any help,
john


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 91
Default Macro that adds records

Yes, that's possible.

john wrote:
I haven't made any macro's in Excel but I was wondering if the following is
possible.

I would like to do the following:

1. Open Excel
2. Run a macro that:
3. Opens a particular file called c:\MyFile.xls, and
4. Opens every Excelfile in a particular folder called C:\ToBeProcessed\
5. For every Excelfile it should copy only the 2nd row (the 1st one with
data), and
6. Paste the whole row after the last record of c:\MyFile.xls

Thanks in advance for any help,
john


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Macro that adds records

And how would I make such a macro? Do you have some sample code?
john

schreef in bericht
ups.com...
Yes, that's possible.

john wrote:
I haven't made any macro's in Excel but I was wondering if the following
is
possible.

I would like to do the following:

1. Open Excel
2. Run a macro that:
3. Opens a particular file called c:\MyFile.xls, and
4. Opens every Excelfile in a particular folder called C:\ToBeProcessed\
5. For every Excelfile it should copy only the 2nd row (the 1st one with
data), and
6. Paste the whole row after the last record of c:\MyFile.xls

Thanks in advance for any help,
john




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro that adds records


Hi,

Ron's site might help

http://www.rondebruin.nl/ado.htm#folder

VBA Noo

--
VBA Noo
-----------------------------------------------------------------------
VBA Noob's Profile: http://www.excelforum.com/member.php...fo&userid=3383
View this thread: http://www.excelforum.com/showthread.php?threadid=57323

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 91
Default Macro that adds records

Make a module in MyFile.xls and paste the following code into it:

Option Explicit

Public sThisFile As String
Public sThisPath As String
Public sFolderName As String
Public sFileName As String
Public dblRows As Double, d As Double
Public FS

Public Sub TransferData()
sThisPath = ActiveWorkbook.Path & "\"
sThisFile = ActiveWorkbook.Name

sFolderName = "C:\ToBeProcessed\"
Dir (sFolderName)

Set FS = Application.FileSearch
With FS
.LookIn = sFolderName
.SearchSubFolders = False
.Filename = "*.xls"
If .Execute() 0 Then
'if there are .xls files in the folder then open each one
and copy the row over
For d = 1 To .FoundFiles.Count
sFileName = .FoundFiles(d)
sFileName = Strings.Replace(sFileName, sFolderName, "")
Workbooks.Open Filename:=sFolderName & sFileName
Range("2:2").EntireRow.Select
Selection.Copy
Workbooks(sThisFile).Activate
dblRows = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Paste
Destination:=Worksheets(ActiveSheet.Name).Range(db lRows + 1 & ":" &
dblRows + 1)
Application.DisplayAlerts = False
Workbooks(sFileName).Close SaveChanges:=False
Application.DisplayAlerts = True
Next d
Else
'else, alert the user that no .xls files could be found
MsgBox "No .xls files found...", vbExclamation, "File(s)
Not Found"
End
End If
End With
End Sub

That should do what you describe.


VBA Noob wrote:
Hi,

Ron's site might help

http://www.rondebruin.nl/ado.htm#folder

VBA Noob


--
VBA Noob
------------------------------------------------------------------------
VBA Noob's Profile: http://www.excelforum.com/member.php...o&userid=33833
View this thread: http://www.excelforum.com/showthread...hreadid=573235




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Macro that adds records

Thanks a lot! That works like a charm.

Somehow the dblRows variable didn't work because the same record in the
destination worksheet got overwritten every time. I was very happy to find
out that I had to alter the related line:

ActiveSheet.Paste Destination:=Worksheets(ActiveSheet.Name).Range(db lRows +
1 & ":" & dblRows + 1)

to

ActiveSheet.Paste Destination:=Worksheets(ActiveSheet.Name).Range(d + 1 &
":" & d + 1).

Thanks again,
john

schreef in bericht
ups.com...
Make a module in MyFile.xls and paste the following code into it:

Option Explicit

Public sThisFile As String
Public sThisPath As String
Public sFolderName As String
Public sFileName As String
Public dblRows As Double, d As Double
Public FS

Public Sub TransferData()
sThisPath = ActiveWorkbook.Path & "\"
sThisFile = ActiveWorkbook.Name

sFolderName = "C:\ToBeProcessed\"
Dir (sFolderName)

Set FS = Application.FileSearch
With FS
.LookIn = sFolderName
.SearchSubFolders = False
.Filename = "*.xls"
If .Execute() 0 Then
'if there are .xls files in the folder then open each one
and copy the row over
For d = 1 To .FoundFiles.Count
sFileName = .FoundFiles(d)
sFileName = Strings.Replace(sFileName, sFolderName, "")
Workbooks.Open Filename:=sFolderName & sFileName
Range("2:2").EntireRow.Select
Selection.Copy
Workbooks(sThisFile).Activate
dblRows = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Paste
Destination:=Worksheets(ActiveSheet.Name).Range(db lRows + 1 & ":" &
dblRows + 1)
Application.DisplayAlerts = False
Workbooks(sFileName).Close SaveChanges:=False
Application.DisplayAlerts = True
Next d
Else
'else, alert the user that no .xls files could be found
MsgBox "No .xls files found...", vbExclamation, "File(s)
Not Found"
End
End If
End With
End Sub

That should do what you describe.


VBA Noob wrote:
Hi,

Ron's site might help

http://www.rondebruin.nl/ado.htm#folder

VBA Noob


--
VBA Noob
------------------------------------------------------------------------
VBA Noob's Profile:
http://www.excelforum.com/member.php...o&userid=33833
View this thread:
http://www.excelforum.com/showthread...hreadid=573235




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Macro that adds records

Hi

I like to add this

There are problems with Application.FileSearch
Better to use Dir or FileSystemObject

See this page for example code
http://www.rondebruin.nl/copy3.htm


--
Regards Ron de Bruin
http://www.rondebruin.nl



"john" wrote in message ...
Thanks a lot! That works like a charm.

Somehow the dblRows variable didn't work because the same record in the destination worksheet got overwritten every time. I was
very happy to find out that I had to alter the related line:

ActiveSheet.Paste Destination:=Worksheets(ActiveSheet.Name).Range(db lRows + 1 & ":" & dblRows + 1)

to

ActiveSheet.Paste Destination:=Worksheets(ActiveSheet.Name).Range(d + 1 & ":" & d + 1).

Thanks again,
john

schreef in bericht ups.com...
Make a module in MyFile.xls and paste the following code into it:

Option Explicit

Public sThisFile As String
Public sThisPath As String
Public sFolderName As String
Public sFileName As String
Public dblRows As Double, d As Double
Public FS

Public Sub TransferData()
sThisPath = ActiveWorkbook.Path & "\"
sThisFile = ActiveWorkbook.Name

sFolderName = "C:\ToBeProcessed\"
Dir (sFolderName)

Set FS = Application.FileSearch
With FS
.LookIn = sFolderName
.SearchSubFolders = False
.Filename = "*.xls"
If .Execute() 0 Then
'if there are .xls files in the folder then open each one
and copy the row over
For d = 1 To .FoundFiles.Count
sFileName = .FoundFiles(d)
sFileName = Strings.Replace(sFileName, sFolderName, "")
Workbooks.Open Filename:=sFolderName & sFileName
Range("2:2").EntireRow.Select
Selection.Copy
Workbooks(sThisFile).Activate
dblRows = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Paste
Destination:=Worksheets(ActiveSheet.Name).Range(db lRows + 1 & ":" &
dblRows + 1)
Application.DisplayAlerts = False
Workbooks(sFileName).Close SaveChanges:=False
Application.DisplayAlerts = True
Next d
Else
'else, alert the user that no .xls files could be found
MsgBox "No .xls files found...", vbExclamation, "File(s)
Not Found"
End
End If
End With
End Sub

That should do what you describe.


VBA Noob wrote:
Hi,

Ron's site might help

http://www.rondebruin.nl/ado.htm#folder

VBA Noob


--
VBA Noob
------------------------------------------------------------------------
VBA Noob's Profile: http://www.excelforum.com/member.php...o&userid=33833
View this thread: http://www.excelforum.com/showthread...hreadid=573235






  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Macro that adds records

Thanks, that will help me to get into writing macro's myself...
john

"VBA Noob" schreef in
bericht ...

Hi,

Ron's site might help

http://www.rondebruin.nl/ado.htm#folder

VBA Noob


--
VBA Noob
------------------------------------------------------------------------
VBA Noob's Profile:
http://www.excelforum.com/member.php...o&userid=33833
View this thread: http://www.excelforum.com/showthread...hreadid=573235



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
Is there a macro which adds selected/highlighted cells? Ed Excel Worksheet Functions 2 September 28th 05 01:36 PM
macro that adds left and right footers Keith Excel Programming 4 August 2nd 05 03:02 PM
Macro that adds then deletes Brian McGuire Excel Programming 3 December 8th 03 11:28 PM
Macro adds 1 to Cell in excel Xispo[_2_] Excel Programming 4 November 12th 03 07:07 PM
Macro which adds new spreadsheets matraxus Excel Programming 0 August 25th 03 10:52 AM


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