find具有特定标题的列,然后将其转换为坐标,将其作为variables并将其包含到函数中

我需要一个macros来运行频繁更改的报表(基本上每次我们拖动它们,列可以按不同的顺序)。 然而,标题总是相同的,所以我做了一个“检测”标题的代码,并允许我的公式适用于正确的列。

我现在有另一个问题:我需要这个相同的概念(从它的标题而不是它的坐标find一个列),但应用到一个IF公式:

在我的电子表格的某个地方将是一个“度”列和其他地方的“gradledate”列。
我想让macros查找列的gradledate,并在它旁边添加一个新的列,并称之为“BSgradledate”(这个部分,我得到了,它工作)。

现在,在“BSgradledate”,我想要一个公式说:
“如果(列”学位“,第一行)说”副学士学位“,然后把[(”gradledate“,第一行)] + 2年”

这是一个简单的IF公式,但我需要它包含的function,允许它从头find这些列,而不是具体的坐标。

我想可能有一种方法来查找标题,将其翻译成坐标,然后将坐标转换为variables,并将该variables包含在公式中。

我猜想是这样的:

Dim ws As Worksheet Dim rngLocation As Range Dim rngNewCol As Range Dim lRow As Long Set ws = ActiveSheet Set rngLocation = ws.Rows(1).Find("Degree") 'Give me the coordinates for that header 'Those coordinates = (x,y) 

然后select列,使用variables(x,y)在其中填充IF公式。
我需要一个代码的唯一的东西是前面有一个撇号的两行。 另外,我需要几个variables为每个IF函数(2个集合,一个从哪个testing将是,另一个是提取值的列:IF((x,y)= 1,(Offset 0, 0)=(w,z)+ 2)。

请让我知道,如果你可以想办法做到这一点,或者如果你需要更多的信息。

编辑:以下stucharo的迹象,这里是我一直在使用的代码:

 Set ws = ActiveSheet Set rngLocation = ws.Rows(1).Find("Graduation Date") If rngLocation Is Nothing Then Exit Sub rngLocation.Offset(, 1).EntireColumn.Insert Set rngNewCol = rngLocation.Offset(, 1) lRow = ws.Cells.Find("*", ws.Range("A1"), SearchDirection:=xlPrevious).row - 1 rngNewCol.Value = "BS Graduation Date" With rngNewCol.Offset(1, 0).Resize(lRow) .NumberFormat = "General" 'the above creates a new column and make it in format general 'next comes stucharo's code (that I tried to adapt) Dim degreeCol As Integer degreeCol = ws.Rows(1).Find("Degree").Column Dim gradDateCol As Integer gradDateCol = ws.Rows(1).Find("Graduation Date (MM/DD/YYYY)").Column Dim bsGradDateCol As Integer bsGradDateCol = ws.Rows(1).Find("BS Graduation Date").Column Dim row As Integer For row = 2 To endRow If ws.Cells(row, degreeCol).Value = "Bachelor's degree (±16 years)" Then ws.Cells(row, bsGradDateCol).Value = ws.Cells(row, gradDateCol).Value End If If ws.Cells(row, degreeCol).Value = "Doctorate degree over (±19 years)" Then ws.Cells(row, bsGradDateCol).Value = DateAdd("yyyy", -3, ws.Cells(row, gradDateCol).Value) End If If ws.Cells(row, degreeCol).Value = "Master's degree (±18 years)" Then ws.Cells(row, bsGradDateCol).Value = DateAdd("yyyy", -2, ws.Cells(row, gradDateCol).Value) End If If ws.Cells(row, degreeCol).Value = "Associate's degree college diploma (±13 years)" Then ws.Cells(row, bsGradDateCol).Value = DateAdd("yyyy", 3, ws.Cells(row, gradDateCol).Value) End If If ws.Cells(row, degreeCol).Value = "Technical diploma (±12 years)" Then ws.Cells(row, bsGradDateCol).Value = DateAdd("yyyy", 4, ws.Cells(row, gradDateCol).Value) End If If ws.Cells(row, degreeCol).Value = " " Then ws.Cells(row, bsGradDateCol).Value = "No Data" End If Next row 

这不会产生任何错误,但它不会填充单元格。 请如果你看到我在做什么错了,让我知道。
谢谢!!

你有没有尝试过Cells()属性? 它允许您通过行号和列号引用单元格,例如Cells(1,1) (第1行,第1列)与Range("A1") 。 如果您创build一个整数来存储Degree列号,这将非常有用:

 Dim degreeCol As Integer degreeCol = ws.Rows(1).Find("Degree").Column 

您可以参考“gradledate”和“学士gradledate”:

 Dim gradDateCol As Integer gradDateCol = ws.Rows(1).Find("Graduation Date").Column Dim bsGradDateCol As Integer bsGradDateCol = ws.Rows(1).Find("BS Graduation Date").Column 

现在,假设这些标题位于工作表的第1行,并且数据从第2行开始,则可以使用Cells()属性引用单元Cells()

 If ws.Cells(2, degreeCol).Value = "Associate Degree" Then ws.Cells(2, bsGradDateCol).Value = ws.Cells(2, gradDateCol).Value + 2 End If 

最重要的是,如果你有一个表,你可以把所有的内容放在一个For Loop ,增加行号并且处理整个表。 而且,如果你在每一行上有多个操作,你可以把它们全部放在For Loop ,find最后一行的编号,然后逐行进行:

 Dim endRow As Integer endRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).row Dim row As Integer For row = 2 To endRow If ws.Cells(row, degreeCol).Value = "Associate Degree" Then ws.Cells(row, bsGradDateCol).Value = DateAdd("yyyy", 2, ws.Cells(row, gradDateCol).Value) End If 'Add further IF statements inside this loop to carry out 'additional operations on a row wise basis. 'You may need to create more column references using the '.Find().Column() method used above. Next row End If