VBA许多Vlookups过滤单元格

我一直在这个问题上磕头。

我有一个macros,我做了一个文件上的一些操作,但被困在做一系列的过滤和vlookups。

这是我得到的一部分。 我添加了评论,以使其更清晰。

'FILTER ALL 3P VALUES IN ONE COLUMN AND ADD A VALUE IN ALL RESPECTIVE CELLS IN OTHER COLUMN Application.ScreenUpdating = False With ActiveSheet.UsedRange .AutoFilter Field:=22, Criteria1:="*3P*" .Offset(1).Range("AU1:AU" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Select 'HERE I SELECT ALL VISIBLE FILTERED CELLS BY COUNTING IN ROW A BECAUSE THESE CELLS ARENT BLANK .Selection.Value = "3P PROGRAM" .AutoFilter End With 'NOW I WANT TO FILTER ROW FOR BLANKS AND THEN FILL THIS RANGE WITH A FORMULA 'HERE IS THE PROBLEM With ActiveSheet.UsedRange .AutoFilter Field:=47, Criteria1:="=" .Offset(1).Range("AU1:AU" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Select .Selection.FormulaR1C1 = "=VLOOKUP(RC[-38],'[WeeklyData.xlsx]Sheet1'!C8:C16,9,FALSE)" .AutoFilter End With 

问题在于查找步骤。 我想要可见的过滤空白单元格的范围,以获得通过vlookup得到的值。 每个单元格都应该在左侧有一个38列的单元格作为查找参考。

我找不到一种方法来使配方工作。 我想: – 将vlookup插入过滤的范围, – 删除filter(自动filter) – select偏移量为1的计算列作为特殊值,并粘贴为特殊值 – 继续执行此过程5,6次或更多空白或无效其他栏目中的条目。

有没有办法做到这一点? 任何帮助表示赞赏

我比较喜欢Worksheet.UsedRange属性的Range.CurrentRegion 属性 。 它指的是在原点创build的“数据岛”(在这种情况下是A1)。

 With ActiveSheet If .AutoFilterMode Then AutoFilterMode = False With .Cells(1, 1).CurrentRegion 'Set the filter .AutoFilter Field:=22, Criteria1:="*3P*" 'Shift off the header row With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'check if there are any visible cells If CBool(Application.Subtotal(103, .Cells)) Then 'Put 3P PROGRAM into the visible cells in column AU Intersect(.Columns(47), .SpecialCells(xlCellTypeVisible)) = "3P PROGRAM" End If End With 'remove the filter .AutoFilter Field:=22 'set the formula on column AU blank cells Intersect(.Columns(47), .SpecialCells(xlCellTypeBlanks)).FormulaR1C1 = _ "=VLOOKUP(RC[-38], '[WeeklyData.xlsx]Sheet1'!C8:C16, 9, FALSE)" 'revert column AU within the .CurrentRegion to the values returned by the formulas .columns(47).cells = .columns(47).cells.value End With End With 

第二个filter被replace为使用Range.SpecialCells方法与xlCellTypeBlanks属性 。 Intersect方法将单元格区域引用隔离到列AU中的空白单元格。 运行该操作之前,您可能需要检查空白单元格。