简体   繁体   中英

Excel-VBA User form text entry

I am trying to make a userform for users to entry a text ,which will set as a worksheet name. Because worksheet names cannot be longer than 30 characters, I want to add a warning. But in the macro, it gives me an error.

I am checking it with this scenario;

First I am writing a name longer then 30 chars. It gives me the error which I wrote. ( which is great ) Then drops me to the entry screen again ( which is great ) Then I am writing the correct name ( shorter then 30 chars. ) It gives me Run-time error '1004; Application-defined or object-defined error.

The codes are like this;

Yeni_Sheet_Adi_Olustur.Show

If Yeni_Sheet_Adi_Olustur.Yeni_Sheet = "" Then
    MsgBox "Lütfen Sheet adi seçiniz"
    GoTo son

ElseIf Len(Yeni_Sheet_Adi_Olustur.Yeni_Sheet) > 30 Then
MsgBox "Sheet adi 30 karakterden kisa olmalidir"
Yeni_Sheet_Adi_Olustur.Show

Else
Yeni_Sheet = CStr(Yeni_Sheet_Adi_Olustur.Yeni_Sheet.Value)
End If

Yeni_Sheet_Adi_Olustur.Yeni_Sheet is giving me "Object variable or With block variable not set" error with F8 control. :(

I am open to any suggestions. Thank you so much.

I figured it out, I missed the part with, what happens after entering longer then 30 chars, so I added a "GoTo" code. Now it looks like this and works great;

Son1:
Yeni_Sheet_Adi_Olustur.Show

If Yeni_Sheet_Adi_Olustur.Yeni_Sheet = "" Then
    MsgBox "Lütfen Sheet adi seçiniz"
    GoTo son

ElseIf Len(Yeni_Sheet_Adi_Olustur.Yeni_Sheet) > 30 Then
MsgBox "Sheet adi 30 karakterden kisa olmalidir"
Yeni_Sheet_Adi_Olustur.Hide
GoTo Son1

Else
Yeni_Sheet = CStr(Yeni_Sheet_Adi_Olustur.Yeni_Sheet.Value)
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM