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:
If I am understanding you correctly, yes.

so if the vendor number is 1234 then after the maco would be x1234,

or

Pre Macro: a1234, post macro XA1234

And what I add could be a combination of numbers, letters, or symbols. Like "AB-" "A1-", ect.
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
To do both, maybe:
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)
  Set thisrng = Union(thisrng, Selection.SpecialCells(xlCellTypeConstants, xlNumbers))
  Select Case whichside
    Case 1
      For Each cell In thisrng
        cell.Value = moretext & cell.Value
      Next cell
    Case 2
      For Each cell In thisrng
        cell.Value = cell.Value & moretext
      Next cell
    Case Else
  End Select
  Exit Sub
  
endit:
  MsgBox "only formulas in range"
End Sub
 
Upvote 0
Kenneth Hobson - That worked!!! Thank you so much for your time and help.

Fluffy - Thank you as well for your time!

You just save me days/hours!!

Mr.P
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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