View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Ron de Bruin Ron de Bruin is offline
external usenet poster
 
Posts: 11,123
Default Trouble selecting multiple sheets using RDB code

Hi John

The copy2 page is for worksheets in the same workbook

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

Try my add-in fist maybe you like it (it have a option for non continuous ranges and a sheet filter option)
http://www.rondebruin.nl/merge.htm

The code above not on this moment but it is possible to change


If you want no empty rows if there is no Assay 2 you can change this part of the code

For FNum = LBound(FileNameXls) To UBound(FileNameXls)
FinalSlash = InStrRev(FileNameXls(FNum), "\")
JustFileName = Mid(FileNameXls(FNum), FinalSlash + 1)
JustFolder = Left(FileNameXls(FNum), FinalSlash - 1)

For I = 1 To 2

ColNum = 1
RwNum = RwNum + 1

'build the formula string
JustFileName = WorksheetFunction.Substitute(JustFileName, "'", "''")
PathStr = "'" & JustFolder & "\[" & JustFileName & "]" & ShName & I & "'!"

On Error Resume Next
SheetCheck = ExecuteExcel4Macro(PathStr & Range("A1").Address(, , xlR1C1))
If Err.Number < 0 Then
Err.Clear
On Error GoTo 0
RwNum = RwNum - 1
Else
'copy the workbook name in column A
SummWks.Cells(RwNum, 1).Value = JustFileName

For Each myCell In Rng.Cells
ColNum = ColNum + 1
SummWks.Cells(RwNum, ColNum).Formula = _
"=" & PathStr & myCell.Address
Next myCell
End If

Next I
Next FNum





--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"John Yab" wrote in message ...
Hi Ron and Dave,

Thankyou very, very much.
I made some modifications and have more modifications to make but it does
work.
I changed the "Set Rng" so that it collects data from cells that display
test data returned better to see with.
I changed "ColNum" to = 2, now, so that the second column is now available
to add the sheet name.
Just under:

'copy the workbook name in column A
SummWks.Cells(RwNum, 1).Value = JustFileName

I added:

'copy the worksheet name in column B
SummWks.Cells(RwNum, 2).Value = ShName & I

and now the sheet name (either "Assay 1" or "Assay 2") displays in the
summary.
A remaining issue that I will solve is the summary returned shows 2 rows
for each: Assay 1 and Assay 2 regardless that in some cases there will not be
an Assay 2 test. I will add code to find all the blanks in a column and then
delete the entire row of those with blanks. I am very grateful for your help.

Ron, you have an excellent web and one of your pages gives me and idea that
maybe I should have used ?? instead:
http://www.rondebruin.nl/copy2.htm
As I think I understand it, this method opens (briefly) each workbook to
collect data and rapidly closes each workbook instead of the method that I am
using. I really like the part in your code on this page that goes like this:

"Replace this line:
If sh.Name < DestSh.Name Then

With:
If LCase(Left(sh.Name, 4)) = "week" Then

If you want to copy only from sheets with a name that start with week."

This looks like something that might work for my situation but your web page
discribes this method in the context of copying a range such as:
"Set CopyRng = sh.Range("A1:G1")"

In my situation I am try to do 2 things: 1) collect data from a lot of non
continuous ranges (Range("B1,F1,F2,J1,J2,J3,F46,B67,F11:F23,M11:M23" ) and 2)
collect data from worksheets that start with "Assay."
Is there a way to combine these 2 situations to make it work with your
methods on your web page: http://www.rondebruin.nl/copy2.htm?
--
John Yab


"Dave Peterson" wrote:

There is a chance that there would be no formulas in the summary worksheet.

Adding a couple of lines will prevent showing an error to the user:

on error resume next 'added
With SummWks.Cells.SpecialCells(xlCellTypeFormulas)
.Replace what:="=", replacement:="=", _
lookat:=xlPart, searchorder:=xlByRows, _
MatchCase:=False
End With
on error goto 0 'added



Ron de Bruin wrote:

Hi John

Here it is
I hade a strange problem if one of the sheets not exist, it will not calculate the formula when there is no data in the cell.
But the replace part at the end of the macro fix this (thanks to Dave Peterson)

