thats too fast Rory, will give you a feedback in a few hours.
Main Topics
Browse All TopicsWith Reference To
http://www.experts-exchang
The above macro met my initial requirement. But it fails when i try to append when one of the columns is in date format and
it also gives problem with the number format.
In short if one of the columns contains the date like
Date
12-May-2006
11-May-2006
19-Mar-2006
it shows its numeric value.
Similarly is there a way that when i copy i can show the number with only two decimal values
So for ex, when i run the macro i get
14.56777 when actually i need 14.56
It seems that the format that has been set in the initial worksheets do not get copied.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
No worries. As you can see, that basically does a copy and paste operation so you will get exactly what was there before including formatting. If you just wanted to copy over the displayed values, you could use:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
If lngCounter = 1 Then
Set rngData = Intersect(wks1.UsedRange, wks1.Columns(1))
Else
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A2:A65536"))
End If
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
If lngOutputRow > 1 Then wksSummary.Cells(lngOutput
lngOutputRow = lngOutputRow + 1
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
Regards,
Rory
The above doesnt work with the date field...
The data in the excel sheet is for example 24/09/1980 which is displayed as 24-Sep-1980 and whn it is copied to the new sheet using the macro it gets displayed as 29488 and if i try to edit the cell it shoes the value of 24/09/1980 .
There is one more modificn which i require is that , there are two date fields and in one column i want to get the age in that.
For ex: Col E and Col F contain the dates 12/01/2001 and 25/06/2006 i want to get the difference in age in those columns.
I tried both.
but am sticking with the second one
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
wksSummary.Cells(lngOutput
or ex: Col E and Col F contain the dates 12/01/2001 and 25/06/2006
where Col E is the bithdate and Col F is the start date, i want to know the Age for that person as on the value in Col F.
so for ex in the above scenario it would be five (5) years
"OK and where do you want it put?"
Lets say the last column .
"Are all the date fields in a specific place? If so, the code could simply format those columns at the end."
Are you asking whether the date field are in specific columns ? If so, rite now they are in Column C,D and F.
Both versions of your code displays the date fields in excel serial number
24/09/1980 as 29488
04/04/1980 as 29315
and so on
OK then, try this:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
If lngCounter = 1 Then
Set rngData = Intersect(wks1.UsedRange, wks1.Columns(1))
Else
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A2:A65536"))
End If
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
With wksSummary.Cells(lngOutput
.Value = rngCell
.Numberformat = "#,##0.00;-#,##0.00;0.00"
End With
With wksSummary.Cells(lngOutput
.Value = rngCell.Offset(0, 1)
.NumberFormat = "dd-mmm-yyyy"
End With
With wksSummary.Cells(lngOutput
.Value = rngCell.Offset(0, 3)
.NumberFormat = "dd-mmm-yyyy"
End With
If lngOutputRow > 1 Then
With wksSummary.Cells(lngOutput
.Value = strCompany
.Offset(0,1).FormulaR1C1 = "=Year(RC[-2])-year(RC[-3]
.Offset(0,1).NumberFormat = "0 ""years"""
End With
End If
lngOutputRow = lngOutputRow + 1
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
Regards,
Rory
Still not working.
Please check the sample upload file that i have uploaded at
http://rapidshare.de/files
The format is not as we are working on..but this is the actual data that we are trying to append.
All the columns that are shown are needed. ( There are more but i have deleted it )
Try this:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
If lngCounter = 1 Then
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A3:A65536"))
Else
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A4:A65536"))
End If
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
If Len(rngCell) > 0 Then
With wksSummary.Cells(lngOutput
.Value = rngCell
.NumberFormat = "#,##0.00;-#,##0.00;0.00"
End With
With wksSummary.Cells(lngOutput
.Value = rngCell.Offset(0, 1)
.NumberFormat = "dd-mmm-yyyy"
End With
With wksSummary.Cells(lngOutput
.Value = rngCell.Offset(0, 3)
.NumberFormat = "dd-mmm-yyyy"
End With
If lngOutputRow > 1 Then
With wksSummary.Cells(lngOutput
.Value = strCompany
.Offset(0, 1).Value = DatePart("yyyy", rngCell.Offset(0, 3)) - DatePart("yyyy", rngCell.Offset(0, 2)) - IIf(DatePart("m", rngCell.Offset(0, 3)) < DatePart("m", rngCell.Offset(0, 2)), 1, 0)
.Offset(0, 1).NumberFormat = "0 ""years"""
End With
End If
lngOutputRow = lngOutputRow + 1
End If
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
Regards,
Rory
PS "Not working" is not that useful - if you can tell me in what way it is failing that will speed things up!
Well, I confess I'm stumped - I tested that several times on the sample file you provided and it *always* formatted the dates. However, I note your comment: "The format is not as we are working on..but this is the actual data that we are trying to append." Does this mean you are amending the code to fit the actual format you are working on?
Regards,
Rory
What i meant is i have not given the whole data which spans 20-30 columns. The format is the same.
"Does this mean you are amending the code to fit the actual format you are working on?"
We are adding the number of cells and thats it. The dateformat is as in the uploaded file.
We are working with excel 2002. Would that make any difference ?
If you ran it already from a new blank workbook then that shouldn't be the problem. Any chance that you could post a copy of the workbook you have run it from (where it didn't work), including the code, and a full sample of the dataset it runs against - you can reduce the number of rows, but please keep all the columns? That way I can just double-check that I haven't missed anything obvious!
Regards,
Rory
One problem in getting the age ( date difference)
The dates are displayed as 24-Sep-80 and 01-Mar-06
so when i use the code
wksSummary.Cells(lngOutput
i get a Type mismatch error. ( the values that they get are the proper date fields i confirmed that )
The dates are in the columns C and H.
So i want to paste the difference between the too in column J .
That line creates a new workbook and sets the output sheet as the first sheet in that new workbook.
This version prompts for a save name and location for the new workbook:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Dim varFileName
Application.ScreenUpdating
Set wksSummary = Workbooks.Add.Sheets(1)
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
If lngCounter = 1 Then
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A3:A65536"))
Else
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A4:A65536"))
End If
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
If Len(rngCell) > 0 Then
With wksSummary.Cells(lngOutput
.Value = rngCell
.NumberFormat = "#,##0.00;-#,##0.00;0.00"
End With
With wksSummary.Cells(lngOutput
.Value = rngCell.Offset(0, 1)
.NumberFormat = "dd-mmm-yyyy"
End With
With wksSummary.Cells(lngOutput
.Value = rngCell.Offset(0, 3)
.NumberFormat = "dd-mmm-yyyy"
End With
If lngOutputRow > 1 Then
With wksSummary.Cells(lngOutput
.Value = strCompany
.Offset(0, 1).Value = DatePart("yyyy", rngCell.Offset(0, 3)) - DatePart("yyyy", rngCell.Offset(0, 2)) - IIf(DatePart("m", rngCell.Offset(0, 3)) < DatePart("m", rngCell.Offset(0, 2)), 1, 0)
.Offset(0, 1).NumberFormat = "0 ""years"""
End With
End If
lngOutputRow = lngOutputRow + 1
End If
Next rngCell
wbk.Close False
Next lngCounter
varFileName = application.getsaveasfilen
if varFileName <> False then
wksSummary.Parent.SaveAs varFileName
end if
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
HTH
Rory
Business Accounts
Answer for Membership
by: roryaPosted on 2006-08-29 at 04:24:01ID: 17410784
Hi,
er As String) = False (lngCounte r)) Row, 1) Row, 2) Row, 3) Row, 4) = strCompany = True
Try this:
Sub ListSubFiles(strParentFold
Dim fs As FileSearch
Dim lngCounter As Long, lngRow As Long, lngOutputRow As Long
Dim wbk As Workbook, wks1 As Worksheet, wks2 As Worksheet, wksSummary As Worksheet
Dim rngData As Range, rngCell As Range
Dim strCompany As String
Application.ScreenUpdating
Set wksSummary = ActiveWorkbook.ActiveSheet
lngOutputRow = 1
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = strParentFolder
.SearchSubFolders = True
.FileName = "*.xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks
.Execute
' Loop through all the found files
For lngCounter = 1 To .FoundFiles.Count
Set wbk = Workbooks.Open(.FoundFiles
Set wks1 = wbk.Sheets("Detail sheet")
Set wks2 = wbk.Sheets("Summary sheet")
strCompany = wks2.Range("C3")
' Assumes there will always be data in column 1
If lngCounter = 1 Then
Set rngData = Intersect(wks1.UsedRange, wks1.Columns(1))
Else
Set rngData = Intersect(wks1.UsedRange, wks1.Range("A2:A65536"))
End If
For Each rngCell In rngData
' Write columns 1, 2 and 4 and company name to sheet 1 of this workbook
rngCell.Copy wksSummary.Cells(lngOutput
rngCell.Offset(0, 1).Copy wksSummary.Cells(lngOutput
rngCell.Offset(0, 3).Copy wksSummary.Cells(lngOutput
If lngOutputRow > 1 Then wksSummary.Cells(lngOutput
lngOutputRow = lngOutputRow + 1
Next rngCell
wbk.Close False
Next lngCounter
Set rngData = Nothing
Set wks1 = Nothing
Set wks2 = Nothing
Set wbk = Nothing
End With
Set fs = Nothing
Application.ScreenUpdating
End Sub
HTH
Rory