2017-04-24 34 views
0

选择多个列运行脚本下面的脚本转换存储为文本到m/d/yyyy格式经由输入框

然而日期的列,这仅与1列的工作原理。

我想让用户能够选择多列,用逗号分隔并产生相同的结果。

任何指针将不胜感激。

Sub DateFixer() 

    'place this script in your personal macro workbook and assign to button 
    'converts column of dates stored as text to m/d/yyyy format 

    'set input dims 
    Dim MySheet As Worksheet 
    Dim MyRange As Range, MyCell As Range 
    Dim MyPrompt As String 
    Dim MyTitle As String 

    'set input box dims 
    MyPrompt = "Please select dates column (1 column only)" 
    MyTitle = "Convert text to dates" 

    'error handling 
    On Error GoTo OuttaHere 

    'capture range 
    Set MyRange = Application.InputBox(MyPrompt, MyTitle, Type:=8) 
    Set MySheet = MyRange.Worksheet 
    MyColumn = MyRange.Column 
    LastRow = MySheet.UsedRange.Rows.Count 

    With MySheet 
     Set MyRange = .Range(.Cells(1, MyColumn), .Cells(LastRow, MyColumn)) 
     MyRange.NumberFormat = "m/d/yyyy" 
     For Each MyCell In MyRange 
      MyCell.Formula = MyCell.Value 
     Next 
    End With 

    OuttaHere: 
    End Sub 

回答

0

我想通了。比我想象的要简单得多。

Sub DateFixer() 

'place this in your personal macro workbook and assign to button 
'converts column of dates stored as text to m/d/yyyy format 

'set input dims 
Dim C As Range 
Dim MyPrompt As String 
Dim MyTitle As String 

'set input box dims 
MyPrompt = "Please select date columns to fix" 
MyTitle = "Convert text to dates" 

'error handling 
On Error GoTo OuttaHere 

'capture and convert range 
Set MyRange = Application.InputBox(MyPrompt, MyTitle, Type:=8) 

MyRange.Select 

For Each C In Selection 
If C.Value <> "" Then 
    C.NumberFormat = "m/d/yyyy" 
    C.Formula = C.Value 
End If 
Next C 

OuttaHere: 
End Sub