Test this one for two sheets named Assay 1 and Assay 2

Sub Summary_cells_from_Different_Workbooks_Test()
Dim FileNameXls As Variant
Dim SummWks As Worksheet
Dim ColNum As Integer
Dim myCell As Range, Rng As Range
Dim RwNum As Long, FNum As Long, FinalSlash As Long
Dim ShName As String, PathStr As String
Dim SheetCheck As String, JustFileName As String
Dim JustFolder As String
Dim I As Long

ShName = "Assay " 'Test for two sheets named Assay 1 and Assay 2
Set Rng = Range("A1,D5:E5,Z10") '<---- Change

'Select the files with GetOpenFilename
FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", _
MultiSelect:=True)

If IsArray(FileNameXls) = False Then
'do nothing
Else
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'Add a new workbook with one sheet for the Summary
Set SummWks = Workbooks.Add(1).Worksheets(1)

'The links to the first workbook will start in row 2
RwNum = 1

For FNum = LBound(FileNameXls) To UBound(FileNameXls)
FinalSlash = InStrRev(FileNameXls(FNum), "\")
JustFileName = Mid(FileNameXls(FNum), FinalSlash + 1)
JustFolder = Left(FileNameXls(FNum), FinalSlash - 1)

For I = 1 To 2 'test for Sheet1 and Sheet2

ColNum = 1
RwNum = RwNum + 1
'copy the workbook name in column A
SummWks.Cells(RwNum, 1).Value = JustFileName

'build the formula string
JustFileName = WorksheetFunction.Substitute(JustFileName, "'", "''")
PathStr = "'" & JustFolder & "\[" & JustFileName & "]" & ShName & I & "'!"

On Error Resume Next
SheetCheck = ExecuteExcel4Macro(PathStr & Range("A1").Address(, , xlR1C1))
If Err.Number < 0 Then
Err.Clear
On Error GoTo 0
'If the sheet not exist in the workbook the row color will be Yellow.
SummWks.Cells(RwNum, 1).Resize(1, Rng.Cells.Count + 1) _
.Interior.Color = vbYellow
Else
For Each myCell In Rng.Cells
ColNum = ColNum + 1
SummWks.Cells(RwNum, ColNum).Formula = _
"=" & PathStr & myCell.Address
Next myCell
End If

Next I
Next FNum

' Use AutoFit to set the column width in the new workbook
SummWks.UsedRange.Columns.AutoFit

MsgBox "The Summary is ready, save the file if you want to keep it"

With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With

With SummWks.Cells.SpecialCells(xlCellTypeFormulas)
.Replace what:="=", replacement:="=", _
lookat:=xlPart, searchorder:=xlByRows, _
MatchCase:=False
End With

End If
End Sub

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

"Ron de Bruin" wrote in message ...
Hi John

It is evening here so I must go to soon but will create a example for you tomorrow


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"John Yab" wrote in message ...

Hi Ron,

Thank you so much for your reply.
Values would be OK.
I have been trying on my own and have the code a little closer and the
new code is below and I have attached the new macro/workbook also.
You may have better ideas or methods though.

Sub Experiment7()
Dim FileNameXls As Variant
Dim SummWks As Worksheet
Dim ColNum As Integer
Dim myCell As Range, Rng As Range
Dim RwNum As Long, FNum As Long, FinalSlash As Long
Dim ShName As String, PathStr As String
Dim SheetCheck As String, JustFileName As String
Dim JustFolder As String

'ShName = "Assay 1" '<---- the name of the sheet searched
Set Rng = Range("B1,F1,F2,J1,J2,J3,F46,B67,F11:F23,M11:M23")
'<---- the cells to collect

'Select the files with GetOpenFilename
FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files,
*.xl*", MultiSelect:=True)

If IsArray(FileNameXls) = False Then
'do nothing

'Add a new workbook with one sheet for the summary
Set SummWks = Workbooks.Add(1).Worksheets(1)
SummWks.Name = "Summary"

'The links to the first workbook will start in row 2
RwNum = 1

'Create the array of filenames
For FNum = LBound(FileNameXls) To UBound(FileNameXls)
ColNum = 2
RwNum = RwNum + 1
FinalSlash = InStrRev(FileNameXls(FNum), "\")
JustFileName = Mid(FileNameXls(FNum), FinalSlash + 1)
JustFolder = Left(FileNameXls(FNum), FinalSlash - 1)

'copy the workbook name in column A
SummWks.Cells(RwNum, 1).Value = JustFileName
'ssssss
'copy the worksheet name in column A
SummWks.Cells(RwNum, 2).Value = "Assay 1"
'sssssss
'build the formula string
JustFileName = WorksheetFunction.Substitute(JustFileName,
"'", "''")
PathStr = "'" & JustFolder & "\[" & JustFileName & "]" &
"Assay 1" & "'!"

On Error Resume Next
'SheetCheck = ExecuteExcel4Macro(PathStr &
Range("A1").Address(, , xlR1C1))
'If Err.Number < 0 Then

'If the sheet name that is being searched does not
exist in the workbook the row color will be Yellow.
'Cells(RwNum, 1).Resize(1, Rng.Cells.Count +
1).Interior.Color = vbYellow

