使用范围为整个列而不是一个单元格

我目前有什么:

Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range) If IsEmpty(Range("G15").Value) = True Then If Selection.Count = 1 Then If Not Intersect(Target, Range("G15")) Is Nothing Then Range("G15").Value = Date End If End If End If End Sub 

我已经创build了这个,原因如下:我想点击一个单元格,它会自动放入今天的date。 现在,如果单元格已经包含文本(不重要),它不会否决该input。

我创造的作品完美.. 一个细胞。 我希望它适用于整个列(在这种情况下,“G”列)。

我尝试使用范围(“G1:G1000”),但没有奏效。 我已经尝试使用一个教程,解释如何使用多个范围( http://www.yogeshguptaonline.com/2009/05/macros-in-excel-selecting-multiple.html ),但也没有解决。

我在这里错过了什么?

在下面使用,如果你select你希望添加到date的G行,这将起作用

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If IsEmpty(Range("G" & Target.Row).Value) = True Then If Selection.Count = 1 Then If Not Intersect(Target, Range("G" & Target.Row)) Is Nothing Then Range("G" & Target.Row).Value = Date End If End If End If End Sub 
 Range("G15").EntireColumn 

可能是你在找什么。

如果你可能在范围内有一些非空的单元格,但是要填充所有空单元格,你需要:

 Option Explicit Sub FillEmptyWithDates() Dim Cell1 As Range Dim Range1 As Range Set Range1 = ActiveWorksheet.Columns(7) For Each Cell1 in Range1 If IsEmpty(Cell1.Value) = True Then Cell1.Value = Date End If Next Cell1 End Sub 

我稍微改了一下你的代码,因为它看起来很复杂,你似乎正在努力完成:用当前date填充G中的所有空单元格。

每次select新的单元格时,此macros都不会再触发。 你会去macros菜单,select它,然后按下运行button。