我需要一个宏来运行经常变化的报表(基本上每次我们提取它们时,列的顺序可以不同)。但是标题始终是相同的,所以我创建了一个“检测”标题的代码,并允许我的公式应用于右列。
我现在有另一个问题:我需要同样的概念(从标题中找到一列而不是它的坐标)但是应用于IF公式:
我的电子表格中的某个位置是“Degree”列,其他位置是“Graduation Date”列 我希望宏找到列毕业日期并在其旁边添加一个新列,并将其称为“B.S毕业日期”(这部分,我得到了。它有效)。
现在,在“B.S毕业日期”,我想要一个公式说:
“如果(列”学位“,第1行)说”副学位“,则将[(”毕业日期“栏,第1行)] + 2年”
这是一个简单的IF公式,但我需要它包含允许它从标题中找到那些列而不是特定坐标的功能。
我在想可能有办法找到标题,将其转换为坐标,然后将坐标转换为变量,并将该变量包含在公式中。
我猜是这样的:
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)
然后选择列,用IF公式填充变量(x,y) 我需要代码的唯一事情是前面有撇号的两行。另外,我需要为每个IF函数设置几个变量(2组。一个用于测试,另一个用于从中提取值的列:IF((x,y)= 1,(偏移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 = "B.S. 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("B.S. 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
这不会产生任何错误,但也不会填充单元格。如果你看到我做错了,请告诉我 谢谢!
答案 0 :(得分:0)
您是否尝试过Cells()
财产?它允许您通过行号和列号引用单元格,例如Cells(1,1)
(第1行,第1列)与Range("A1")
相同。如果您创建一个整数来存储Degree
列号:
Dim degreeCol As Integer
degreeCol = ws.Rows(1).Find("Degree").Column
你可以做同样的事情来引用“毕业日期”和“B.S.毕业日期”:
Dim gradDateCol As Integer
gradDateCol = ws.Rows(1).Find("Graduation Date").Column
Dim bsGradDateCol As Integer
bsGradDateCol = ws.Rows(1).Find("B.S. Graduation Date").Column
现在,假设这些标题位于工作表的第1行,而数据从第2行开始,则可以使用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
内,找到最后一行的编号并逐行执行:
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