search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
Office GroupsView
Excel
Excel Charting
Excel Crashesgpfs
Excel Misc
Excel Newusers
Excel Programming
Excel Setup
Excel Worksheet Functions
Office Communicator
Office Developer Automation
Office Developer Com Add_Ins
Office Developer Visual Basica
Office Misc
Office Setup
Office Templates Misc
Officeupdate
Onenote
Outlook
Outlook Bcm
Outlook Calendaring
Outlook Contacts
Outlook General
Outlook Installation
Outlook Program_Addins
Outlook Program_Forms
Outlook Program_Visual Basica
Outlookexpress General
Project
Project Developer
Project Server
Visio
Visio Developer
Visio General
Word Application Errors
Word Customization Menustoolbars
Word Drawing Graphics
Word Mailmerge Fields
Word Pagelayout
Word Programming
Word Tables
Word Visual Basica Beginners
Word Visual Basica Customization
Word Visual Basica General
Works Win

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Application Development
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft Excel Worksheet Functions Posts  Ask A New Question 

Set Cell format using VB.NET - Hug

Thursday, February 14, 2008 12:07 PM

Hi,

I want to open a exsiting CSV file and set the Data Format of column A (Date
and time) to Date (m/d/yyyy) and column width to certain number. I can open
the file but how to set the format? Thanks very much.

My code:

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
excel = New Microsoft.Office.Interop.Excel.Application
wb = excel.Workbooks.Open(FileName)
excel.Visible = True
wb.Activate()
reply
 

I believe this will do the trick for you - just add these lines of code to - HelpFrom

Thursday, February 14, 2008 12:58 PM

I believe this will do the trick for you - just add these lines of code to
what you already have displayed here.  You'll need to change the sheet name
and column affected based on what's actually in your workbook, along with the
proper width value in the first line.

wb.Worksheets("Sheet1").Columns("B:B").ColumnWidth = 15
wb.Worksheets("Sheet1").Columns("B:B").NumberFormat = "m/d/yyyy;@"
reply

Hi JLanthan,Thank you very much for your help. Your code works fine. - Hug

Thursday, February 14, 2008 2:46 PM

Hi JLanthan,

Thank you very much for your help.  Your code works fine.  I was stuck at
syntex of numeric format.  It looks you are the expert.  Can I ask one more
trick?  The column width can not be saved and went back to default width
after the file reopen.  Any trick?    Thank you again.
reply

Arrrgh... - HelpFrom

Thursday, February 14, 2008 3:17 PM

Arrrgh... serious FatFinger exercise on the keyboard at my end if you see a
partial response!

I'll try again.  The reason it's not saving the width, I think, is because
it's a .CSV file and not a .XLS file.  In reality a .CSV file is an ASCII
text file and it has no format storing ability.  You'd need to SAVE AS and
choose to save it as an Excel Workbook to preserve the formatting.

The easiest way to accomplish this could be by adding this line of code
after the formatting is done:
wb.SaveAs Filename:=wb.FullName & ".xls", FileFormat:=xlNormal

if it balks at xlNormal, try using a value of -4143, as:
wb.SaveAs Filename:=wb.FullName & ".xls", FileFormat:=-4143

I say this is easiest instead of stripping off the .csv and replacing it
with .xls.  The filename you'll end up with would be something like
myFile.csv.xls if it started out as myFile.csv
The .FullName property returns the entire drive/path/filename.xls string, so
the copy of the file would be saved to the same location that the .csv file
is at.

That may be undesirable, and you may want to save it in a location you
designate.  You could deal with that by creating a String variable and
assigning it to the path and name you want.  Something like this:

Dim newLocation As String
newLocation = "X:\MySavedCSVFiles\" & wb.Name & ".xls"
so if your file started out as myFile.csv it would end up saved in
X:\MySavedCSVFiles as a file named myFile.csv.xls
the .Name property just returns the name of the workbook without drive/path
info.

Hope that helps.
reply

In what format did you save the file? - David Biddulph

Thursday, February 14, 2008 3:27 PM

In what format did you save the file? If you saved as CSV, remember that CSV
is just comma separated text, so has no formatting data.  If you want to
save things like column width, save as xls.
--
David Biddulph
reply

Thanks again, Latha. - Hug

Thursday, February 14, 2008 4:09 PM

Thanks again, Latha.  I could see the width was changed by the code and could
not be saved.  I suspected the reason was csv file but was not sure.  Thanks
for your great help.
reply

Hi Daved,I guess that I have to sacrifice the column width stuff because I - Hug

Thursday, February 14, 2008 4:10 PM

Hi Daved,

I guess that I have to sacrifice the column width stuff because I want to
keep it in csv format.

Thanks again.
reply

"David Biddulph" wrote...... - Harlan Grove

Friday, February 15, 2008 3:35 AM

...

Picky: the OP could save the file in SLK file format, which does save
formatting information but is also plain text though structured.
reply

How about adding spaces as filler to make fields even length - Mehdi Anis

Wednesday, October 01, 2008 12:27 PM

If you need 15 char long values in CSV, you can add SPACEs as FILLER to your EXCEL Cell Value, then save it as CSV.



Unfortunately you have to loop through each row to make the changes. High level logic is like :-



For Each MyRow as ExcelRow in MySheet.Rows

MyRow(Col).Value &= "      " 'u put 15 spaces

MyRow(Col).Value = MyRow(Col).Value.substring(0, 15)

Next



The above code will give you LEFT JUSTIFIED 15 char long CSV Field. If you want RIGHT JUSTIFIED Text in CSV, then CODE will be like (for the filler only)



MyRow(Col).Value = MyRow(Col).Value.Reverse()

MyRow(Col).Value &= "      "

MyRow(Col).Value = MyRow(Col).Value.substring(0, 15)

MyRow(Col).Value = MyRow(Col).Value.Reverse()



Looping and string operation will take toll on performance - depending on how many rows you have and computer's power. Thnaks you.
reply

format excel worksheet in vb.net - bolton ss

Thursday, October 09, 2008 8:17 AM

if you need more help on format worksheet through vb.net , just refer this.

http://vb.net-informations.com/excel-2007/vb.net_excel_page_format.htm

bolton
reply
 
 

Previous Microsoft Excel Worksheet Functions conversation.