根据您想要实现的目标,我有多个可能的代码。在任何情况下,如果没有找到具有数值的单元格(我保持了您通过Application.Sum 制定条件的方式)或无法以任何其他方式确定适当的范围,则不会返回任何内容。
第一个是您自己的代码的修改版本:
Sub Charts_Update1()
'Declarations.
Dim TotFTE As Range, CellIndex As Range
Dim ColIndex As Long, i As Long
'Settings
Set TotFTE = Sheets("FTE Detail").Range("E19:P19")
i = TotFTE.Columns.Count
'Focusing TotFTE.
With TotFTE
'Covering the cells from the most right cell with data to the left of cell(0, 16) to the firt column of TotFTE.
For ColIndex = .Cells(0, 16).End(xlToLeft).Column To TotFTE.Column Step -1
'Focusing the entire column with ColIndex index.
With Columns(ColIndex)
'Checking if the sum of the cell of TotFTE within the column with ColIndex index is 0.
If Application.Sum(Intersect(.Cells, TotFTE)) = 0 Then
'Setting i for the previous column.
i = i - 1
'If i is equal to 0, no result with a sum different from 0 has been found.
If i = 0 Then
'Setting TotFTE to nothing and terminating the macro.
Set TotFTE = Nothing
Exit Sub
End If
'Resizing TotFte.
Set TotFTE = TotFTE.Resize(1, i)
Else
'The first cell with a sum different than 0 most to the right in TotFTE has been found. The macro is terminated.
Exit Sub
End If
End With
Next ColIndex
End With
End Sub
它将返回范围:
- 来自原始 TotFTE 左边缘的单元格(即使为空或带有文本)
- 到原始 TotFTE 右边缘左侧的第一个最右边的非空非文本值
我希望至少有一个代码是您自己的修改版本。因此,我将保持一些关键性(例如使用标头来确定ColIndex)。
第二个是全新的代码:
Sub Charts_Update2()
'Declarations.
Dim TotFTE As Range
Dim RngTarget As Range
'Settings.
Set TotFTE = Sheets("FTE Detail").Range("E19:P19")
Set RngTarget = TotFTE.Cells(1, 1)
'If the sum of RngTarget is zero, TotFTE is set to nothing and the macro is terminated.
If Application.Sum(RngTarget) = 0 Then
Set TotFTE = Nothing
Exit Sub
End If
'RngTarget is resized until its sum doesn't change anymore or it reaches the TotFTE range limit.
Do Until Application.Sum(RngTarget) = Application.Sum(RngTarget.Resize(, RngTarget.Columns.Count + 1)) Or _
RngTarget.Columns.Count + 1 > TotFTE.Columns.Count
Set RngTarget = RngTarget.Resize(, RngTarget.Columns.Count + 1)
Loop
'Setting TotFTE.
Set TotFTE = RngTarget
End Sub
它将返回范围:
- 仅当非空且非文本值时才从原始 TotFTE 的左边缘开始
- 到原始 TotFTE 内第一个连续令人满意(非空,非文本值)数据块右边缘的单元格。
第三个也是全新的代码:
Sub Charts_Update3()
'Declarations.
Dim TotFTE As Range
Dim RngLeft As Range
Dim RngRight As Range
'Settings.
Set TotFTE = Sheets("FTE Detail").Range("E19:P19")
Set RngLeft = TotFTE.Cells(1, 1)
Set RngRight = TotFTE.Cells(1, TotFTE.Columns.Count)
'Checking if RngLeft sum is zero.
If Application.Sum(RngLeft.Value) = 0 Then
'Setting RngLeft as the firt cell with value to the right of RngLeft.
Set RngLeft = RngLeft.End(xlToRight)
'Checking if RngLeft has reached beyond the TotFTE limits or has a sum total of 0.
If RngLeft.Column > TotFTE.Column + TotFTE.Columns.Count - 1 Or Application.Sum(RngLeft.Value) = 0 Then
'Setting TotFTE to nothing end terminating the sub.
Set TotFTE = Nothing
Exit Sub
End If
End If
'Checking if RngLeft sum is zero.
If Application.Sum(RngRight.Value) = 0 Then
'Setting RngRight as the firt cell with value to the left of Rngright.
Set RngRight = RngRight.End(xlToLeft)
End If
'Setting TotFTE.
Set TotFTE = Range(RngRight, RngLeft)
End Sub
它将返回范围:
- 从原始 TotFTE 最左边的单元格开始,非空且非文本值
- 到原始 TotFTE 的最右侧单元格,该单元格非空且非文本值。
它将包括这两个单元格之间的任何空值和/或文本值。