PDA

View Full Version : copying a range from multiple csv files



tomkane
06-30-2011, 06:33 AM
i am new to the forum so i hope i have posted this in the correct place. i am also a bit for a novice when it comes to vba.

i am trying to copy a range from multiple csv files into a new workbook. i have been trying to amend a vba program that does something similar without much luck.

here is what i have been working on. this currently returns the address of the data that i require but with error messages. this has worked fine for excel files but not the csv files.

also, this is a very slow method and i know there must be a much better/quicker way to copy and paste the whole range from each csv file and not go row by row...



Sub Summary_cells_from_Different_Workbooks_1()
Dim CSVFileNames 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




Set Rng = Range("C4:C6261") '<---- Change
'Select the files with GetOpenFilename
CSVFileNames = Application.GetOpenFilename _
(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)
If IsArray(CSVFileNames) = 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(CSVFileNames) To UBound(CSVFileNames)
RwNum = 1
ColNum = 1 + ColNum
FinalSlash = InStrRev(CSVFileNames(FNum), "\")
JustFileName = Mid(CSVFileNames(FNum), FinalSlash + 1)
JustFolder = Left(CSVFileNames(FNum), FinalSlash - 1)
shName = Mid(CSVFileNames(FNum), 131, 6)


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


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

For Each myCell In Rng.Cells
'ColNum = ColNum + 1
RwNum = RwNum + 1
SummWks.Cells(RwNum, ColNum).Value = "=" & PathStr & myCell.Address
'SummWks.Cells(RwNum, ColNum).Paste.Value

Next myCell

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

'If the sheet not exist in the workbook the colum color will be Yellow.
SummWks.Cells(, RwNum).Resize(1, Rng.Cells.Count + 1) _
.Interior.Color = vbYellow
Else
For Each myCell In Rng.Cells
'ColNum = ColNum + 1
RwNum = RwNum + 1
SummWks.Cells(RwNum, ColNum).Formula = _
"=" & PathStr & myCell.Address
Next myCell
End If
On Error GoTo 0
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
End If
End Sub

Kenneth Hobs
07-01-2011, 10:48 AM
Welcome to the forum!

You have not received an answer because of several issues. At least for me anyway. Since you don't have 5 posts yet, you can not post a workbook or zip of files. That is the easiest way to help which some forums don't offer. The second best way is to post code as you did. However, you did not enclose your code in vba code tags so that makes it more difficult to follow the structure.

Conceptually, there are several ways to do what you need, I think.
1. Get all of the file as a text string and parse.
2. Import the CSV file and parse.
3. Use ADO methods.
4. Data Query.

So, try making an example xls and txt files and zip. Post to a free site like box.net or wait until you get 5 posts.

Example for method 2 or 4 using record for Data > From Text > select the file, etc.:
With ActiveSheet.QueryTables.Add(Connection:="TEXT;X:\adds.csv", Destination _
:=Range("$A$1"))
.Name = "adds"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

tomkane
07-01-2011, 10:53 AM
thanks for the advice and the VBA tips. i'll have a go...