简体   繁体   English

如何将文件名设为 = range("b3") & now()

[英]how to get the file name to = range("b3") & now()

So in this string of code it does everything i need it to except naming correctly.所以在这串代码中,除了正确命名之外,它完成了我需要的一切。 It will pop open a box that asks me where to save but not an option for a name furthermore, i would like it to save as..它会弹出一个框,询问我在哪里保存,但不是名称选项,我希望它另存为..

range("b3") & format(now(), ddmmyy)

how can i get that to fit into this and have it formatted correctly?我怎样才能让它适合这个并正确格式化?

Private Sub CommandButton1_Click()

Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf 
& "Press OK to exit this macro.", vbCritical, "Must Specify Destination 
Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + ".pdf"

'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do 
you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", 
vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is 
not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", 
vbCritical, "Unable to Delete File"
        Exit Sub
   End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, 
Quality:=xlQualityStandard

'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
    .Display
    .To = ""
    .CC = ""
    .Subject = xSht.Name + ".pdf"
    .Attachments.Add xFolder
    If DisplayEmail = False Then
        '.Send
    End If
End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If

Unload Me
CLOSE1.Show

End Sub

I'm just not sure how to have it set the way i need it to be set.我只是不确定如何按照我需要的方式设置它。 Like i said everything else works, i just need it to save as my range and date.就像我说的一切正常,我只需要将其保存为我的范围和日期。

Without explanation I'm not sure how all that code is relative but assuming cell B3 contains text only, your example is close -- but is missing quotes.没有解释,我不确定所有这些代码是如何相对的,但假设单元格B3仅包含文本,您的示例很接近 - 但缺少引号。

It should be:它应该是:

Range("B3") & Format(Now(), "ddmmyy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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