Easiest Way to do this Importing method?

D

D

Hey guys- been searching for a while now, and all the posts are a little
more complex than I think this needs to be. Can you help?

Basically, I have a CSV file that I need to import into another 'Template'
file. But- here's the key- not EVERY column in the CSV file needs to import
into my template. Also, the columns in the CSV file are NOT in the same
order they need to be in the Template file. So, I need to be able to pick
and choose which columns import and go where. Does that make sense? Is there
an easy 'import mapping' system to use or any other way to do this? Like I
said, everything Im seeing is looking pretty complicated- more than I think
it needs to be. This seems as if it'd be fairly easy to accomplish, I'm just
not finding answers I need.
Thanks for any help.
D
 
D

D

Hmmm- not sure about this. Someone was telling me about 'linking' files or
columns from one sheet to another. Is this easily done and can work in my
situation? How do I do it if it can work?
thanks
D
 
N

NickHK

D,
You could query the text file then.
Check out Data>get External data>New database query and tailor to your needs
for the text file.

NickHK
 
D

D

ok, this may sound silly here, but-
The CSV file isn't a text file per se', it's an Excel CSV file. Does that
matter? I am checking that out now- thanks Nick.
D
 
N

NickHK

D,
It's still text, just indicating that it is Comma Separated.

Alternative way is Data>Import Text file, select which columns to drop, then
rearrange your columns in the required order.

NickHK
 
D

D

Hmmm- I dont see that option. I go to Data>Import I select my file, select
my sheet, and then it imports the ENTIRE sheet into the file, not give me
the options as to which columns I want where. Any ideas? What am I missing?
Thanks
D
 
P

Patrik

D said:
Hey guys- been searching for a while now, and all the posts are a little
more complex than I think this needs to be. Can you help?

Basically, I have a CSV file that I need to import into another 'Template'
file. But- here's the key- not EVERY column in the CSV file needs to import
into my template. Also, the columns in the CSV file are NOT in the same
order they need to be in the Template file. So, I need to be able to pick
and choose which columns import and go where. Does that make sense? Is there
an easy 'import mapping' system to use or any other way to do this? Like I
said, everything Im seeing is looking pretty complicated- more than I think
it needs to be. This seems as if it'd be fairly easy to accomplish, I'm just
not finding answers I need.
Thanks for any help.
D

Hello
Well I like programming/VBA approaches because its quick and gives you
good control. I dont know if there is any "Import wizard" that can do
this. To write code that does what you want is not very difficult.
A suggestion would be to read all of the CSV-file into an array and go
from there. It is easier and more effective to manupulate data "In
memory".
Something like this:
Function CSVFileToArray(inFile As String) As Variant
Dim fileHandle As Integer
Dim i As Integer
Dim outArray() As String
Dim tempStr As String
If Dir(inFile) = "" Then 'Check if the file exists
Msgbox "CSVFileToArray: Could not find/open " & inFile
Exit Function
End If
fileHandle = FreeFile()
Open inFile For Input As #fileHandle
i = 0
Do While Not EOF(fileHandle) ' Loop until end of file.
On Error Resume Next
Input #fileHandle, tempStr
On Error GoTo 0
If tempStr <> "" And InStr(",", tempStr) = 0 Then
ReDim Preserve outArray(i)
outArray(i) = tempStr
i = i + 1
End If
Loop
Close #fileHandle
CSVFileToArray = outArray
End Function

Then you can describe what you want from the file in another array
that you initialize early on. Here is an example function from a
program I've done:

Type tFieldDesc
sFieldName As String
iColPos As Integer
bConvert As Boolean
bDate As Boolean
End Type

Function CreateFieldDescrArray() As tFieldDesc()
Static bIsInitialized As Boolean
Static arrFieldDesc(FIELD_DESC_ARRAY_UBOUND) As tFieldDesc
'CAUTION the global constant FIELD_DESC_ARRAY_UBOUND must be set to
'the number of field descriptions minus one!

