Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
LPS LPS is offline
external usenet poster
 
Posts: 108
Default Modifying a Macro - Help Please

A few months ago, someone (I apologise as I cannot remember who) very kindly
gave me some terrific help with an Excel Macro. The macro was designed to
add a new row, in the same positon, to several workbooks simultaneously. The
workbooks getting the new row have their names hard-coded in the macro (see
code below).

Initially my client did not give me all the details of her situation. What
my client really needs is for that macro to add a specific row, not just to
the identified workbooks in the macro, but to all workbooks within a specific
directory. She has hundreds of workbooks spread throughout about 3 dozen (or
more) directories.

Can this mcaro (below) be modified to autoamtically add whatever row number
to ALL workbooks with the directory? We are using Excel 2000 in a Windows
2000 or XP O/S. Any and all help is hugely appreciated. Cheers - LPS.

Existing Macro:

Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
v = Array("1424511.xls", "1424611.xls", "1424411.xls", _
"141461.xls", "141451.xls", "141251.xls", _
"tu0336001.xls", "tu033000w.xls")
For i = LBound(v) To UBound(v)
Set bk = Workbooks.Open(Filename:=sPath & v(i))
bk.Worksheets(1).Rows(rw).Insert
Next
End Sub


--
LPS
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Modifying a Macro - Help Please

you could try something like:
Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
With Application.FileSearch
.NewSearch
.LookIn = sPath
.Filename = "*.xls"
.MatchTextExactly = False
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If (.Execute 0) Then
For i = 1 To .FoundFiles.Count
Set bk = Workbooks.Open(Filename:=sPath & .FoundFiles(i))
bk.Worksheets(1).Rows(rw).Insert
bk.Close ' probably don't want them all open
Next
End If
End With
End Sub


"LPS" wrote:

A few months ago, someone (I apologise as I cannot remember who) very kindly
gave me some terrific help with an Excel Macro. The macro was designed to
add a new row, in the same positon, to several workbooks simultaneously. The
workbooks getting the new row have their names hard-coded in the macro (see
code below).

Initially my client did not give me all the details of her situation. What
my client really needs is for that macro to add a specific row, not just to
the identified workbooks in the macro, but to all workbooks within a specific
directory. She has hundreds of workbooks spread throughout about 3 dozen (or
more) directories.

Can this mcaro (below) be modified to autoamtically add whatever row number
to ALL workbooks with the directory? We are using Excel 2000 in a Windows
2000 or XP O/S. Any and all help is hugely appreciated. Cheers - LPS.

Existing Macro:

Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
v = Array("1424511.xls", "1424611.xls", "1424411.xls", _
"141461.xls", "141451.xls", "141251.xls", _
"tu0336001.xls", "tu033000w.xls")
For i = LBound(v) To UBound(v)
Set bk = Workbooks.Open(Filename:=sPath & v(i))
bk.Worksheets(1).Rows(rw).Insert
Next
End Sub


--
LPS

  #3   Report Post  
Posted to microsoft.public.excel.programming
LPS LPS is offline
external usenet poster
 
Posts: 108
Default Modifying a Macro - Help Please

Thank you to both "Barnabel" and "Bob Phillips". I will try both suggestions.

Cheers,
--
LPS


"barnabel" wrote:

you could try something like:
Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
With Application.FileSearch
.NewSearch
.LookIn = sPath
.Filename = "*.xls"
.MatchTextExactly = False
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If (.Execute 0) Then
For i = 1 To .FoundFiles.Count
Set bk = Workbooks.Open(Filename:=sPath & .FoundFiles(i))
bk.Worksheets(1).Rows(rw).Insert
bk.Close ' probably don't want them all open
Next
End If
End With
End Sub


"LPS" wrote:

A few months ago, someone (I apologise as I cannot remember who) very kindly
gave me some terrific help with an Excel Macro. The macro was designed to
add a new row, in the same positon, to several workbooks simultaneously. The
workbooks getting the new row have their names hard-coded in the macro (see
code below).

Initially my client did not give me all the details of her situation. What
my client really needs is for that macro to add a specific row, not just to
the identified workbooks in the macro, but to all workbooks within a specific
directory. She has hundreds of workbooks spread throughout about 3 dozen (or
more) directories.

Can this mcaro (below) be modified to autoamtically add whatever row number
to ALL workbooks with the directory? We are using Excel 2000 in a Windows
2000 or XP O/S. Any and all help is hugely appreciated. Cheers - LPS.

Existing Macro:

Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
v = Array("1424511.xls", "1424611.xls", "1424411.xls", _
"141461.xls", "141451.xls", "141251.xls", _
"tu0336001.xls", "tu033000w.xls")
For i = LBound(v) To UBound(v)
Set bk = Workbooks.Open(Filename:=sPath & v(i))
bk.Worksheets(1).Rows(rw).Insert
Next
End Sub


--
LPS

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Modifying a Macro - Help Please

Sub AddRows()
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)

sPath = "C:\test\" '"H:\training\user requests\2007\helen tsang\Macro
Test\"
Set oFSO = CreateObject("Scripting.FileSystemobject")
Set oFolder = oFSO.GetFolder(sPath)

For Each oFile In oFolder.Files
If oFile.Type Like "*Excel*" Then
Set bk = Workbooks.Open(Filename:=oFile.Path)
bk.Worksheets(1).Rows(rw).Insert
bk.Save
bk.Close
End If
Next file

Set bk = Nothing
Set Folder = Nothing
Set oFSO = Nothing

End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"LPS" wrote in message
...
A few months ago, someone (I apologise as I cannot remember who) very
kindly
gave me some terrific help with an Excel Macro. The macro was designed to
add a new row, in the same positon, to several workbooks simultaneously.
The
workbooks getting the new row have their names hard-coded in the macro
(see
code below).

