简体   繁体   English

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

[英]Excel VBA: Find columns with dates, return column value if found

I'll keep it quick and honest, I am developing some code in order to find column references against peoples names if they have a financial figure associated with them. 我会保持诚实和诚实,我正在开发一些代码,以便找到与人们姓名相关的列引用(如果他们有与之相关的财务数据)。

Below I have a set of tables, one which outlines the original spreadsheet, and the other which is what I am expecting this code to do, i have written the code to find matching date entries on the two sheets, and return the column number of the date against the names which ONLY contain values. 在下面,我有一组表,一个表概述了原始电子表格,另一个表是我期望此代码执行的工作,我编写了代码以在两张纸上找到匹配的日期条目,并返回其中的列号。日期与仅包含值的名称相对。 The rest are ignored, however the code doesn't paste anything into column D as it shows below, and I get no errors. 其余部分将被忽略,但是代码不会将任何内容粘贴到D列中,如下所示,并且我没有收到任何错误。 The resulting entry against all the names always ends up being 16384, can anybody see any errors with my code? 针对所有名称的结果条目总是以16384结尾,有人可以看到我的代码有任何错误吗?

See below for sheet examples and code. 请参见下面的工作表示例和代码。 Its worth noting that the original sheet is a pivot table. 值得注意的是,原始工作表是数据透视表。

                    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    |          |
    _______|__________|__________|__________|

Code begins here 代码从这里开始

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

UPDATE : The 'sourcename' variable can pick up the dates in the source sheet, but it cannot seem to find them in the target sheet. 更新 :'sourcename'变量可以在源工作表中获取日期,但似乎无法在目标工作表中找到它们。 In the source sheet, the dates are written in text, but in the target sheet the dates are not free text, they are formulas (example, 01/03/2015 is written as =+DATE(YEAR(AB5),MONTH(AB5)+1,1)) where the value in AB5 is 01/02/2015. 在源工作表中,日期以文本形式编写,但在目标工作表中,日期不是自由文本,而是公式(例如,2015年1月3日写为= + DATE(YEAR(AB5),MONTH(AB5 )+1,1)),其中AB5中的值为2015年1月2日。 Could this affect the outcome in the .FIND method? 这会影响.FIND方法的结果吗?

Use this code and check if it helps you: 使用此代码,并检查它是否对您有帮助:

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

OR 要么

You use this method to get exact number of columns: TO use this, set rw and cl variables. 您可以使用此方法获取确切的列数:要使用此列,请设置rw和cl变量。

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

Better off using UsedRange instead of .End(xlToRight).Row and .End(xlToRight).Column and then iterating over the rows checking which columns have date values in the used range. 最好使用UsedRange而不是.End(xlToRight).Row.End(xlToRight).Column ,然后遍历行,检查哪些列的日期值在使用范围内。

The method I use to check cell formats is =Cell("format", A1) . 我用来检查单元格格式的方法是=Cell("format", A1) This will yield Date formats for actual dates. 这将产生实际日期的日期格式。 As I mention in the link below dont store dates as text becuse under the bonnet Excel uses Doubles, so you make it hard for yourself to detect if dates are or aren't numbers (without Excels help). 正如我在下面的链接中提到的那样,不要将日期存储为文本,因为Excel在引擎盖下使用Doubles,因此您很难自己检测日期是否为数字(没有Excel帮助)。

REF: How to know the formatting of Excel Cell 参考: 如何知道Excel单元格的格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM