Excel 查找数据时返回文本

Excel 查找数据时返回文本,excel,match,lookup,vlookup,Excel,Match,Lookup,Vlookup,我有一个电子表格,如下所示: Name Task Date Mike Go to the beach 10/1/13 Mike Go Shopping 10/2/13 Mike Go to work 10/3/13 Bill Go Hiking 10/1/13 Bill Go to work 10/3/13 Name 10/1/13

我有一个电子表格,如下所示:

Name Task Date Mike Go to the beach 10/1/13 Mike Go Shopping 10/2/13 Mike Go to work 10/3/13 Bill Go Hiking 10/1/13 Bill Go to work 10/3/13 Name 10/1/13 10/2/13 10/3/13 Mike Go to the beach Go shopping Go to work Bill Go Hiking *Blank* Go to work 名称任务日期 迈克13年1月10日去海滩 迈克去购物2013年10月2日 迈克13年3月10日上班 比尔去远足2013年10月1日 比尔于2013年3月10日上班 我正在尝试为电子表格构建另一个选项卡,该选项卡将查看数据选项卡,并在行和列匹配时返回匹配的文本值

我正在尝试使用公式创建一种透视表

结果应该如下所示:

Name Task Date Mike Go to the beach 10/1/13 Mike Go Shopping 10/2/13 Mike Go to work 10/3/13 Bill Go Hiking 10/1/13 Bill Go to work 10/3/13 Name 10/1/13 10/2/13 10/3/13 Mike Go to the beach Go shopping Go to work Bill Go Hiking *Blank* Go to work 名称10/1/13 10/2/13 10/3/13 迈克去海滩购物去上班 比尔去远足*布兰克*去上班 我试图发布图片,但无法,因为这是我的第一篇文章。
我希望你能理解我的要求。

我不是数据透视表方面的专家,我用了一种愚蠢的方式,但很有效。假设:

1) 源数据始终位于“Sheet1”上,并带有这3个列标题

2) “Sheet2”将用于存储已排序的数据

Sub SO_19105503()
    Const NameCol As Long = 1
    Const TaskCol As Long = 2
    Const DateCol As Long = 3

    Dim oShSrc As Worksheet, oShTgt As Worksheet, R As Long, C As Long
    Dim aNames As Variant, aDates As Variant
    Dim lNames As Long, lDates As Long
    Dim oRng As Range, oArea As Range

    Set oShSrc = ThisWorkbook.Worksheets("Sheet1") ' Source worksheet with original data
    oShSrc.Copy Before:=oShSrc
    Set oShSrc = ThisWorkbook.Worksheets("Sheet1 (2)") ' Copy of Source worksheet
    Set oShTgt = ThisWorkbook.Worksheets("Sheet2") ' Target worksheet to store sorted data
    oShSrc.AutoFilterMode = False
    ' Get unique names (sorted) in column A
    aNames = Array()
    lNames = 0
    R = 1
    oShSrc.UsedRange.Sort Key1:=oShSrc.Cells(R, NameCol), Header:=xlYes
    Do
        R = R + 1
        If Not IsEmpty(oShSrc.Cells(R, NameCol)) And oShSrc.Cells(R, NameCol).Value <> oShSrc.Cells(R - 1, NameCol).Value Then
            ReDim Preserve aNames(lNames)
            aNames(lNames) = oShSrc.Cells(R, NameCol).Value
            lNames = lNames + 1
        End If
    Loop Until IsEmpty(oShSrc.Cells(R, NameCol))
    ' Get unique dates (sorted) in column C
    aDates = Array()
    lDates = 0
    R = 1
    oShSrc.UsedRange.Sort Key1:=oShSrc.Cells(R, DateCol), Header:=xlYes
    Do
        R = R + 1
        If Not IsEmpty(oShSrc.Cells(R, DateCol)) And oShSrc.Cells(R, DateCol).Value <> oShSrc.Cells(R - 1, DateCol).Value Then
            ReDim Preserve aDates(lDates)
            aDates(lDates) = oShSrc.Cells(R, DateCol).Value
            lDates = lDates + 1
        End If
    Loop Until IsEmpty(oShSrc.Cells(R, DateCol))
    ' Prepare and put data to Target sheet
    oShTgt.Range("A1").Value = oShSrc.Range("A1").Value ' Name
    ' Insert Dates (start from column B on Row 1)
    For C = 0 To lDates - 1
        oShTgt.Cells(1, C + 2).Value = aDates(C)
    Next
    ' Insert Names (start from Row 2 on Column A)
    For R = 0 To lNames - 1
        oShTgt.Cells(R + 2, 1).Value = aNames(R)
    Next
    ' Reprocess the source data with Autofilter
    For R = 0 To lNames - 1
        oShSrc.AutoFilterMode = False ' Remove AutoFilter before apply
        ' Apply AutoFilter with Criteria of R'th entry in array aNames
        oShSrc.UsedRange.AutoFilter Field:=1, Criteria1:="=" & aNames(R)
        ' Go through Ranges in each Area
        For Each oArea In oShSrc.Cells.SpecialCells(xlCellTypeVisible).Areas
            For Each oRng In oArea.Rows
                ' Stop checking if row is more than used
                If oRng.Row > oShSrc.UsedRange.Rows.count Then
                    Exit For
                End If
                ' Check only if the row is below the header
                If oRng.Row > 1 Then
                    For C = 0 To lDates - 1
                        ' Find the matching date and put the "Task" value
                        If oShSrc.Cells(oRng.Row, DateCol).Value = aDates(C) Then
                            oShTgt.Cells(R + 2, C + 2).Value = oShSrc.Cells(oRng.Row, TaskCol).Value
                            Exit For
                        End If
                    Next C
                End If
            Next oRng
        Next oArea
    Next R
    Application.DisplayAlerts = False
    oShSrc.Delete ' Delete the temporary data source sheet
    Application.DisplayAlerts = True
    Set oShSrc = Nothing
    Set oShTgt = Nothing
