VBA Code not filling in complete range

Mr.Piccolo22

New Member
Joined
Feb 29, 2012
Messages
14
I found the following code and it has worked for years, however today it stopped and I am not sure why.

It will add let's X in front of the number I wanted to add but now it will only add it to a majority of cells and then the rest leave alone. When I do select the cells that didn't change and try to run it again, it gives me and only in range error.

I have Windows 10, and Excel 365 if that helps.

Code:
Sub Add_Text()
Dim cell As Range
Dim moretext As String
Dim thisrng As Range
On Error GoTo endit
whichside = InputBox("Left = 1 or Right =2")
Set thisrng = Range(ActiveCell.Address & "," & Selection.Address) _
.SpecialCells(xlCellTypeConstants, xlTextValues)
moretext = InputBox("Enter your Text")
If whichside = 1 Then
For Each cell In thisrng
cell.Value = moretext & cell.Value
Next
Else
For Each cell In thisrng
cell.Value = cell.Value & moretext
Next
End If
Exit Sub
endit:
MsgBox "only formulas in range"
End Sub

I am not sure what broke and still learning VBA. Thank you for any help.

Mr.P
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Do the cells contain text, as opposed to numbers or formulae?
 
Upvote 0
I can not duplicate a problem. I did change the set range a bit.

I would suggest a Select Case rather than an If/Else. e.g. If 1 is not set in first inputbox and it is a number, the Else executes as it is now.

As Fluff said, If numbers in the cell, nothing happens in them.

Code:
Sub Add_Text()
  Dim cell As Range, thisrng As Range
  Dim moretext As String, whichside As Integer
  
  On Error GoTo endit
  whichside = InputBox("Left = 1 or Right =2")
  moretext = InputBox("Enter your Text")
  
  Set thisrng = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
  If whichside = 1 Then
    For Each cell In thisrng
      cell.Value = moretext & cell.Value
    Next cell
    Else
    For Each cell In thisrng
      cell.Value = cell.Value & moretext
    Next cell
  End If
  Exit Sub
  
endit:
  MsgBox "only formulas in range"
End Sub
 
Last edited:
Upvote 0
Are any of the values purely numbers?
 
Upvote 0
Your code will only look at text values, not numbers.
Do you want to add the prefix/suffix to numbers as well as text?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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