VBA宏没有错误,不起作用

时间:2016-10-11 17:04:18

标签: vba macros

我有一个来自第三方的excel文件,我写了一个宏来操作。宏需要获取技术人员名称并在每项服务之前插入 列在每个客户的行中。
excel文件设置如下。

| RowID | ClientID | CLname | CFname | TLname | TFname |位置|日期| Serv | ServDur | ServID | Cost |

每行有1个独特的客户,多个技术人员,每个技术人员可以拥有多项服务。 目前我的宏运行没有错误,但它没有任何变化。

Sub UpdateText()
' UpdateText Macro
' Keyboard Shortcut: Ctrl+Shift+D

Dim fName As String
Dim lName As String
Dim LastRow As Long
Dim LastCell As Long
Dim i As Long
Dim x As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To LastRow
    LastCell = Range("IV1").End(xlToLeft).Column
    For x = 1 To LastCell
        If ActiveCell = "Location" Then
            lName = ActiveCell.Offset(0, -2).Value
            fName = ActiveCell.Offset(0, -1).Value
        ElseIf ActiveCell.Value Like "HC:H*" Then
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            ActiveCell.Value = lName
            ActiveCell.Offset(0, 1).Value = fName
            ActiveCell.Offset(0, 3).Select
        End If
    Next x
Next i

End Sub

1 个答案:

答案 0 :(得分:0)

在您的代码中没有任何结构变化的情况下,您似乎需要更改ActiveCell以及For i循环:

For i = 1 To LastRow
    LastCell = Range("IV1").End(xlToLeft).Column
    For x = 1 To LastCell
        If ActiveCell = "Location" Then
            lName = ActiveCell.Offset(0, -2).Value
            fName = ActiveCell.Offset(0, -1).Value
        ElseIf ActiveCell.Value Like "HC:H*" Then
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            ActiveCell.Value = lName
            ActiveCell.Offset(0, 1).Value = fName
            ActiveCell.Offset(0, 3).Select
        End If
    Next x
    Cells(i, 1).Select  '## Change your "ActiveCell" to the first cell in the next row.
Next i

这可能无法在100%的时间内正常工作,因为它依赖于ActiveCell而且,它没有明确地激活任何东西,所以这意味着它在运行宏之前,必须依赖用户之前选择/激活的内容。这是不可靠的,因为人们忘记选择正确的细胞等等......

此外:依赖ActiveCell导致意大利面条代码变得越来越难以阅读,解释或排除故障(尤其是当您开始跨多个工作表,工作簿等工作时)并且违背了面向对象的目的节目。 Learn to use and manipulate the objects directly, and you'll find that it's very rarely (like, 0.01% of the time) necessary to Select or Activate anything:

Dim cl as Range '# Declaring a Range object to represent a cell on which to operate

For i = 1 To LastRow
    LastCell = Range("IV1").End(xlToLeft).Column
    For x = 1 To LastCell
        Set cl = Cells(i, x)   '## Assign your cell: row:=i, column:=x
        If cl.Value = "Location" Then
            lName = cl.Offset(0, -2).Value
            fName = cl.Offset(0, -1).Value
        ElseIf ActiveCell.Value Like "HC:H*" Then
            cl.Resize(1,2).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            cl.Value = lName
            cl.Offset(0, 1).Value = fName
            Set cl = cl.Offset(0, 3)
        End If
    Next x
    Set cl = Cells(i, 1).Select 
Next i

您可能还想查看此内容,以获得更可靠的方式来获取" last"行或列:

Error in finding last used cell in VBA