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

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。 我不明白为什么。 此macros的目的是格式化电子表格,以便每列具有一致的数据,以便可以将其映射并导入到CRM文件中。 我需要进入每个单元格从左到右检查值,并获取值或插入单元格,并放入值抓住。

您没有使用当前行索引

用这个:

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