范围(“IV1”)。结束(xlToLeft).Column在第一行之后返回1

时间:2016-10-12 14:48:03

标签: excel vba macros

Sub UpdateText()
' The purpose of this Macro is to set up an excel spreadsheet in order for
' it to be properly mapped to be exported into an CRM application.
' Some variables to use.
Dim fName As String
Dim lName As String
Dim LastRow As Long
Dim LastCell As Long
Dim i As Long
Dim x As Long
' Get the last Row in the worksheet.
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
' Start the loop for each row.
For i = 1 To LastRow
    ' Grab how many cells that are in the current row.
    LastCell = Range("IV1").End(xlToLeft).Column ' <---- My Error is here. After first row, it returns 1 everytime.
    ' Start the loop through each Cell.
    For x = 1 To LastCell
        ' Look for the consistant tag for each technician to grab Name.
        If ActiveCell = "Location" Then
            lName = ActiveCell.Offset(0, -2).Value
            fName = ActiveCell.Offset(0, -1).Value
            ' Skip to the next cell.
            ActiveCell.Offset(0, 1).Select
        ' Check for the Service to drop in the name of the Technician.
        ElseIf ActiveCell.Value Like "HC:H*" Then
            ' Step back 1 cell to drop before date of service.
            ActiveCell.Offset(0, -1).Select
            ' Insert 3 cells.
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
            ' drop in data.
            ActiveCell.Value = lName
            ActiveCell.Offset(0, 1).Value = fName
            ActiveCell.Offset(0, 2).Value = "Location"
            ' Go back to the next cell that should be checked.
            ActiveCell.Offset(0, 5).Select
        ' Delete the blank cells that seperate the next technician group.
        ElseIf ActiveCell.Value = "" Then
            Selection.Delete Shift:=xlToLeft
        ' If nothing is to do done, Skip to next cell.
        Else
            ActiveCell.Offset(0, 1).Select
        End If
    Next x
    ' End of row, go to the beginning of next row to start.
    Cells(i + 1, 1).Select
Next i
End Sub

第16行

LastCell = Range("IV1").End(xlToLeft).Column

在第一行之后的每一行返回值1。我不明白为什么。此宏的目的是格式化电子表格,以便每列具有一致的数据,以便可以将其映射并导入到CRM文件中。我需要进入每个单元格,从左到右检查值,然后获取值或插入单元格并放入抓取的值。

1 个答案:

答案 0 :(得分:1)

您没有使用当前行索引

使用它:

LastCell = Cells(i, "IV").End(xlToLeft).Column