我想在列中vlookup值,但仅在其他列长度范围内。 (用其他列的长度表示我的值包含多少个单元格)。 A列具有ID,F列具有产品代码。我要在I栏中写下每个产品代码的名称。 (我正在从其他产品页面进行vlookup)。如果vlookup失败,我将在特定行中写入ERROR而不是#N / A。用户可能会在A列中输入ID,但忘记输入产品代码,因此我想对F列进行查找,直到A列中的行数(输入的ID)为止(这样用户就可以看到由于没有产品代码的现有ID而导致错误) 我应该如何覆盖以下代码? 我知道我应该以某种方式覆盖此行: 对于Sheet1.range(ToLookup,range(columnName&Rows.Count).End(xlUp))中的每个单元格
Sub Validation()
Dim inputTemplate As Worksheet
Set inputTemplate = Worksheets("Input template")
Dim accounts As Worksheet
Set accounts = Worksheets("Accounts")
Dim products As Worksheet
Set products = Worksheets("Products")
Call Clear(inputTemplate)
Call aValidation(inputTemplate, accounts, inputTemplate.range("F2"), accounts.range("A1:B45"), "F", 4, -5)
Call aValidation(inputTemplate, products, inputTemplate.range("E2"), products.range("A1:C33"), "E", 4, -4)
End Sub
Sub aValidation(Sheet1 As Worksheet, Sheet2 As Worksheet, ToLookup As range, LookupTable As range, columnName As String, LookupPos As Integer, CIDPos As Integer)
Dim Acell As range
For Each Acell In Sheet1.range(ToLookup, range(columnName & Rows.Count).End(xlUp))
Acell.Offset(0, LookupPos).HorizontalAlignment = xlLeft
Acell.Offset(0, LookupPos).Formula = Application.VLookup(Acell, LookupTable, 2, False)
Debug.Print (Acell.Offset(0, -4).Value)
If Application.WorksheetFunction.IsNA(Acell.Offset(0, LookupPos).Value) And IsEmpty(Acell.Offset(0, CIDPos)) = False Then
Acell.Offset(0, LookupPos).Interior.Color = RGB(255, 0, 0)
Acell.Offset(0, LookupPos).Value = "ERROR"
Acell.Offset(0, LookupPos).HorizontalAlignment = xlVAlignCenter
End If
Next Acell
End Sub
Sub Clear(Sheet1 As Worksheet)
Sheet1.Columns("H:J").Rows("2:" & Rows.Count).ClearContents
Sheet1.Columns("H:J").Rows("2:" & Rows.Count).ClearFormats
End Sub
答案 0 :(得分:0)
与此同时,我得到了答案:
For Each Acell In Sheet1.range(ToLookup, range(columnName & Sheet1.Columns("A").Cells.SpecialCells(xlCellTypeConstants).Count))