End Sub
Sub-SO_19105503()
常量NameCol的长度=1
Const TaskCol的长度=2
Const DateCol的长度=3
将oShSrc标注为工作表,oShTgt标注为工作表,R标注为长,C标注为长
Dim aNames作为变体,aDates作为变体
暗的名字和长的一样,长的一样
暗角为射程,欧雷亚为射程
将oShSrc=ThisWorkbook.Worksheets(“Sheet1”)源工作表与原始数据一起设置
oShSrc.Copy Before:=oShSrc
设置oShSrc=ThisWorkbook.Worksheets(“Sheet1(2)”)源工作表的副本
将oShTgt=ThisWorkbook.Worksheets(“Sheet2”)目标工作表设置为存储排序数据
oShSrc.AutoFilterMode=False
'获取列A中的唯一名称(已排序)
aNames=Array()
lNames=0
R=1
oShSrc.UsedRange.Sort Key1:=oShSrc.Cells(R,NameCol),标题:=xlYes
做
R=R+1
如果不是IsEmpty(oShSrc.Cells(R,NameCol))和oShSrc.Cells(R,NameCol).Value oShSrc.Cells(R-1,NameCol).Value然后
雷迪姆保护区安纳姆斯(lNames)
aNames(lNames)=oShSrc.Cells(R,NameCol).Value
lNames=lNames+1
如果结束
循环直到IsEmpty(oShSrc.单元格(R,NameCol))
'在C列中获取唯一日期(已排序)
aDates=Array()
lDates=0
R=1
oShSrc.UsedRange.Sort Key1:=oShSrc.Cells(R,DateCol),头:=xlYes
做
R=R+1
如果不是IsEmpty(oShSrc.Cells(R,DateCol))和oShSrc.Cells(R,DateCol).Value oShSrc.Cells(R-1,DateCol).Value然后
雷迪姆保留酸盐(lDates)
aDates(lDates)=oShSrc.单元格(R,DateCol).值
lDates=lDates+1
如果结束
循环直到IsEmpty(oShSrc.单元格(R,DateCol))
'准备并将数据放入目标工作表
oShTgt.Range(“A1”).Value=oShSrc.Range(“A1”).Value”名称
'插入日期(从第1行B列开始)
对于C=0到lDates-1
oShTgt.单元(1,C+2).值=aDates(C)
下一个
'插入名称(从A列第2行开始)
对于R=0到lNames-1
oShTgt.单元格(R+2,1).值=aNames(R)
下一个
'使用自动筛选重新处理源数据
对于R=0到lNames-1
oShSrc.AutoFilterMode=False“在应用前删除自动筛选”
'使用数组aNames中第R个条目的条件应用自动筛选'
oShSrc.UsedRange.AutoFilter字段:=1,准则1:=“=”&aNames(R)
“在每个区域进行范围检查
对于oShSrc.Cells.SpecialCells(xlCellTypeVisible)中的每个oArea。区域
一排排的每一只鸟
'停止检查行是否超出使用范围
如果oRng.Row>oShSrc.UsedRange.Rows.count,则
退出
如果结束
'仅当行位于标题下方时才选中
如果oRng.Row>1,则
对于C=0到lDates-1
'找到匹配的日期并输入“任务”值
如果oShSrc.Cells(oRng.Row,DateCol).Value=aDates(C),则
oShTgt.Cells(R+2,C+2).Value=oShSrc.Cells(oRng.Row,TaskCol).Value
退出
如果结束
下一个C
如果结束
下一个
下一个奥雷亚
下一个R
Application.DisplayAlerts=False
oShSrc.Delete“删除临时数据源表
Application.DisplayAlerts=True
设置oShSrc=Nothing
设置oShTgt=Nothing
端接头
屏幕截图-源数据/结果: