Excel:如果以上陈述为真,则跳过下一行x行(或转到该特定行)

时间:2015-06-29 16:20:14

标签: excel vba excel-vba

强文下午好,

我花了几个月的时间在一个非常冗长的宏上工作的项目,我接近结束但我一直遇到错误,主要是因为宏必须运行在有大量数据空白的报告上(grrrr) 。

所以我有一个IF条件列表。并且宏在列的每个单元格上运行它们,然后进入下一行。我的问题是这会产生错误(请参阅下面的代码)

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
For row = 2 To endRow
    If ws.Cells(row, gradDateCol).Value = "" Then
        ws.Cells(row, bsGradDateCol).Value = "No Data"
    End If
    If ws.Cells(row, degreeCol).Value = "" And ws.Cells(row, gradDateCol).Value <> "" Then 
        ws.Cells(row, bsGradDateCol).Value = ws.Cells(row, gradDateCol).Value
    End If
    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 = "Non degree program (±14 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 = "" And ws.Cells(row, gradDateCol).Value <> "" Then
        ws.Cells(row, bsGradDateCol).Value = ws.Cells(row, gradDateCol).Value
    End If
Next row  

所以我有三个专栏:学位,毕业日期和B.S.毕业日期。我请宏观填写B.S.毕业日期列,取决于学位栏所说的内容,从毕业日期栏中取和更改值。而且因为我对报告无法控制而且它可以改变我使用列标题而不是坐标。

现在,正如我所说,我的问题来自这样一个事实:对于每一行,宏检查每一行,一些行有数据间隙。因此,例如,一行具有&#34;硕士学位&#34;在Degree列但没有日期。宏将应用第一行并填写B.S. Grad Date单元格为&#34; No Data&#34;但是当它达到&#34; Master's Degree&#34;它停止并给我一个错误,因为显然没有它可以减去两年的日期。

我考虑过它并尝试了不同的语句(例如IF ELSE),但我仍然认为最好的方法是找宏,以便宏观考虑如果上面的IF语句为真,那么忽略其他陈述并转到下一行。起初看起来很容易,但到目前为止我还没有在宏中包含这个解决方案。

有人可以提供一些帮助/意见/建议/批评吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

很抱歉,但我需要这样说:错误的做法!如果多次使用某段代码,最好的编程实践之一就是定义自定义方法。另一个是:处理数据,而不是“Excel-cells”。

我建议将Dictionary object与函数一起使用:

'Requires reference to MS Scripting Runtime.dll
Function GetGraduationDate(ByVal sGradDate As String, ByVal sDescription As String) As Variant  'Date if possible
Dim dic As Dictionary

Set dic = New Dictionary
dic.Add "Bachelor's degree (±16 years)", 0
dic.Add "Doctorate degree over (±19 years)", -3
dic.Add "Master's degree (±18 years)", -2
dic.Add "Non degree program (±14 years)", 2
dic.Add "Associate's degree college diploma (±13 years)", 3
dic.Add "Technical diploma (±12 years)", 4

If Not IsDate(sGradDate) Or sDescription = "" Then
    GetGraduationDate = "No data"
Else
    GetGraduationDate = DateAdd("yyyy", dic(sDescription), CDate(sGradDate))
End If

End Function

用法:

Sub DoWhatever()

    For ...
         ws.Cells(row, bsGradDateCol) = GetGraduationDate(ws.Cells(row, degreeCol), _ 
          wsh.Cells(row, gradDateCol))

    Next    

End Sub

诀窍在哪里?

如果GradDate是一个正确的日期而且Description不是空字符串,则函数会查找字典对象并返回与描述相对应的值。

知道了吗?