Excel VBA - Type conversion?

danne_dash

New Member
Joined
Aug 10, 2011
Messages
2
Hi,

I'm working on an Excel VBA project where I want to copy the content of a range of cells to another range of cells.
The source cells contain figures (formatted as Text in the Excel sheet) and the destination cells shall have the exact same format. The data is for example: "1.1", "1.10", "2.3" etc.

I use the command
Sheets("Sheet1").Cells(destRow, destCol).Value = Sheets("Sheet1").Cells(sourRow, sourCol).Value
to copy the data, but them the destination cells is filled with data according to:
"1.1" >> "1,1"
"1.10" >> "1,1"
"2.3" >> "2,3"

I think that the reason for this is that the VBA function treats the source data as numbers and I have swedish version of Excel 2010. In Sweden the decimal designator is comma.

I have tried to use Cstr() and Format() to type convert the source data to string, but the destination data is still the same. Displaying all different kinds of Sheets("Sheet1").Cells(sourRow, sourCol).Value with or without conversion in a msgbox shows the data correctly.

Anyone having any idea?
/Dash
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Why not use Copy?

The code you posted is just setting the values of the destination range.

What exactly do you want to copy?

This will copy values and formatting.
Code:
 Sheets("Sheet1").Cells(sourRow, sourCol).Copy Sheets("Sheet1").Cells(destRow, destCol)
 
Upvote 0
Thanks a lot. It works with your suggestion.

I wasn't aware of the copy function, I'm quite unexperienced with VBA, so I didn't know it existed.

/Dash
 
Upvote 0
You could also format your destination cells as text before you do your assignment. Something like this:

Code:
Sheets("Sheet1").Cells(destRow, destCol).Format = "@"
Sheets("Sheet1").Cells(destRow, destCol).Value = Sheets("Sheet1").Cells(sourRow, sourCol).Value
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top