Hi,
This is my first post, so don't be too rough.
My understanding of Excel (and programming) in general, is limited. I have a broad understanding of concepts, but limited hands on experience.
So, to the problem at hand:
Using VB in Excel, I am trying to open a whole bunch of user selected files (.TBL extension) and copy data from all of these files into a single workbook (in fact, a single worksheet).
There are some formatting issues with the TBL files that I managed to solve, but I am having difficulting copying the data from each file in the array to the new workbook.
My problem lies in how I can use the value stored in the array to activate that workbook. Everything I try seems to return a "subscript out of range error" , or a "data type mismatch". I can't find anything on converting the array data type "variant" to a file name type "String", and using the array variable name as my workbook reference isn't working.
Some issues I would llike to point out:
I'm not sure how to appropriately reference between workbooks and worksheets to begin with, so I tend to end up refer to everything with a sort of "absolute reference" ie, there is proabably a shorthand way of coding my activation procedures (fill me in please!)
I *like* coding various subs that I call with a main routine, but I don't think that I have fully grasped how various declarations interact in this kind of scenario. For example, it would nice to know how I can declare global variables in a main routine and then vary those values in the various subs, but for some reason I can't get it happening. I am aware of Private and Public elements, but not too sure when (or where) I can/should use them.
Here is a copy of the pertinent code (extraneous is snipped), you might even recognise some of it from these boards:
'fetches all the file names to an array
Sub SelectOpenCopy()
Dim i As Long
Dim wbkToCopy As Workbook
Dim vaFiles As Variant
vaFiles = Application.GetOpenFilename("TBL Files (*.tbl), *.tbl", _
Title:="Select files", MultiSelect:=True)
addNew
'sequentially sorting throught the array files
If IsArray(vaFiles) Then
For i = LBound(vaFiles) To UBound(vaFiles)
Set wbkToCopy = Workbooks.Open(Filename:=vaFiles(i))
'the below subs do all the neccessary formatting to each file
createSheets
formatSheets
dataSort
'this sub is what i would like to copy and paste the data with (and is where my problem lies)
dataTransferNext i
End If
End Sub
Sub addNew()
'the file that I wish to populate
Set NewBook = Workbooks.Add
With NewBook
.Title = "Converted TBL Files"
.Subject = "Traffic Counter Database"
End With
Application.DisplayAlerts = False
Worksheets("sheet2").Delete
Worksheets("sheet3").Delete
Application.DisplayAlerts = True
Worksheets(Sheets(1).Name).Name = "completedCounterData"
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
NewBook.SaveAs Filename:=fName
End Sub
ANy help would be gretaly appreciated.
Thanks,
-mudz
This is my first post, so don't be too rough.
My understanding of Excel (and programming) in general, is limited. I have a broad understanding of concepts, but limited hands on experience.
So, to the problem at hand:
Using VB in Excel, I am trying to open a whole bunch of user selected files (.TBL extension) and copy data from all of these files into a single workbook (in fact, a single worksheet).
There are some formatting issues with the TBL files that I managed to solve, but I am having difficulting copying the data from each file in the array to the new workbook.
My problem lies in how I can use the value stored in the array to activate that workbook. Everything I try seems to return a "subscript out of range error" , or a "data type mismatch". I can't find anything on converting the array data type "variant" to a file name type "String", and using the array variable name as my workbook reference isn't working.
Some issues I would llike to point out:
I'm not sure how to appropriately reference between workbooks and worksheets to begin with, so I tend to end up refer to everything with a sort of "absolute reference" ie, there is proabably a shorthand way of coding my activation procedures (fill me in please!)
I *like* coding various subs that I call with a main routine, but I don't think that I have fully grasped how various declarations interact in this kind of scenario. For example, it would nice to know how I can declare global variables in a main routine and then vary those values in the various subs, but for some reason I can't get it happening. I am aware of Private and Public elements, but not too sure when (or where) I can/should use them.
Here is a copy of the pertinent code (extraneous is snipped), you might even recognise some of it from these boards:
'fetches all the file names to an array
Sub SelectOpenCopy()
Dim i As Long
Dim wbkToCopy As Workbook
Dim vaFiles As Variant
vaFiles = Application.GetOpenFilename("TBL Files (*.tbl), *.tbl", _
Title:="Select files", MultiSelect:=True)
addNew
'sequentially sorting throught the array files
If IsArray(vaFiles) Then
For i = LBound(vaFiles) To UBound(vaFiles)
Set wbkToCopy = Workbooks.Open(Filename:=vaFiles(i))
'the below subs do all the neccessary formatting to each file
createSheets
formatSheets
dataSort
'this sub is what i would like to copy and paste the data with (and is where my problem lies)
dataTransferNext i
End If
End Sub
Sub addNew()
'the file that I wish to populate
Set NewBook = Workbooks.Add
With NewBook
.Title = "Converted TBL Files"
.Subject = "Traffic Counter Database"
End With
Application.DisplayAlerts = False
Worksheets("sheet2").Delete
Worksheets("sheet3").Delete
Application.DisplayAlerts = True
Worksheets(Sheets(1).Name).Name = "completedCounterData"
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
NewBook.SaveAs Filename:=fName
End Sub
ANy help would be gretaly appreciated.
Thanks,
-mudz