2010-07-07 24 views
2

這可能很簡單,但我已經Google搜索並找不到答案。我也只是學習VBA(我已經完成了VB.NET等)在VBA Excel中,當我將一個單元格複製到另一個單元格時,如果它是不復制的日期,我該怎麼辦?

如果宏試圖複製的單元格是一個日期,我只是得到一個複製的數字,例如, 40352從23/06/2010

下面的代碼片段,最欣賞的任何幫助,感謝:

Sheet5.Range(Cells(rwStartNumber, currentColumn + 1).Address(False, False)) = 
    Sheet5.Range(Cells(rwStartNumber, currentColumn).Address(False, False)) 

顯然,這是兩個迴路,但是這並不是問題的所在。

謝謝!

回答

2

你可以嘗試以下

With Selection 
    .PasteSpecial xlPasteValuesAndNumberFormats 
End With 

讓我知道它是否適合你,

親切的問候,

1

你看到的數字,因爲這是how Excel stores dates:你需要change the format of your target cell to be Date,它會正常顯示。

+0

我有料到它是類似的東西。有沒有辦法將格式與單元格數據一起復制?我有一個搜索和範圍()。選擇,然後Selection.Copy,Selection.Paste方法帶來了應用程序錯誤或somesuch(我甚至不知道是否會複製格式無論如何) – 2010-07-07 12:13:33

+0

@Thomas King Take使用xlPasteFormats查看PasteSpecial方法http://msdn.microsoft.com/zh-cn/library/aa195818%28office.11​​%29.aspx – 2010-07-07 12:42:21

0

可以從源小區物業NumberFormat的複製,如:

'Following line copies the values...' 
ws.Cells(curRow, curCol + 1) = ws.Cells(curRow, curCol) 

'And this copies the formats...' 
ws.Cells(curRow, curCol + 1).NumberFormat = ws.Cells(curRow, curCol).NumberFormat 

(ws是工作表類型的變量,可以像這樣分配它:

Dim ws as Worksheet 
Set ws = Worksheets("Sheet5") 

,或者你也可以只使用ActiveSheet)

相關問題