Cell Formating with VBA

Brian_Richmond

New Member
Joined
Apr 10, 2002
Messages
14
I receive many reports in with cells in various formats. I have written a small script that I am trying to get a given cell within a manually selected range to have it's value temporarily stored in a variable, clear the contents of the cell, format the cell as text, and write the temporary stored value into the newly formatted cell as text.

I need this for MATCH & VLOOKUP across multiple worksheets which don't work as I hoped when the Cells are formatted differently.

I have tried to use the .NumberFormat = "@" but it won't change Currency, Date, and others to Text. I am including a sample of the code:

Option Explicit
Sub Range_Conversion()
Dim Selection As Object
Dim Target As Variant
Dim Result As String
Dim Y_Axis As Integer

Y_Axis = 0

For Each Selection In ActiveCell.CurrentRegion.Cells
Target = Selection.Value
ActiveCell.Offset(Y_Axis, 2).ClearContents
ActiveCell.Offset(Y_Axis, 2).NumberFormat = "@"
ActiveCell.Offset(Y_Axis, 2) = Target
Y_Axis = Y_Axis + 1
Next

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Here is the fix I figured out on my own.... Helps if I actually use all of the variables I defined Heh! :wink: The Column offset of 2 was just for visual testing purposes, the final script does use column offset of 0.


Option Explicit
Sub Range_Conversion()
Dim Selection As Object
Dim Target As Variant
Dim Result As String
Dim Y_Axis As Integer

Y_Axis = 0

For Each Selection In ActiveCell.CurrentRegion.Cells
Target = Selection.Value
Result = Target
ActiveCell.Offset(Y_Axis, 0).ClearContents
ActiveCell.Offset(Y_Axis, 0).NumberFormat
= "@"
ActiveCell.Offset(Y_Axis, 0) = Result
Y_Axis = Y_Axis + 1
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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