Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2013, 07:29 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy If not last cell in selection range.

Can anybody correct my excel macro code as below, failed to execute.


Sub add_comma()
Dim r As Range

With Selection

For Each r In Selection
If r.Value not_last_cell Then
r.Value = " '" & r.Value & "'" & ","
Else
r.Value = " '" & r.Value & "'"
Endif
Next

End With
Excel.Columns.AutoFit
Excel.Rows.AutoFit
End Sub
 
Old December 6th, 2013, 08:33 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Can you brief what this macro is intended to do..it would be helpful

Regards
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old December 6th, 2013, 10:14 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Shasur View Post
Hi

Can you brief what this macro is intended to do..it would be helpful

Regards
Shasur
Thanks for reply dear friend, I want to put comma for each cells that contains value in range selected.

For last cell which contains value, i dont want to put a comma.

What should I code, valiue is not_last_cell in macro ?


With Selection

For Each r In Selection
If r.Value not_last_cell Then
r.Value = " '" & r.Value & "'" & ","
Else
r.Value = " '" & r.Value & "'"
Endif
Next

End With



Example output.

'No',
'Comma',
'at',
'Last',
'Cell' ---see no comma for value at last cell---


Thanks buddy..
 
Old December 7th, 2013, 08:57 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You need not check the last cell in each iteration
This will increase the time of execution. Instead you can use the offset function to 'adjust' your range

Set oRng = Selection.Range
Set oRng = oRng.Offset (-1,0)
For each can start from here

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old December 8th, 2013, 07:47 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy

Quote:
Originally Posted by Shasur View Post
Hi

You need not check the last cell in each iteration
This will increase the time of execution. Instead you can use the offset function to 'adjust' your range

Set oRng = Selection.Range
Set oRng = oRng.Offset (-1,0)
For each can start from here

Cheers
Shasur
I am sorry friend, I am new in excel macro, I couldnt get the output as needed when try executed. Can you correct my code below in order to get : -

Example output.

'No',
'Comma',
'at',
'Last',
'Cell' ---see no comma for value at last cell---



Sub add_comma()
Dim oRng As Range
With Selection
Set oRng = Selection.Range
Set oRng = oRng.Offset(-1, 0)
For Each oRng In Selection
If oRng.Value <> "" Then
oRng.Value = " '" & oRng.Value & "'" & ","
Else
oRng.Value = " '" & oRng.Value & "'"
End If
Next
End With
Excel.Columns.AutoFit
Excel.Rows.AutoFit
End Sub
 
Old December 10th, 2013, 09:31 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Wenggo View Post
I am sorry friend, I am new in excel macro, I couldnt get the output as needed when try executed. Can you correct my code below in order to get : -

Example output.

'No',
'Comma',
'at',
'Last',
'Cell' ---see no comma for value at last cell---



Sub add_comma()
Dim oRng As Range
With Selection
Set oRng = Selection.Range
Set oRng = oRng.Offset(-1, 0)
For Each oRng In Selection
If oRng.Value <> "" Then
oRng.Value = " '" & oRng.Value & "'" & ","
Else
oRng.Value = " '" & oRng.Value & "'"
End If
Next
End With
Excel.Columns.AutoFit
Excel.Rows.AutoFit
End Sub

Anybody world ?
 
Old December 11th, 2013, 01:12 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Can you try with this hint

Code:
Sub add_comma()

Dim oRng As Range
Set oRng = ActiveWorkbook.Sheets(3).Range("G1:G4")


For Each oCell In oRng.Cells
    If oCell.Value <> "" Then
        oCell.Value = " '" & oCell.Value & "'" & ","
    Else
        oCell.Value = " '" & oCell.Value & "'"
    End If
Next oCell
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
cell is empty while using For Each cell In Range jase2007 Excel VBA 4 April 5th, 2012 10:20 PM
Search & Replace Text in Selection/Range ArchieDog Word VBA 3 May 5th, 2009 02:21 AM
How to get nodes from Selection/Range kaps77 Javascript How-To 1 June 17th, 2007 07:38 AM
Name a range simply for each third cell wapfu Excel VBA 2 December 6th, 2006 03:04 PM
Range selection keithd Excel VBA 7 April 7th, 2005 11:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.