Hard return in code only for cells that have va

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
771
Office Version
  1. 365
Platform
  1. Windows
This code below uses cell values to add comments to a comment box. I am trying to avoid having a hard return entered (& vbCrLf) for any cell in columns 79 to 85 that is blank. See the three lines of code I tried to add to accomplish this, but it is not working (generates an error that it wants a "For").

1. Can this be changed so that there is no hard return if the cell is blank?
2. On another note, i =79 to 85, a contiguous range of columns. Is there a way to look at 79 to 81 and 84 to 85, for example (non-contiguous)?

Thanks! C

Code:
Sub AddCommentBox(rowNumber)  ' will be 10 through 16

Dim c As Range
Dim commentText As String
Dim i As Integer

For i = 79 To 85
 '   If IsEmpty(Cells(rowNumber, i)) Then       THESE THREE LINES ARE WHAT I AM TRYING TO ADD
'Next i                                                            THESE THREE LINES ARE WHAT I AM TRYING TO ADD
  '  Else                                                            THESE THREE LINES ARE WHAT I AM TRYING TO ADD
    If IsNumeric(Cells(rowNumber, i)) Then
        commentText = commentText & Format(Cells(rowNumber, i), "#,###")
    Else
        commentText = commentText & Cells(rowNumber, i)
    End If
    commentText = commentText & vbCrLf        'I don't want a hard return if the cell is blank
    Next i

Set c = Cells(rowNumber, 16)

c.AddComment
c.Comment.Text Text:= commentText
c.Comment.Shape.TextFrame.AutoSize = True

End Sub

Sub JUNK()
Call AddCommentBox(10)
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Use a variable to see if the cell is blank, if blank then add a blank, if not blank the add vbCrLf

VBA Code:
For i = 79 To 85
'-----------------------------'
x=iif((rowNumber, i)="","",vbCrLf)
'------------------------------------'


    If IsNumeric(Cells(rowNumber, i)) Then
        commentText = commentText & Format(Cells(rowNumber, i), "#,###")
    Else
        commentText = commentText & Cells(rowNumber, i)
    End If
    commentText = commentText & x        'I don't want a hard return if the cell is blank
    Next i
 
Upvote 0
Use a variable to see if the cell is blank, if blank then add a blank, if not blank the add vbCrLf

VBA Code:
For i = 79 To 85
'-----------------------------'
x=iif((rowNumber, i)="","",vbCrLf)
'------------------------------------'


    If IsNumeric(Cells(rowNumber, i)) Then
        commentText = commentText & Format(Cells(rowNumber, i), "#,###")
    Else
        commentText = commentText & Cells(rowNumber, i)
    End If
    commentText = commentText & x        'I don't want a hard return if the cell is blank
    Next i
This makes perfect sense, but I am getting a compile error "Expected )" for the x=iif((rowNumber, i)="","",vbCrLf)

Any idea why? Thanks! C
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,142
Members
452,615
Latest member
bogeys2birdies

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