web-it support and consulting - bulk dbase (dbf) exports via microsoft excel vba code and csv file...

15
Web - IT Support and Consulting Web - IT Support and Consulting A human vision on the digital world

Upload: dirk-cludts

Post on 21-Jun-2015

181 views

Category:

Data & Analytics


0 download

DESCRIPTION

If you're working with dBase files and want them to migrate to MS SQL Server or export them into CSV files? If so, this handy white paper will be important for you.

TRANSCRIPT

Page 1: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Web-IT Support and Consulting

A human vision on the digital world

Page 2: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Still using dBase?

One would be surprised how many dBase databases are still active in companies.

But what if you intend to migrate toMS SQL Server or just want to export all these files into a *.CSV

format for further use?

Not an easy task, lots of effort and really time consuming.That’s why I want to share the next solution with you

(download the slide share file to use the copy-paste function).

Enjoy reading and for any additional question,just give a ring to my team (info at the last page).

Cheeriooo,Webby (that’s me)

Page 3: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Why this solution

My boss and his (my) team always have creative

solutions and like to share them.

Fastest way to convert hundreds of dBase files in 1 go

Countering the fact that MS Excel doesn’t use the system settings (delimiters) in VBA code (more on this can be found on the web)

Solid basis to use the *.CSV files in MS Excelor as bulk import files in MS SQL Server

Page 4: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 1

Yep, this looks easy to me.

Create a new MS Excel file and save it with the option

Define a sheet where people can enter the location of their sources and destinations

Page 5: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 2.1

Hey guys, wait a second. By default the ‘DEVELOPER’ menu is not activated.

To do so follow these steps:1. Click on File >> Options >> Customize Ribbon

2. Select in the right pane the ‘Developer’ tab.3. Click OK.

Create a new macro (module) in the same MS Excel file(that’s why you had to save as a Macro-enabled workbook)

Page 6: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 2.2

Easy peasy

Create a new macro (module) in the same MS Excel file(that’s why you had to save as a Macro-enabled workbook)

Page 7: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 3.1

Green textgives all the

information on the code

Paste all the code on the following pages one after the other in the module Window

Sub ExportdBaseFiles()

' -------------------------------------------------------

' Definition of variables

' -------------------------------------------------------

Dim OriginalFolderName As String

Dim OriginalFileName As String

Dim SaveAsFolderName As String

Dim SaveAsFileName As String

Dim LastCol As Integer

Dim LastRow As Long

Dim LoopInRows As Long

Dim LoopInCols As Integer

Dim SaveAsFileNumber As Integer

Dim DelimiterSign As String

Dim SaveAsExtension As String

' -------------------------------------------------------

Start of SUB

Download the presentationto be able to copy-paste.

Page 8: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 3.2

Green textgives all the

information on the code

Paste this code after the previous one

' -------------------------------------------------------

' Initializing variables

' -------------------------------------------------------

' Refer as much as possible to cell values so the end-users can define their

own sources and destinations.

OriginalFolderName = Range("C2").Value

SaveAsFolderName = Range("C3").Value

OriginalFileName = Dir(OriginalFolderName & Range("C4").Value)

SaveAsFileName = ""

DelimiterSign = Range("C5").Value

SaveAsFileNumber = FreeFile ' FreeFile defines a file number that is not yet used.

SaveAsExtension = Range("C6").Value

' -------------------------------------------------------

Page 9: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 3.3

Green textgives all the

information on the code

Paste this code after the previous one' -------------------------------------------------------

' Loop through all the files in the specified folder

' -------------------------------------------------------

Do While OriginalFileName <> ""

' Open the first file with the specified extension (cell C4 (.DBF)).

Workbooks.Open Filename:=OriginalFolderName & OriginalFileName

' Immediately save a file (destination) in the given folder (SaveAsFolderName) with

the same name as the original one and the specified extension (SaveAsExtension).

SaveAsFileName = SaveAsFolderName & Left(OriginalFileName, Len(OriginalFileName) - 4) &

SaveAsExtension

' Define the position of the last column and the last row. So the next part of this code can

scroll this entire range.

With ActiveSheet.Cells

LastCol = .Find("*", [A1], , , xlByColumns, xlPrevious).Column

LastRow = .Find("*", [A1], , , xlByRows, xlPrevious).Row

End With

' Open the destination file you just created (SaveAsFileName).

Open SaveAsFileName For Output As #SaveAsFileNumber

