在通过输入框

时间:2017-04-24 22:24:32

标签: excel-vba vba excel

以下脚本将存储为文本的日期列转换为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

1 个答案:

答案 0 :(得分: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
相关问题