我目前正在从一个软件导出一个文档(wsCopy)
,并将数据的特殊值粘贴到另一个正在创建(wsDest)
的excel工作簿中。在(wsDest)
中,我还有两个查询,分别是AI:AJ列(引用新创建的导出数据)、一个"SupportReference“选项卡和一个"RegionLookup”选项卡。我的问题是导出数据中的值是奇怪地格式化的。即使导出的数据格式显示“通用”,列AJ中的查找仍返回#N/A错误。解决这个问题的唯一方法是,如果我单击引用的单元格(列AH),其中包括导出的数据,点击F2,然后输入或创建一个vba,它将使列AI中的所有查找量乘以1。但是,我不想做第一个选项,因为我必须对大约14000行这样做。第二个选项的问题是,如果AI列中的vlookup以任何字母返回,则将整个AI列乘以1将无法工作;此外,它将去掉列AI中的vlookup。下面是我想出的:
Sub CopyOver()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Set wsCopy = Workbooks("Export").Worksheets("Sheet1")
Set wsDest = ThisWorkbook.Worksheets("Data")
'1. Find last used row in the copy range based on data in column A
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
'3. Clear contents of existing data range
wsDest.Range("A9:AH" & lDestLastRow).ClearContents
'4. Copy & Paste Data
wsCopy.Range("A2:AH" & lCopyLastRow).SpecialCells(xlCellTypeVisible).Copy
wsDest.Range("A9").PasteSpecial Paste:=xlPasteValues
'Start copy down formulas
Dim DestLastPopulatedRow As Long
'Find last populated row
DestLastPopulatedRow = wsDest.Range("A" & Rows.Count).End(xlUp).Row
'Select the rows where formula is to be populated
wsDest.Range("AI9: " & "AJ" & DestLastPopulatedRow).FillDown
End Sub
代码(wsDest.Range("AI9: " & "AJ" & DestLastPopulatedRow).FillDown)
的最后一部分就是我开始出现问题的地方,因为vlookup公式是从单元格AI9开始的。vlookup公式如下:
列AI Vlookup:=VLOOKUP(AH9,'SupportReference'!E:E,1,FALSE)
列AJ Vlookup:=VLOOKUP(AI9,'RegionLookup'!M:M,1,FALSE)
尝试使用VBA代码乘列AI:
With wsDest.Range("AI9: " & "AI" & DestLastPopulatedRow)
.Value = Evaluate(.Address & "*1")
End With
如果您需要更多的澄清或进一步的数据,请告诉我。
谢谢。
发布于 2022-03-16 23:18:07
如果您不需要VLOOKUP函数是活动的,我希望是这样的,因为您正在复制大量数据块,您可以使用VBA来设置查找单元格的值,而不是函数。下面是一个子过程,将其传递给目标单元格和其他查找参数,VBA查找值并将其放入目标单元格中:
Sub exact_lookup(destination As Range, lookup_value As Variant, lookup_table As Range, column As Integer)
Dim foundCell As Range
Set foundCell = lookup_table.Columns(1).Find(lookup_value, , , xlWhole)
If foundCell Is Nothing Then
destination.Value = CVErr(xlErrNA)
Else
destination.Value = foundCell.Offset(0, column - 1).Value
End If
End Sub
然后编写一个循环来填充当前方法使用查找公式的每个单元格的值。一个例子是:
exact_lookup wsDest.range("AI9"), wsDest.range("AH9").value, worksheets("RegionLookup").range("E:E"), 1
注意:我不太确定我是否理解你在做什么,但希望这种方法能奏效--这足以让我朝着正确的方向前进。
https://stackoverflow.com/questions/71503372
复制相似问题