如何使单元格范围在 VBA 中工作?
How do I make the cell Range work in VBA?
我的自动填充功能有问题。我想用宏来填充日期,直到B中什么都没有。问题是那里有一些空白。我可以更改代码以使其填充到 B 中的最后一行吗?我使用下面的代码进行了尝试。但是,它不起作用。
Sub fill()
Sheets("Table1").Select
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Range("C2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""",R[-1]C,RC[-1])"
ActiveCell.Select
Dim last As Long
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).AutoFill Destination:=Range("C2:C" & last)
Selection.End(xlDown).Select
Selection.ClearContents
ActiveCell.Offset(-1, 0).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
Y你不需要这么漫长的过程。您只需几行即可完成。例如
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
这是一个例子。假设您的范围是从 C2
开始,如下所示。
试试这个代码
Option Explicit
Sub Sample()
Dim ws As Worksheet
Set ws = Sheets("Table")
Dim lRow As Long
With ws
lRow = .Range("C" & .Rows.Count).End(xlUp).Row
Dim rng As Range
Set rng = .Range("C3:C" & lRow)
Dim visibleCells As Range
On Error Resume Next
Set visibleCells = rng.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not visibleCells Is Nothing Then
visibleCells.FormulaR1C1 = "=R[-1]C"
End If
End With
End Sub
在行动
值得一提
- 同时避免使用
Select/Selection/Activecell
等。您可能需要阅读 How to avoid using Select in Excel VBA
- 你不需要 VBA 来实现你想要的。您只需点击几下即可达到同样的效果。
非VBA方法
- Select 你的范围。在这种情况下
C3:C13
- 按CTRL + G调出转到对话框
- 单击特殊 按钮
- Select 空白单选按钮 并单击 确定。
- 在公式栏中键入
=C2
并按 CTRL + ENTER 键,您就完成了
在行动
我的自动填充功能有问题。我想用宏来填充日期,直到B中什么都没有。问题是那里有一些空白。我可以更改代码以使其填充到 B 中的最后一行吗?我使用下面的代码进行了尝试。但是,它不起作用。
Sub fill()
Sheets("Table1").Select
Columns("C:C").Select
Selection.Insert Shift:=xlToRight
Range("C2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""",R[-1]C,RC[-1])"
ActiveCell.Select
Dim last As Long
Range("C2").Select
Range(Selection, Selection.End(xlToRight)).AutoFill Destination:=Range("C2:C" & last)
Selection.End(xlDown).Select
Selection.ClearContents
ActiveCell.Offset(-1, 0).Range("A1").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.Copy
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Range("A1").Select
End Sub
Y你不需要这么漫长的过程。您只需几行即可完成。例如
rng.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
这是一个例子。假设您的范围是从 C2
开始,如下所示。
试试这个代码
Option Explicit
Sub Sample()
Dim ws As Worksheet
Set ws = Sheets("Table")
Dim lRow As Long
With ws
lRow = .Range("C" & .Rows.Count).End(xlUp).Row
Dim rng As Range
Set rng = .Range("C3:C" & lRow)
Dim visibleCells As Range
On Error Resume Next
Set visibleCells = rng.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not visibleCells Is Nothing Then
visibleCells.FormulaR1C1 = "=R[-1]C"
End If
End With
End Sub
在行动
值得一提
- 同时避免使用
Select/Selection/Activecell
等。您可能需要阅读 How to avoid using Select in Excel VBA - 你不需要 VBA 来实现你想要的。您只需点击几下即可达到同样的效果。
非VBA方法
- Select 你的范围。在这种情况下
C3:C13
- 按CTRL + G调出转到对话框
- 单击特殊 按钮
- Select 空白单选按钮 并单击 确定。
- 在公式栏中键入
=C2
并按 CTRL + ENTER 键,您就完成了
在行动