Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How do I set up macro to truncate column/field?

I have been asked to write a macro in visual basic to truncate a specific
column in excel not to exceed 25 characters.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How do I set up macro to truncate column/field?

Hi,

Try:

'=============
Public Sub Tester()
Dim rng As Range
Dim rCell As Range
Const col As String = "A" '<<==== CHANGE

Set rng = Intersect(ActiveSheet.UsedRange, _
Columns(col))

For Each rCell In rng.Cells
With rCell
If Len(.Value) 25 Then
.Value = Left(.Value, 25)
End If
End With
Next rCell

End Sub
'<<=============

---
Regards,
Norman


"Macro to truncate column" <Macro to truncate
wrote in message
...
I have been asked to write a macro in visual basic to truncate a specific
column in excel not to exceed 25 characters.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I set up macro to truncate column/field?

Thanks so much.

So I go to tools, macro, VB Editor and just put this code in the right pane
and than change (where you show "change") the title of the column, or the
letter (i.e., "D")?

Thanks for your help.

Linda

"Norman Jones" wrote:

Hi,

Try:

'=============
Public Sub Tester()
Dim rng As Range
Dim rCell As Range
Const col As String = "A" '<<==== CHANGE

Set rng = Intersect(ActiveSheet.UsedRange, _
Columns(col))

For Each rCell In rng.Cells
With rCell
If Len(.Value) 25 Then
.Value = Left(.Value, 25)
End If
End With
Next rCell

End Sub
'<<=============

---
Regards,
Norman


"Macro to truncate column" <Macro to truncate
wrote in message
...
I have been asked to write a macro in visual basic to truncate a specific
column in excel not to exceed 25 characters.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How do I set up macro to truncate column/field?

Hi Linda,

Alt-F11 to open the VBE
Insert | Module (if no module already exists)
Paste the code
With the cursor anywhere in the pasted code, F5 to run the macro
Alt-F11 to return to Excel

Const col As String = "A" '<<==== CHANGE


Change 'A' to the column of interest - perhaps 'D'

You may wish to visit David McRitchie's 'Getting Started With Macros And
User Defined Functions' at:

http://www.mvps.org/dmcritchie/excel/getstarted.htm


---
Regards,
Norman


"Macro to truncate column"
wrote in message ...
Thanks so much.

So I go to tools, macro, VB Editor and just put this code in the right
pane
and than change (where you show "change") the title of the column, or the
letter (i.e., "D")?

Thanks for your help.

Linda

"Norman Jones" wrote:

Hi,

Try:

'=============
Public Sub Tester()
Dim rng As Range
Dim rCell As Range
Const col As String = "A" '<<==== CHANGE

Set rng = Intersect(ActiveSheet.UsedRange, _
Columns(col))

For Each rCell In rng.Cells
With rCell
If Len(.Value) 25 Then
.Value = Left(.Value, 25)
End If
End With
Next rCell

End Sub
'<<=============

---
Regards,
Norman


"Macro to truncate column" <Macro to truncate
wrote in message
...
I have been asked to write a macro in visual basic to truncate a
specific
column in excel not to exceed 25 characters.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I set up macro to truncate column/field?

Norman,

I get an error message.

1. The Public Sub Tester () shows up in yellow

Error message:

Compile error
Constant expession required (for COL)

Linda

"Macro to truncate column" wrote:

I have been asked to write a macro in visual basic to truncate a specific
column in excel not to exceed 25 characters.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I truncate a text field in Excel? Baxter Excel Worksheet Functions 3 September 27th 07 12:23 AM
How do you truncate 2.22 to just 0.22? Crackles McFarly Excel Worksheet Functions 6 September 5th 07 01:14 AM
can i write a macro to truncate alpha-numeric data? T-Dot Excel Discussion (Misc queries) 2 August 12th 06 08:15 AM
truncate tamar Excel Worksheet Functions 1 July 19th 05 10:32 PM
pivot field size (column field) Kanan Excel Programming 0 April 9th 04 11:41 PM


All times are GMT +1. The time now is 01:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"