问题
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