0

If I enter or paste one word or one number, it gives an error: "Runtime '9': Subscript out of range". It is necessary that one character can be entered, but without conversion. If remove values from a column, then the error also occurs: "Runtime '9': subscript out of range". Need to be able to delete values from cells. I need to be able to enter and paste everything, but not convert. Convert only (any number)m and (any number)M. Can delete and edit data in cells.

I have this macro:

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 1 Then Exit Sub
    Dim v As Variant
    v = Split(Target, " ")
    If Right(Target, 1) <> "m" Then
        Target = v(1) & " " & v(0)
    End If
End Sub

This macro converts the data to (any number)m and (any number)M be on the right.

For example:

L3 280M
500m FMA
Nest 475m
340m Pr6
720M uT10
etc.

Convert to:

L3 280M
FMA 500m
Nest 475m
Pr6 340m
uT10 720M
etc.
2
  • 1
    Welcome! Just check the number of elements in array V after doing the SPLIT() - there will be two elements if there is one space in the cell. In other cases, there will be one element ( UBound(V)=0 ) or more. The error occurs precisely when accessing the element V(1) when it does not exist.
    – JohnSUN
    Commented Sep 12, 2022 at 15:43
  • @JohnSUN How can I do this? Could you send me the complete code or a piece of code where this should be added?
    – moninah
    Commented Sep 12, 2022 at 16:16

1 Answer 1

1

Try this:

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 1 Then Exit Sub
    Dim v As Variant
    v = Split(Target, " ")
'===
    If UBound(v) <> 1 Then Exit Sub
'===
    If Right(Target, 1) <> "m" Then
        Target = v(1) & " " & v(0)
    End If
End Sub
1
  • Works great! Thanks a lot!
    – moninah
    Commented Sep 12, 2022 at 16:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .