Excel VBA:查找包含日期的列,如果找到则返回列值

时间:2015-03-20 05:51:46

标签: excel vba excel-vba

我会保持快速和诚实,我正在开发一些代码,以便找到针对人名的列引用,如果他们有与之相关的财务数据。

下面我有一组表格,一个用于概述原始电子表格,另一个用于我希望此代码可以执行的操作,我已编写代码以查找两个表格上的匹配日期条目,并返回与仅包含值的名称相对应的日期的列号。其余部分将被忽略,但代码不会将任何内容粘贴到D列中,如下所示,我没有错误。针对所有名称的结果条目总是最终为16384,任何人都可以看到我的代码有任何错误吗?

请参阅下面的表格示例和代码。值得注意的是,原始工作表是一个数据透视表。

                    Original Sheet
       A   |    B     |    C     |    D     |
  1________|__________|__________|__________|
  2________|__________|__________|__________|
  3________|__________|__________|__________|
  4________|__________|__________|__________|
  5        |01/01/2015|01/02/2015|01/03/2015|
   ________|__________|__________|__________|     
  6 henry  |   $200   |          |    $300  |
    _______|__________|__________|__________|
  7 luke   |   $100   |   $250   |          |
    _______|__________|__________|__________|
  8 michael|   $300   |          |          |
    _______|__________|__________|__________|
  9 james  |          |   $250   |          |
    _______|__________|__________|__________|


       Sheet with Column values where the original figures existed

       A   |    B     |    C     |    D     |
  1________|__________|__________|__________|
  2________|__________|__________|__________|
  3________|__________|__________|__________|
  4________|__________|__________|__________|     
  5________|__________|__________|__________|
  6 henry  |    2     |          |    4     |
    _______|__________|__________|__________|
  7 luke   |    2     |     3    |          |
    _______|__________|__________|__________|
  8 michael|    2     |          |          |
    _______|__________|__________|__________|
  9 james  |          |     3    |          |
    _______|__________|__________|__________|

代码从此处开始

Sub MapValues
Dim namerow As Long, actualrow As Long, actualcolumnsource As Long, actualcolumntarget As Long
Dim actualcost As Range
Dim actualcostvalue As String
Dim J As Long
Set CapexSourceSheet = RngSource.Worksheets("Capex Pivot")
Set CapexTargetSheet = RngDest.Worksheets("Capex")

Set dumpsheet = RngSource.Worksheets.Add
dumpsheet.Name = "Dump Sheet"
dumpsheet.Range("A1").Value = "Name"
dumpsheet.Range("B1").Value = "Source Row Number"
dumpsheet.Range("C1").Value = "Target Row Number"
dumpsheet.Range("D1").Value = "Actuals"

'Set ranges and arrays for the find function
dumpsheet.Activate
namerow = CapexSourceSheet.Cells(rows.Count, 1).End(xlUp).row
actualcolumnsource = ActiveSheet.Cells(5, Columns.Count).End(xlToRight).row
CapexTargetSheet.Activate
actualcolumntarget = ActiveSheet.Cells(5, Columns.Count).End(xlToRight).Column

lngCnt = 2

' Loop takes the date from the original sheet, and checks the target sheet for the date 
' column, if it finds a match, it should return the column number.  Instead it returns
' nothing


CapexSourceSheet.Activate

For J = 1 To actualcolumnsource
       actualcostvalue = CapexSourceSheet.Cells(5, actualcolumnsource).Value
          CapexTargetSheet.Activate
            Set actualcost = ActiveSheet.Cells(5, actualcolumntarget).Find(What:=actualcostvalue, _
                                                                      LookIn:=xlFormulas, _
                                                                      LookAt:=xlPart, _
                                                                      SearchOrder:=xlByColumns, _
                                                                      SearchDirection:=xlNext, _
                                                                      MatchCase:=False, _
                                                                      SearchFormat:=False)
            If Not actualcost Is Nothing Then
            lngCnt = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
               Do While lngCnt > 0
               If actualcost.Value = CapexTargetSheet.Cells(5, actualcolumntarget).Value Then
                  dumpsheet.Cells(lngCnt, 4) = actualcost.Column
               End If
               lngCnt = lngCnt - 1
               Loop
            End If
            actualcolumnsource = actualcolumnsource - 1
Next J

更新:'来源名称'变量可以在源表中选取日期,但似乎无法在目标表中找到它们。在源表中,日期以文本形式写入,但在目标表中日期不是自由文本,它们是公式(例如,2015年3月1日写为= + DATE(年(AB5),月(AB5) )+1,1))其中AB5中的值是01/02/2015。这会影响.FIND方法的结果吗?

2 个答案:

答案 0 :(得分:0)

使用此代码并检查它是否对您有所帮助:

actualcolumntarget = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column

OR

您可以使用此方法获取确切的列数: 要使用它,请设置rw和cl变量。

rw = 1
cl = 1
Do While ThisWorkbook.Sheets("Sheet1").Cells(rw, cl) <> ""
cl = cl + 1
Loop

答案 1 :(得分:0)

最好使用UsedRange代替.End(xlToRight).Row.End(xlToRight).Column,然后迭代行检查哪些列在使用范围内具有日期值。

我用来检查单元格格式的方法是=Cell("format", A1)。这将产生实际日期的日期格式。正如我在下面的链接中提到的那样,不要将日期作为文本存储在引擎盖下Excel使用双打,所以你要让自己很难检测日期是否是数字(没有Excel帮助)。

参考:How to know the formatting of Excel Cell