'Fill the collected data to the new workbook
'Else
For Each myCell In Rng.Cells
ColNum = ColNum + 1
SummWks.Cells(RwNum, ColNum).Formula = _
"=" & PathStr & myCell.Address
Next myCell
'End If
On Error GoTo 0

Next FNum

Columns("D:I").Insert Shift:=xlToRight


I had to cut the bottom of my paste because:
"The text that you have entered is too long (11885 characters). Please
shorten it to 10000 characters long."


Thanks,
John

Ron de Bruin;591638 Wrote:
Hi John

Do you want formula links or are values also OK

--

Regards Ron de Bruin
'Ron's Excel Tips' (http://www.rondebruin.nl/tips.htm)


"John Yab" wrote in message
...
I need a little help to modify some RDB code, please. Below is the
code. It
collects data from multiple workbooks that contain the worksheet
named:
"Assay 1". Could someone please assist me to modify the code so that
it would
collect data from the same workbooks for the times when a book also
contains
the worksheet named: "Assay 2".

Sub Experiment4()
Dim FileNameXls As Variant
Dim SummWks As Worksheet
Dim ColNum As Integer
Dim myCell As Range, Rng As Range
Dim RwNum As Long, FNum As Long, FinalSlash As Long
Dim ShName As String, PathStr As String
Dim SheetCheck As String, JustFileName As String
Dim JustFolder As String

ShName = "Assay 1" '<---- the name of the sheet searched
Set Rng = Range("B1,F1,F2,J1,J2,J3,F46,B67,F11:F23,M11:M23")
'<----
the cells to collect

'Select the files with GetOpenFilename
FileNameXls = Application.GetOpenFilename(filefilter:="Excel
Files,
*.xl*", MultiSelect:=True)

If IsArray(FileNameXls) = False Then
'do nothing

'Change ScreenUpdating and calculation to increase speed of
macro
Else
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

'Add a new workbook with one sheet for the summary
Set SummWks = Workbooks.Add(1).Worksheets(1)
SummWks.Name = "Summary"

'The links to the first workbook will start in row 2
RwNum = 1

'Create the array of filenames
For FNum = LBound(FileNameXls) To UBound(FileNameXls)
ColNum = 1
RwNum = RwNum + 1
FinalSlash = InStrRev(FileNameXls(FNum), "\")
JustFileName = Mid(FileNameXls(FNum), FinalSlash + 1)
JustFolder = Left(FileNameXls(FNum), FinalSlash - 1)

'copy the workbook name in column A
SummWks.Cells(RwNum, 1).Value = JustFileName

'build the formula string
JustFileName = WorksheetFunction.Substitute(JustFileName,
"'",
"''")
PathStr = "'" & JustFolder & "\[" & JustFileName & "]" &
ShName
& "'!"

On Error Resume Next
SheetCheck = ExecuteExcel4Macro(PathStr &