Home > Back-end >  Getting an error "Object variable not set" using VB
Getting an error "Object variable not set" using VB

Time:12-18

I'm trying to write a program, which will delete every first letter of the words in MsgBox. Here is my code:

Sub Test 
dim k as String
dim f as String
dim bd as String
k = inputbox("input text")
f = "inputed text:" & chr(9) & k & chr(13)

bd = bd   Split(mid(k,1,1))

for i = 2 to len(k)
if mid(k,i-1,1) = "" then bd = bd   Ucase(mid(k,i,1)) else bd = bd   mid(k,i,1)
next i

f = f & "result of work:" & chr(9) & bd
MsgBox f

End Sub

I'm getting error here: bd = bd Split(mid(k,1,1))

CodePudding user response:

If you want removing of first letter of any wards in a string, please test the next code:

Sub deleteFirstWdLett()
 Dim x As String, i As Long, arr
 x = "My dog has three houses" 'consider it the InputBox string
 arr = Split(x)
 For i = 0 To UBound(arr)
    arr(i) = Mid(arr(i), 2)
 Next
 Debug.Print Join(arr, " ")
End Sub

What to happen if the word consists in only a letter? Should it be removed, resulting an extra empty space (as it happens above)? Should the code skip such cases? I cannot understand the reason of doing that, so I cannot imagine what you really need, from this point of view...

  • Related