Page 10: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 3.4

Green text gives allthe information on

the code

Paste this code after the previous one' Loop through all the rows.

For LoopInRows = 1 To LastRow

' Loop through all the columns.

For LoopInCols = 1 To LastCol

' This IF-statement is optional. It's only useful if you intend to import the files afterwards in MS SQL Server.

' This is a predefined conversion to have a correct date format (in this case yyyy-mm-dd).

' First you test if it is a date, next you check if it's not a time. This because you don't want a time to be

converted in a date format (I noticed this is by default done by MS Excel).

If IsDate(ActiveSheet.Cells(LoopInRows, LoopInCols).Value) = True And InStr(ActiveSheet.Cells(LoopInRows,

LoopInCols).Value, ":") = 0 Then

' If the scrolling reached the last column, a line feed and not the given delimiter is needed to start a new line.

If LoopInCols = LastCol Then

Print #SaveAsFileNumber, Year(ActiveSheet.Cells(LoopInRows, LoopInCols).Value) & "-" &

Month(ActiveSheet.Cells(LoopInRows, LoopInCols).Value) & "-" & Day(ActiveSheet.Cells(LoopInRows,

LoopInCols).Value) & vbNewLine;

' If it's not the last column, the delimiter needs to be inserted.

Else

Print #SaveAsFileNumber, Year(ActiveSheet.Cells(LoopInRows, LoopInCols).Value) & "-" &

Month(ActiveSheet.Cells(LoopInRows, LoopInCols).Value) & "-" & Day(ActiveSheet.Cells(LoopInRows,

LoopInCols).Value) & DelimiterSign;

End If

Else

' Same as previous but in case it's not a date.

If LoopInCols = LastCol Then

Print #SaveAsFileNumber, ActiveSheet.Cells(LoopInRows, LoopInCols).Value & vbNewLine;

Else

Print #SaveAsFileNumber, ActiveSheet.Cells(LoopInRows, LoopInCols).Value & DelimiterSign;

End If

End If

Next LoopInCols

Next LoopInRows

Page 11: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 3.5

Green textgives all the

information on the code

Paste this code after the previous one

' Close the destination file (SaveAsFileName) after the last insert is done

Close #SaveAsFileNumber

' Move on to the next file

OriginalFileName = Dir()

' Close the source file used for the export

ActiveWindow.Close SaveChanges:=False

Loop

End Sub

End of SUB

Page 12: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 4

Run the macro (module)

Ready to Rock ’n Roll

1

2

3

Page 13: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

Step 5

Check the destination folder and look if all files have been converted

If so, you can now work with these files in MS Excel or use them as import files into your database

It’s again a perfect solution offered by my team. So proud of youguys and girls! So what do you want me to add to this…

Nothing?Hey I’m a wise ass, so just for you to know: The call or croak of a

frog is unique to its species. Frogs create this sound by passing air through the larynx in the throat. In most calling frogs, the sound is amplified by one or more vocal sacs, membranes of skin under the throat or on the corner of the mouth, that distend during the amplification of the call.Some frog calls are so loud that they can be heard up to a mile away.

Page 14: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

One last thing before I let you work…

I am so proud about these 2 kids.They saw immediately that

Web-IT needed somebody like me (smart, good looking, brains, ...)

One day the 2 little daughters of the owners were playing with the PC of the old man

They saw a presentation of Web-IT Support and Consulting and they asked what W,E,B,-,I,T was

My parents explained that it was the name of the company and they started to make funny noises

Webbit, webbiiiit, webbbi, webbeee and they said it sounded like the sound of a frog

Result?My name: WebbyMy body: a frog

Why is my name is Webby?

Page 15: Web-IT Support and Consulting - bulk dBase (DBF) exports via Microsoft Excel VBA code and CSV file structure

Web-IT Support and Consulting

It might seem a bit weird that I take over the entire (and last) screen.

But it’s my job to end this case study.I would be glad to give an answer to all your questions

and remarks.Just call or e-mail me.

Oh yeah, ask for Webby. They’ll know where to find me.Thanks for your attention and hope to hear you soon.

Web-IT Support and Consulting bvbaSteenhuffelstraat 8, 1861-Wolvertem

E-mail: [email protected]

Phone: +32 (0)2 309 67 76Mobile: +32 (0)475 59 98 20

GPS: latitude: 50.97395, longitude: 4.30504