Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve an Excel cell value in VBA as formatted in the WorkSheet?

I need to keep leading zeros on a list of numbers. The numbers are added like this (in a loop, but this is just an example using the (1, 1):

Set cel = Sheet.worksh.Cells(1, 1)
cel.ColumnWidth = 10
cel.Value = e.Name
cel.NumberFormat = "0000"

Where e.Name is the number, something like "0720". This displays on the worksheet just fine, but if I do something like this:

Msgbox Sheet.worksh.Cells(1, 1).Value

I get "720". I need it to be "0720", I know I could check using Len() and add the zeros that way, but I was wondering if there was a more direct approach with the Range object that would do this for me.

like image 254
Logan Serman Avatar asked Aug 03 '11 20:08

Logan Serman


People also ask

How do I pull a value from a cell in Excel VBA?

For the defined variable, put an equal sign and mention the cell address. Once again, put a dot to see the IntelliSense list. From the VBA IntelliSense list, choose “Value” property to get the value from the mentioned cell. Now the variable “CellValue” holds the value from the cell A1.

How do you find a value in Excel VBA?

To find a cell with a numeric value in an Excel Table, set the SearchDirection parameter to either of the following, as applicable: xlNext (SearchDirection:=xlNext): To search for the next match. xlPrevious (SearchDirection:=xlPrevious): To search for the previous match.

How do I find the formatted values in Excel?

Instead of entering the format manually, we can select "Choose Format From Cell" from the Format button menu. Excel will temporarily close the Find dialog and wait for us to select a cell that contains the formatting we're after. When we click a cell, the Find window returns with the format set.

How to read or get data from worksheet cell to VBA?

Read or Get Data from Worksheet Cell to VBA in Excel – Solution(s): It is very simple to read the data from Excel to VBA. We can use Cell or Range Object to refer a Worksheet Cell. Get Data from Worksheet Cell – An Example of using Cell Object. The following example will show you how to read or get the data from Worksheet Cell using Cell Object.

How to get cell value in Excel VBA?

Inserting value to cells and getting value from the cell requires the VBA “VALUE” property to be used. Using the CELLS property, we can select only one cell but using the RANGE object. We can select multiple cells. This has been a guide to Get Cell Value in Excel VBA.

What is the use of the cell format property in VBA?

This property cell format property returns or sets a variant value that represents the horizontal alignment for the specified object. Returned or set constants can be: xlGeneral, xlCenter, xlDistributed, xlJustify, xlLeft, xlRight, xlFill, xlCenterAcrossSelection. For example: Tired of Searching for VBA Code Examples? Try AutoMacro!

How do you use range value in VBA?

Range.Value & Cells.Value. There are two ways to reference cell(s) in VBA: The Range object allows you to reference a cell using the standard “A1” notation. This will set the range A2’s value = 1: The Cells object allows you to reference a cell by it’s row number and column number.


2 Answers

You are confusing a number with its textual representation.

You want the .Text property, not .Value, but then, you might have problems with it.

like image 148
GSerg Avatar answered Oct 17 '22 03:10

GSerg


Use This:

Msgbox Format(Sheet.worksh.Cells(1,1).Value, "0000")
like image 38
Lance Roberts Avatar answered Oct 17 '22 02:10

Lance Roberts