Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Format of a Cell to Text using VBA

Tags:

excel

vba

I have a "duration" column in an Excel sheet. Its cell format always changes — I want convert the duration from minutes to seconds, but because of the cell formatting it always gives me different answers.

I was thinking that before doing the conversion I could convert that cell format to text so that it will consider that as text value and not try to auto-format it.

Currently I am copying all data into Notepad and then saving it back to the Excel sheet to remove all of the previous format. Is there a way to automate setting a cell's formatting to text using VBA?

like image 826
Code Hungry Avatar asked Nov 25 '11 06:11

Code Hungry


People also ask

How do I change the format in VBA?

Click on Insert tab > select Module. Step 2: Write the subprocedure for VBA Format Date or choose anything to define the module. Step 3: Choose the range cell first as A1. Step 4: Then use the Number Format function as shown below.


1 Answers

To answer your direct question, it is:

Range("A1").NumberFormat = "@" 

Or

Cells(1,1).NumberFormat = "@" 

However, I suggest changing the format to what you actually want displayed. This allows you to retain the data type in the cell and easily use cell formulas to manipulate the data.

like image 199
Justin Self Avatar answered Oct 05 '22 08:10

Justin Self