在VBA中将变量值更改为Proper Case?

时间:2016-03-31 14:07:02

标签: string vba variables case title

我有一个文本框,传统上标记为TextBox1,我的TextBox1只接受大写输入(名称),但我使用相同的文本将文件保存在文件夹中,当我保存我想要的人的名字在标题的情况下是不是,但我无法弄清楚如何。

这是我到目前为止所得到的(我不知道如何使用字符串,所以原谅如果有一些错误的错误):

 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 个答案:

答案 0 :(得分:3)

对代码的更新应该可以解决问题:

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