Initially my client did not give me all the details of her situation.
What
my client really needs is for that macro to add a specific row, not just
to
the identified workbooks in the macro, but to all workbooks within a
specific
directory. She has hundreds of workbooks spread throughout about 3 dozen
(or
more) directories.

Can this mcaro (below) be modified to autoamtically add whatever row
number
to ALL workbooks with the directory? We are using Excel 2000 in a Windows
2000 or XP O/S. Any and all help is hugely appreciated. Cheers - LPS.

Existing Macro:

Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
v = Array("1424511.xls", "1424611.xls", "1424411.xls", _
"141461.xls", "141451.xls", "141251.xls", _
"tu0336001.xls", "tu033000w.xls")
For i = LBound(v) To UBound(v)
Set bk = Workbooks.Open(Filename:=sPath & v(i))
bk.Worksheets(1).Rows(rw).Insert
Next
End Sub


--
LPS



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default Modifying a Macro - Help Please

If you don't have the FileSearch available (gone in 2007), or if scripting is
not available, you can use the 'old fashioned' way by simply selecting the
folder and working through the list of .xls files in it using Dir$()

Sub AddRowToAllFiles()
Dim fNames() As String
Dim anyFile As String

Dim sPath As String
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = GetFolder()
If sPath = Application.PathSeparator Then
MsgBox "No folder selected. Exiting.", vbOKOnly, _
"Cannot Continue"
Exit Sub
End If
'initialize array fNames()
ReDim fNames(1 To 1)
'with path determined, get list of .xls file in it
anyFile = Dir$(sPath & "*.xls") ' seed entry
Do While anyFile < ""
If UBound(fNames) < "" Then
ReDim Preserve fNames(1 To UBound(fNames) + 1)
End If
fNames(UBound(fNames)) = anyFile
anyFile = Dir$ ' get next .xls filename
Loop
'test if any files found
If fNames(UBound(fNames)) = "" Then
MsgBox "No .xls files found in path" & vbCrLf & sPath, _
vbOKOnly, "Exiting"
Exit Sub
End If

'v no longer used, array fNames() has data
For i = LBound(fNames) To UBound(fNames)
Set bk = Workbooks.Open(Filename:=sPath & fNames(i))
bk.Worksheets(1).Rows(rw).Insert
Next

End Sub
Private Function GetFolder() As String
'NOTE: returns 1 character string "\" (PathSeparator)
'if the user cancels - test for that on return
'
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
If dlg.Show = -1 Then
GetFolder = dlg.SelectedItems(1)
End If
If Right(GetFolder, 1) < Application.PathSeparator Then
GetFolder = GetFolder & Application.PathSeparator
End If
End Function


"LPS" wrote:

A few months ago, someone (I apologise as I cannot remember who) very kindly
gave me some terrific help with an Excel Macro. The macro was designed to
add a new row, in the same positon, to several workbooks simultaneously. The
workbooks getting the new row have their names hard-coded in the macro (see
code below).

Initially my client did not give me all the details of her situation. What
my client really needs is for that macro to add a specific row, not just to
the identified workbooks in the macro, but to all workbooks within a specific
directory. She has hundreds of workbooks spread throughout about 3 dozen (or
more) directories.

Can this mcaro (below) be modified to autoamtically add whatever row number
to ALL workbooks with the directory? We are using Excel 2000 in a Windows
2000 or XP O/S. Any and all help is hugely appreciated. Cheers - LPS.

Existing Macro:

Sub AddRows()
Dim sPath As String, v As Variant
Dim bk As Workbook, i As Long
Dim ans As Variant
Dim rw As Long

ans = Application.InputBox("Enter the row to add", Type:=1)
If ans = False Then Exit Sub
rw = CLng(ans)
sPath = "H:\training\user requests\2007\helen tsang\Macro Test\"
v = Array("1424511.xls", "1424611.xls", "1424411.xls", _
"141461.xls", "141451.xls", "141251.xls", _
"tu0336001.xls", "tu033000w.xls")
For i = LBound(v) To UBound(v)
Set bk = Workbooks.Open(Filename:=sPath & v(i))
bk.Worksheets(1).Rows(rw).Insert
Next
End Sub


--
LPS



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Modifying a Macro - Help Please

That is frustrating. Why would they take a perfectly useful function out? I
am just starting to convert stuff from Excel 2000 to 2007. Now I am going to
have to deal with things that have always worked not compiling.

"JLatham" wrote:

If you don't have the FileSearch available (gone in 2007), or if scripting is
not available, you can use the 'old fashioned' way by simply selecting the
folder and working through the list of .xls files in it using Dir$()


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Modifying a Macro - Help Please

It is one of the very few things to worry about.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"barnabel" wrote in message
...
That is frustrating. Why would they take a perfectly useful function out?
I
am just starting to convert stuff from Excel 2000 to 2007. Now I am going
to
have to deal with things that have always worked not compiling.

"JLatham" wrote:

If you don't have the FileSearch available (gone in 2007), or if
scripting is
not available, you can use the 'old fashioned' way by simply selecting
the
folder and working through the list of .xls files in it using Dir$()




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
Modifying Macro simplymidori[_2_] Excel Discussion (Misc queries) 3 April 13th 08 04:17 PM
Need help modifying a macro EAHRENS Excel Discussion (Misc queries) 13 March 31st 06 12:22 AM
Help in modifying a filesearch macro! drucey Excel Programming 8 March 24th 06 12:14 PM
Modifying an Old Excel Macro LPS Excel Programming 3 January 9th 06 02:54 PM
Modifying Macro carl Excel Worksheet Functions 3 August 25th 05 08:45 PM


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