为Excel添加到VBA代码中的循环 [英] Add loop to vba code for excel
本文介绍了为Excel添加到VBA代码中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对vba知之甚少,所以我希望有人能帮助我.
I know little about vba so I am hoping someone can help me.
我下面有以下代码,它用上面的值填充列中的空白单元格"并且工作正常.
I have the following code below, it "fill blank cells in column with value above" and works fine.
我需要在非连续的列上使用它. 有没有一种方法可以向其中添加循环,使其可以在[B D H I]列上运行?
I need to use it on NON-contiguous coloumns. Is there a way to add a loop to it so that it will run on [B D H I] columns?
我试图迷惑这个没有任何地方
I have tryed to puzzel this out have not got anywhere
谢谢
Sub FillColBlanks()
'by Dave Peterson 2004-01-06
'fill blank cells in column with value above
'http://www.contextures.com/xlDataEntry02.html
Dim wks As Worksheet
Dim rng As Range
Dim Lastrow As Long
Dim col As Long
Set wks = ActiveSheet
With wks
'col = ActiveCell.Column
'or
col = .Range("G2").Column
Set rng = .UsedRange 'try to reset the lastcell
Lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row
Set rng = Nothing
On Error Resume Next
Set rng = .Range(.Cells(2, col), .Cells(Lastrow, col)) _
.Cells.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "No blanks found"
Exit Sub
Else
rng.FormulaR1C1 = "=R[-1]C"
End If
'replace formulas with values
With .Cells(1, col).EntireColumn
.Value = .Value
End With
End With
End Sub
推荐答案
您可以尝试以下操作:
Sub FillColBlanks(sColRange as string)
'by Dave Peterson 2004-01-06
'fill blank cells in column with value above
'http://www.contextures.com/xlDataEntry02.html
Dim wks As Worksheet
Dim rng As Range
Dim Lastrow As Long
Dim col As Long
Set wks = ActiveSheet
With wks
'col = ActiveCell.Column
'or
col = .Range(sColRange).Column
Set rng = .UsedRange 'try to reset the lastcell
Lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row
Set rng = Nothing
On Error Resume Next
Set rng = .Range(.Cells(2, col), .Cells(Lastrow, col)) _
.Cells.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "No blanks found"
Exit Sub
Else
rng.FormulaR1C1 = "=R[-1]C"
End If
'replace formulas with values
With .Cells(1, col).EntireColumn
.Value = .Value
End With
End With
End Sub
所以您这样调用该过程:
so you call that procedure like this:
Call FillColBlanks("B1")
Call FillColBlanks("D1")
Call FillColBlanks("H1")
Call FillColBlanks("I1")
这篇关于为Excel添加到VBA代码中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文