1

So I was testing a simple program to find the letter e and remove it from the cell. Except that Variable A doesn't work even though Cells(1,1) does.

Sub remove()
  A = Cells(1, 1)
  pos = InStr(A, "e")
       'A works for pos though I don't know why
  'A.characters(pos,1).delete'
       'The above doesn't work for some reason'   
  Cells(1, 1).Characters(pos, 1).delete    
End Sub

Things that I have tried

  • Dim A as Range
  • Using Range

Also Sorry for not being more specific about my problem. I'm not sure about the terminology behind VBA and coding in general though I'm working on it

1
  • 1
    I suggest you always use Option Explicit Commented Jun 6, 2018 at 7:24

2 Answers 2

1

Jeeped beat me to it. VBA is not strongly typed, so unless you explicitly tell it that the variable you are assigning is an object-type by using 'Set', the value that A takes is, in this case, the contents of the cell instead of the cell itself.

You don't even need to Dim it, although it is good practice.

Sub remove()
  Set A = Cells(1, 1) 'Need to use Set
  pos = InStr(A, "e")

  'This works just fine now
  A.characters(pos,1).delete'
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

I get it I think. So I'm making A=Cells(1,1).value by not typing set. Thanks very much :)
0

You're assigning cells(1, 1).value to A. If you want the range object properties of cells(1, 1) (like 'characters') you need to Set a range-type var to cells(1,1).

Sub remove()
  dim A as range
  set A = Cells(1, 1)
  pos = InStr(A.value, "e")
  A.characters(pos,1).delete'   
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.