2015-04-08 52 views
0

我試圖從主excel文件中抓取數據,將零件複製到新文件中,並將日期保存爲字符串中的日期文件名,格式dd.mm.yyyy但我不能得到日期轉換爲字符串,以便在文件名中使用它。非常感謝。我在網上找到了一大堆解決方案,但沒有一個適合我。非常感謝。vba將日期dd.mm.yyyy轉換爲字符串以便在文件名中使用

Sub invoicing() 
Dim invoiceno As Variant 'but when grabbed from the file, it's string 
Dim tourdate As Date 
Dim final As String 
Dim tourdate2 As String 

Dim startrow As Variant 
Dim endrow As Variant 
startrow = 98 
endrow = 170 
Dim templatefilename As String 
Dim newfilename As Variant 
Dim oWorkbook As Workbook 
Dim ws1 As Worksheet 
Dim ws2 As Worksheet 


Dim destinationpath As String 
destinationpath = CurDir 
templatefilename = "Template Invoice.xlsx" 


Set oWorkbook = Workbooks.Open(destinationpath & Application.PathSeparator &templatefilename) 
oWorkbook.Activate 

Set ws1 = oWorkbook.ActiveSheet 
Set ws2 = ThisWorkbook.Sheets("2015 client details") 


With ws1 
    .Cells(10, 3).Value = ws2.Cells(startrow, 16).Value  'invoice number 
    .Cells(9, 3).Value = ws2.Cells(startrow, 25).Value  'enquiry date 
    .Cells(11, 3).Value = ws2.Cells(startrow, 9).Value  'first name and last name 
    .Cells(12, 3).Value = ws2.Cells(startrow, 30).Value  'pay date 
    .Cells(12, 4).Value = ws2.Cells(startrow, 18).Value  'pay method 
    .Cells(17, 2).Value = ws2.Cells(startrow, 12).Value  '# Adults, # Children, (Private) 
    .Cells(17, 2).RowHeight = 50 
    .Cells(17, 2).EntireRow.AutoFit 
    .Cells(17, 3).Value = ws2.Cells(startrow, 23).Value  'tour date 
    .Cells(17, 4).Value = ws2.Cells(startrow, 17).Value  'Amount 
    .Cells(17, 5).Value = ws2.Cells(startrow, 17).Value  'Total Amount 

End With 
invoiceno = ws1.Cells(10, 3).Value 

tourdate = ws1.Cells(17, 3).Value 'as date which cannot be used in file name in this form 

tourdate2 = Format(tourdate, "dd.mm.yyyy") 


'tourdate.Value = tourdate.Text 
'tourdate.NumberFormat = "@" 

newfilename = "Invoice" & " " & invoiceno & tourdate2 
final = destinationpath & Application.PathSeparator & newfilename & tourdate 
oWorkbook.SaveAs final, xlOpenXMLWorkbook 
oWorkbook.Close 

End Sub 
+2

我覺得你的問題是,你想保存多點的工作文件''在文件名中。有些操作系統允許這樣做,但Windows已經很難(根據我的經驗)。作爲一個長期習慣,我使用下劃線。所以'newfilename =「Invoice」&「」&invoiceno&「_」&Format(tourdate,「dd_mm_yyyy」)'應該適合你。 – PeterT

回答

1

對不起,沒有足夠的信譽評論...

你行:

final = destinationpath & Application.PathSeparator & newfilename & tourdate 

使用日期變量 'tourdate',而不是字符串變量 'tourdate2'(這是包含在newfilename中),所以也許可以將此行更改爲:

final = destinationpath & Application.PathSeparator & newfilename 

我不認爲多個點導致問題了 - 除非你仍然在Windows 98或類似的東西。

+0

謝謝,我在文件名中嘗試使用tourdate2,沒有工作。 我不認爲點應該是一個問題,因爲我一直在不使用VBA的時候這樣做。我試過 tourdate2 =格式(tourdate,「dd_mm_yyyy」),錯誤仍然存​​在。 代碼實際上卡住了 tourdate2 =格式(tourdate,「dd.mm.yyyy」) 錯誤是:錯誤的參數數量或錯誤的分配或無效的屬性分配 – user3579897

0

我終於得到了它的拳頭讀取日期值。文本,然後將其轉換爲CStr的()

 invoiceno = ws1.Cells(10, 3).Value 
     tourdate = ws1.Cells(17, 3).Text 
     tourdate = CStr(tourdate) 
     newfilename = "Invoice" & " " & invoiceno & " " & tourdate & ".xlsx" 
     newpdfname = "invoice" & " " & invoiceno & " " & tourdate & ".pdf" 
     final = destinationpath & "Invoice Out 2015" & Application.PathSeparator & newfilename 
     finalpdf = destinationpath & "invoice Out 2015" & Application.PathSeparator & newpdfname 
     oWorkbook.SaveAs final, xlOpenXMLWorkbook 
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=finalpdf 
     oWorkbook.Close