'An array that contains descriptions of fields
'0 = The name of the field
'1 = The column position
'2 = Does the field need conversion
'3 = Is it a date field
If Not bIsInitialized Then
Dim arrTmp, vElement
arrTmp = Array( _
Array("Provid", "1", "", ""), Array("Status", "2", "Y", ""), _
Array("UPPDRAG", "6", "", ""), Array("Header", "7", "", ""), _
Array("Bestnamn", "8", "", ""), Array("BestAvd", "9", "", ""), _
Array("Memoid", "10", "", ""), Array("UtfAvd", "11", "", ""), _
Array("StartW", "12", "", "Y"), Array("EndW", "13", "", "Y"), _

Dim i As Integer
i = 0
For Each vElement In arrTmp
arrFieldDesc(i).sFieldName = vElement(0)
arrFieldDesc(i).iColPos = CInt(vElement(1))
arrFieldDesc(i).bConvert = False
If vElement(2) <> "" Then
arrFieldDesc(i).bConvert = True
End If
arrFieldDesc(i).bDate = False
If vElement(3) <> "" Then
arrFieldDesc(i).bDate = True
End If
i = i + 1
Next
bIsInitialized = True
End If
CreateFieldDescrArray = arrFieldDesc
End Function

Then you write the main data crunching Sub. Something like this:

Sub GetTheData()
arrFieldDescr = CreateFieldDescrArray()
arrTheWholeFile = CSVFileToArray("C:\Datafile.csv")

j = 0
k = 0
For i = 0 to Ubound(arrTheWholeFile)
If j Mod NUMBER_OF_COLUMNS = 0 Then
'NUMBER_OF_COLUMNS is a constant that has the number of columns
in the
'CSVfile you wish to process
j = 0
else
j = j + 1
end if
sResult = GetElement(arrFieldDescr, arrTheWholeFile(i), j)
if sResult <> "" then
arrResult(k) = sResult
k = k + 1
end if
next
end sub

Function GetElement(arrFieldDescr, sElement, j) as string
GetElement = ""
For each item in arrFieldDescr
if item.iColPos = j then
GetElement = sElement
break
end if
next
end function


This was a very quick repsonse just to get you started along the way I
would reccomend. All variables should be declared of course.

Regards /Patrik
 
T

Tom Ogilvy

After you get it in, you can use code to rearrange and delete columns.

Most of the code can be done by turning the macro recorder on and doing it
manually, then altering the recorded code if necessary.
 
G

Gary Rowe

Select data/import external data/new database query. Select your datasource
or create a new one using the Microsoft text driver. Uncheck the use query
wizard and complete the process. In MSQuery select the columns in whatever
order you want and return to excel.
 
E

Electra

Hi D,
the simple answer is that there is no easy way to do this. But with
some effort Excel will allow you to do this. Programming/formulas
will be required. I am not sure if you still want to do it? But
below is 1 way to do it.

To import this file into Excel it matters which version of Excel you
are using. The way to do this varies depending on the version of
Excel you are working in. I am working in Excel 2003.

in this version of excel choose:
data>import external data>import data>files of type: all file> browse
to your cvs file. click on it and choose open.

when you do that the import wizard opens and you are on step 1 of 3 in
the import wizard screens. on screen 1 choose >delimited file and
click> next

on the step 2 of 3 screens in the import wizard choose:
delimiters: commas(deselect others)
also check: treat consective delimiters as 1
and choose text qualifiers: none
click >next

on the step 3 of 3 screens in the import wizard HIGHLIGHT EACH COLUMN
YOU DON'T WANT imported AND CHOOSE the Column data format radio button
at the bottom> do not import column(skip)> when done click finished

on the next screen (import data screen) select the New worksheet radio
button
(note the properites button at bottom: you can change some properities
but if you are a novice user just leave it) click ok and you are done.

The next time you want to read this data into excel just click>
data>refresh data (at the bottom). Note the file has to have the
exact same structure the next time. More rows are ok but more
columns(commas) are not.

Now moving the columns around. it is best not to move the columns
around on this sheet because when you refesh the sheet all those
changes will be wiped out.

I think a better idea is to make another sheet in the same workbook
and set up formula that rearrange the sheets. For example if sheet2
is the import sheet then on sheet1 in cell a1 you could type
=sheet2!c1 and copy this formula down. that would mmove column C to
Colunm A
 
D

D

FYI-
Found the easiest way to do this is via linking the cells. I hi-light a
column of info from the CSV file, then paste it on another sheet in the
column I want it to go. Then I click the little clip board and select link
cells. And then I use this whole file as a template for all my work. Then
just save as when I'm done. Fastest, easiest way to do it.
Thanks!
D
 
Top