2017-04-24 32 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