如何避免使用VBA中的selectvariables单元格范围?
我听说过使用.select在excelmacros中select不喜欢,但是我想知道如何在不使用它的情况下实现我的特定目标? 例如,假设有一个单元(用作标题),其值为“商品”。 在它下面,所有单元都需要VLookupfunction。 但是,在macros的每一次迭代中,列都会移动(添加新列),并添加新行(以便新添加的行也需要添加该function)。 如何才能一致地find这个商品专栏,find最低的空缺行? 使用select会很简单:
Do Until ActiveCell.Value = "Commodity" Activecell.offset(0,1).select loop Do Until ActiveCell.Value = "" ActiveCell.offset(1,0).select loop
显然,我宁愿避免使用这种types的语法,但我不知道如何解决这个问题。 我已经看到关于避免select的所有答案似乎设置,例如,rng =单元格(x,y)或其他东西,但它们总是已知的位置单元格。 我不知道如何做到这一点,而不利用select来检查单元格的值。
首先findSting所在的列,然后计算它旁边的行,设置范围并input公式。
Sub FindColumn() Dim f As Range, c As Integer Dim LstRw As Long, rng As Range Set f = Rows(1).Find(what:="Commodity", lookat:=xlWhole) If Not f Is Nothing Then c = f.Column Else: MsgBox "Not Found" Exit sub End If LstRw = Cells(Rows.Count, c - 1).End(xlUp).Row Set rng = Range(Cells(2, c), Cells(LstRw, c)) rng = "My Formula" End Sub
以下是基于ActiveCell的两个迭代行。
Sub Examples() Dim Target As Range Dim x As Long Set Target = ActiveCell Do Until Target.Value = "Commodity" Set Target = Target.Offset(0, 1) Loop Do Until ActiveCell.Offset(x, 1).Value = "" x = x + 1 Loop End Sub
假设想要的头在那里,你可以使用这个函数:
Function FindLowestUnfilledCell(headerRow As Range, header As String) As Range With headerRow.Find(What:=header, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False) '<--| look for header in passed row Set FindLowestUnfilledCell = headerRow.Parent.Cells(headerRow.Parent.Rows.Count, .Column).End(xlUp) End With End Function
由您的主要子分组使用如下
Sub main() FindLowestUnfilledCell(Rows(1), "Commodity").Formula = "myformula" End Sub
如果没有想要的头文件被处理,相同的函数将得到如下所示的一些时间
Function FindLowestUnfilledCell(headerRow As Range, header As String) As Range Dim r As Range Set r = headerRow.Find(What:=header, lookat:=xlWhole, LookIn:=xlValues, MatchCase:=False) '<--| look for "Commodity" in row 1 If Not r Is Nothing Then Set FindLowestUnfilledCell = headerRow.Parent.Cells(headerRow.Parent.Rows.Count, r.Column).End(xlUp) End Function
因此它的利用会考虑到没有build立想要的标题的可能性:
Sub main() Dim lowestUnfilledRange As Range Set lowestUnfilledRange = FindLowestUnfilledCell(Rows(1), "Commodity") If Not lowestUnfilledRange Is Nothing Then lowestUnfilledRange.Formula = "myformula" End Sub
我想简化一下答案。 例如
Set r = ActiveCell MsgBox r.Address ' $A$1 Columns("A").Insert ' insert column before the first column MsgBox r.Address ' $B$1
所以你可以改变你的代码
Dim cell As Range ' optional Set cell = ActiveCell While cell = "Commodity" Set cell = cell(, 2) ' similar to Set cell = cell.Resize(1,1).Offset(, 1) Wend While cell = "" Set cell = cell(, 2) Wend
尝试使用引用:
Dim lastrow as long, lastColumn as long, i as long, j as long 'Reference for the workbook that has the script running With Thisworkbook.sheets("NameOfTheSheet") 'Count #'s of used rows lastrow = .UsedRange.Rows.Count + .UsedRange.Rows(1).Row - 1 'Count #'s of used columns lastColumn = .UsedRange.Columns(.UsedRange.Columns.Count).Column For i = 1 to lastColumn if .Cells(1,i).Value = "Commodity" then For j = 2 to lastrow if .Cells(j,i).Value = "" then 'Cell found 'Exit function/Sub end if next j end if next i End With