表中的交叉引用

时间:2017-07-11 09:48:04

标签: excel vba

我在VBA中开始寻找解决方案来检查表格中的内容。我想创建一个函数,告诉某个列(范围)中的单元格是否仅为空,然后标题列(范围)中的单元格是否等于某些内容。我尝试使用isempty和vlookup的组合,但它没有用。 我希望描述清楚,无论如何我附上了一个简化的表格。先感谢您! enter image description here

2 个答案:

答案 0 :(得分:0)

不确定我100%理解你的问题,但让我从我认为的理解开始,并从下面开始:

Sub isitEmpty()

With Sheets("Sheet1")
    If IsEmpty(.Range("B1:E2")).Value Then
        'do something
    Else
        'do something
    End If
End With

End Sub

如果单元格是/否为空,您想要做什么?

答案 1 :(得分:0)

以下代码适用于以下假设:

    Project Type 开始,{li> Column A列在Cell A4
  1. A,B,C,D类别可能会有所不同,但始终会在Row 3
  2. 中标注 您希望参与Project Type
  3. Y列在标题为A,B,C,D的最后一列之后的列中。因此,根据您的图片Column F

    Sub Demo()
        Dim ws As Worksheet
        Dim lRProject As Long, lRMatch As Long, lastColumn As Long, i As Long
        Dim rngProject As Range, celPro As Range, rngMatch As Range, celMatch As Range
    
        Set ws = ThisWorkbook.Sheets("Sheet5") 'change to your sheet
        With ws
            lastColumn = .Cells(3, Columns.count).End(xlToLeft).Column 'gives last column with A,B,C,D
            lRProject = .Cells(.Rows.count, "A").End(xlUp).Row 'last row in Column A
            lRMatch = .Cells(.Rows.count, lastColumn + 1).End(xlUp).Row 'last row in Column F
    
            Set rngMatch = .Range(.Cells(1, lastColumn + 1), .Cells(lRMatch, lastColumn + 1))
            Set rngProject = .Range("A4:A" & lRProject)
    
            For Each celMatch In rngMatch
                For Each celPro In rngProject
                    For i = 2 To lastColumn
                        If celPro.Value = celMatch Then
                            If .Cells(celPro.Row, i) = "X" Then
                                .Cells(celMatch.Row, i) = "Y"
                            End If
                        End If
                    Next i
                Next celPro
            Next celMatch
        End With
    End Sub
    
  4. 参见图片以供参考。

    enter image description here