2016-10-06 33 views
0

我的要求是我要更改Word文档中表格中选定单元格的日期格式。我尝试了下面的代码。如果我选择一个日期,我可以更改日期格式。但是,我无法更改所选号码的日期格式。表格中的单元格。 ....请帮助通过选择一个编号在字中进行日期格式设置。表格中的单元格

代码:

Sub ConvertDateFormat() 
' 
' Date formating 
' 

Dim oCl As Word.Cell 
Dim oRng As Range 
' 
' Condition to check the selected data 
' 
    If Selection.Type = wdSelectionIP Or _ 
     Not Selection.Information(wdWithInTable) Then 
    MsgBox "Select a cell or range of cells before running" _ 
     & " this macro.", , "Nothing Selected" 
    Exit Sub 
    End If 
    For Each oCl In Selection.Cells 
    Set oRng = oCl.Range 
    ' 
    'Drop of the end of cell mark 
    ' 
    oRng.End = oRng.End - 1 
    With oRng 


    If IsDate(oRng) Then 
     Selection.Text = Format(Selection.Text, "yyyy-MM-dd") 
     Selection.Collapse wdCollapseEnd 
    Else ' not a date - end loop 
     MsgBox ("Invalid Date Format") 


    End If 
     End With 
     Next oCl 
    lbl_Exit: 

Exit Sub 
End Sub 

回答

0

我觉得你的问题是,你试图去改变它包含多个单元格的整个选择的文本。所以我认为你必须用

oRng.Text = Format(oRng.Text, "yyyy-MM-dd") 

替换那条线我不明白你为什么要折叠选择。

+0

非常感谢。有用 –

相关问题