Change a variable value to Proper Case in VBA?

随声附和 提交于 2021-02-11 02:49:41

问题


I have a textbox, traditionally labeled as TextBox1, my TextBox1 accepts only Uppercase input (for names), but i'm using the same text to save the file in a folder, when i save i want the name of the person to be in Title case isntead, but I cant figure out how.

Here is what i got so far (i dont know how to use strings, so pardon if there is some werid mistake):

 Private Sub CommandButton1_Click()
 Dim title As String
 title = TextBox1.Value
 Console.WriteLine (StrConv(title, VbStrConv.ProperCase))
 ' Proper Case /\

On Error GoTo Erro1
ChDir "C:\Modelos"
Workbooks.Open Filename:="C:\Modelos\MODELO - PACKING LIST.xlsx"
ChDir "C:\Users\andre.lins\Documents\Processos\Packs"
Tryagain:    ActiveWorkbook.SaveAs Filename:= _
"C:\Users\andre.lins\Documents\Processos\Packs\PKDB\Packing list - " & TextBox1.Value & ".xlsx", FileFormat:= xlOpenXMLWorkbook, CreateBackup:=False



GoTo Errorjump

Erro1:

escape = MsgBox("Qualquer alteração modificará o modelo, por favor, evite modificar o documento. Deseja salvar uma cópia?", vbYesNoCancel, "Atenção")

end sub

回答1:


This update to your code should do the trick:

Private Sub CommandButton1_Click()

    Dim title As String
    Dim wrkBk As Workbook

    'As this code sits behind the command button on the form, ME is a reference to the form.
    title = StrConv(Me.TextBox1.Value, vbProperCase)

    'Set a reference to the workbook - code or user interaction may change the activeworkbook.
    Set wrkBk = Workbooks.Open("C:\Modelos\MODELO - PACKING LIST.xlsx")
    wrkBk.SaveAs "C:\Users\andre.lins\Documents\Processos\Packs\PKDB\Packing list - " & title & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

End Sub


来源:https://stackoverflow.com/questions/36335774/change-a-variable-value-to-proper-case-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!