VBA for cell text formatting based on another column's cell values

afrobea_r

Board Regular
Joined
Aug 16, 2015
Messages
76
Dear Excel Community,

Currently I have the following formulas for formatting text in each cell within a certain column.


Selection.Replace What:=Chr(10) & Chr(10), Replacement:=Chr(10), LookAt:=xlPart


Selection.Replace What:=Chr(10), Replacement:=" "

However I wish to combine this process as I have a marker in another column whereby the cells will be indicated by the numbers 1 & 2.

How do I combine the above formulas into one script which can do the following,

If the adjacent cell in the column = 1 then use
Selection.Replace What:=Chr(10) & Chr(10), Replacement:=Chr(10), LookAt:=xlPart

If the adjacent cell in the column = 2 then use
Selection.Replace What:=Chr(10), Replacement:=" "

Any help will be greatlyy appreciated! Thank you.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
Dim rw as Integer 
For rw = range1 to range2 Step 1[COLOR=#008000] 'range1 and range2 are the starting and ending row numbers of the cells you want the formula to be in[/COLOR] 
If Cells(rw, n).Value = 1 then [COLOR=#008000]'n is the column no that contains the value (1 or 2) to be compared[/COLOR] 
Selection.Replace What:=Chr(10) & Chr(10), Replacement:=Chr(10), LookAt:=xlPart 
Elseif the Cells(rw, n).Value = 2 then 
Selection.Replace What:=Chr(10), Replacement:=" " 
Else Msgbox("Please double check the value in " & Cells(rw, n).Address) 
End If
Next
 
Last edited:
Upvote 0
Code:
Dim rw as Integer 
For rw = range1 to range2 Step 1[COLOR=#008000] 'range1 and range2 are the starting and ending row numbers of the cells you want the formula to be in[/COLOR] 
If Cells(rw, n).Value = 1 then [COLOR=#008000]'n is the column no that contains the value (1 or 2) to be compared[/COLOR] 
Selection.Replace What:=Chr(10) & Chr(10), Replacement:=Chr(10), LookAt:=xlPart 
Elseif the Cells(rw, n).Value = 2 then 
Selection.Replace What:=Chr(10), Replacement:=" " 
Else Msgbox("Please double check the value in " & Cells(rw, n).Address) 
End If
Next

Hi Sir,

Thanks for sharing. At the moment I am getting a run type error type mismatch prompt, any suggestions as to how to work around this?
 
Upvote 0
If I understand correctly, you are going to select the cells to be modified based on the column next to those selected cells. If so, here is the "normal" what to write the macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub FixLineFeeds()
  Dim Cell As Range
  For Each Cell In Selection
    If Cell.Offset(, 1) = 1 Then
      Cell.Value = Replace(Cell.Value, vbLf & vbLf, vbLf)
    ElseIf Cell.Offset(, 1) = 2 Then
      Cell.Value = Replace(Cell.Value, vbLf, " ")
    End If
  Next
End Sub[/td]
[/tr]
[/table]
and here is an alternate, "not really normal" way to write the macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub FixLineFeed2()
  Selection = Evaluate(Replace(Replace("IF(@=1,SUBSTITUTE(#,CHAR(10)&CHAR(10),CHAR(10)),IF(@=2,SUBSTITUTE(#,CHAR(10),"" ""),IF(#="""","""",#)))", "#", Selection.Address), "@", Selection.Offset(, 1).Address))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
If I understand correctly, you are going to select the cells to be modified based on the column next to those selected cells. If so, here is the "normal" what to write the macro...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub FixLineFeeds()
  Dim Cell As Range
  For Each Cell In Selection
    If Cell.Offset(, 1) = 1 Then
      Cell.Value = Replace(Cell.Value, vbLf & vbLf, vbLf)
    ElseIf Cell.Offset(, 1) = 2 Then
      Cell.Value = Replace(Cell.Value, vbLf, " ")
    End If
  Next
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
and here is an alternate, "not really normal" way to write the macro...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub FixLineFeed2()
  Selection = Evaluate(Replace(Replace("IF(@=1,SUBSTITUTE(#,CHAR(10)&CHAR(10),CHAR(10)),IF(@=2,SUBSTITUTE(#,CHAR(10),"" ""),IF(#="""","""",#)))", "#", Selection.Address), "@", Selection.Offset(, 1).Address))
End Sub[/TD]
[/TR]
</tbody>[/TABLE]

Dear Rick,

Thank you for your expertise, this worked a charm! Appreciate it!!!
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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