将dateinputstring传递给单元格范围

我试图通过一个特定列中的单元格范围在此input框中input的date。 范围不是特定的,但必须填写当前包含该列中数据的所有单元格。

'Date input box Sub dateInput() Dim dateString As String, TheDate As Date dateString = Application.InputBox("Enter Certificate Date") If IsDate(dateString) Then TheDate = DateValue(dateString) Else MsgBox "That's not good!" End If End Sub 

这就像我已经得到,但我似乎无法将inputdate喂入单元格的范围。

您可以使用下面的示例来replace范围中的所有非空单元格与date:

 Sub dateInput() Dim dateString As String, TheDate As Date Dim rng As Range Dim LastRow As Long Dim LastCol As Long Dim ws As Worksheet Dim wb As Workbook Set wb = ActiveWorkbook '<-- Workbook you are working in Set ws = wb.ActiveSheet '<-- Worksheet you are working in With ws If Application.WorksheetFunction.CountA(.Cells) <> 0 Then '<-- Finding last row used LastRow = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row '<-- Finding last column used LastCol = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column Else LastCol = 1 '<-- Selecting first column if nothing found LastRow = 1 '<-- Selecting first row if nothing found End If End With Set rng = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, LastCol)) '<-- You can set your range here dateString = Application.InputBox("Enter Certificate Date") If IsDate(dateString) Then TheDate = DateValue(dateString) For Each c In rng.Cells If c.Value <> "" Then c.Value = TheDate End If Next Else MsgBox "That's not good!" End If End Sub 

这将允许用户select列:

 Sub dateInput() Dim dateString As String, TheDate As Date, r As Range dateString = Application.InputBox("Enter Certificate Date") If IsDate(dateString) Then TheDate = DateValue(dateString) Else MsgBox "That's not good!" End If Set r = Application.InputBox("OK Now pick a column", Type:=8) For Each rr In Intersect(r, ActiveSheet.UsedRange) If rr.Value <> "" Then rr.Value = TheDate End If Next rr End Sub 

你有一些逻辑错误,所以我固定它,即使用dateString,但没有声明,并检查它没有设置值时(假设你修复它,因为它不再显示在您的代码)。 除此之外,您只需要定义范围并设置值。 下面的例子

 Sub dateInput() Dim r As Range Set r = Cells.Range(Cells(1, 1), Cells(10, 1)) Dim TheString As String, TheDate As Date TheString = Application.InputBox("Enter Certificate Date") If IsDate(TheString) Then TheDate = DateValue(TheString) Else MsgBox "That's not good!" End If r.Value = TheDate End Sub