Excel 有没有一种方法可以将两个柱中的一个柱作为基础
我正在尝试创建一个新列,该列使用其他两列中的条件来生成输出。现在我得到一个类型不匹配错误。在此之前,我的输出根本没有出来Excel 有没有一种方法可以将两个柱中的一个柱作为基础,excel,vba,Excel,Vba,我正在尝试创建一个新列,该列使用其他两列中的条件来生成输出。现在我得到一个类型不匹配错误。在此之前,我的输出根本没有出来 Sub FormatcolumnF() Dim eqa As Range, eqt As Range, rngResult As Range Dim arr_a As Variant, arr_t As Variant Dim wks As Worksheet, i As Integer Dim lngLastRow As Long
Sub FormatcolumnF()
Dim eqa As Range, eqt As Range, rngResult As Range
Dim arr_a As Variant, arr_t As Variant
Dim wks As Worksheet, i As Integer
Dim lngLastRow As Long
Set wks = ActiveSheet
'or even better by name like "Worksheets(Table1)"
With wks
'Now that the Worksheet is defined, we'll find the last row number
lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'We can now use a Range to grab all the category data
Set eqa = .Range(.Cells(2, 4), .Cells(lngLastRow, 1))
Set eqt = .Range(.Cells(2, 3), .Cells(lngLastRow, 1))
End With
arr_a = eqa
arr_t = eqt
Dim result As String
For i = LBound(arr_a, 1) To UBound(arr_a, 1)
If arr_a(i, 1) >= arr_t(i, 1) - 0.025 _
Or arr_a(i, 1) <= arr_t(i, 1) + 0.025 Then
result = "ON TARGET"
ElseIf arr_a(i, 1) <= arr_t(i, 1) - 0.025 Then
result = "UNDER"
ElseIf arr_a(i, 1) >= arr_t(i, 1) + 0.025 Then
result = "OVER"
End If
Next i
With wks
Set rngResult = .Range(.Cells(2, 6), .Cells(lngLastRow, 1))
.Cells(1, 6) = "OVER/UNDER"
End With
End Sub
子格式ColumnF()
尺寸eqa作为范围,eqt作为范围,rngResult作为范围
作为变型的尺寸阵列,作为变型的阵列
Dim wks作为工作表,i作为整数
暗淡的玻璃和长的一样
设置wks=ActiveSheet
或者更好的名称,如“工作表(表1)”
有工作
'现在工作表已经定义,我们将找到最后一个行号
lngLastRow=.Cells.Find(What:=“*”,LookIn:=xlFormulas_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious).行
我们现在可以使用一个范围来获取所有类别数据
设置eqa=.Range(.Cells(2,4),.Cells(lngLastRow,1))
设置eqt=.Range(.Cells(2,3),.Cells(lngLastRow,1))
以
arr_a=eqa
arr_t=eqt
将结果变暗为字符串
对于i=LBound(arr_a,1)到UBound(arr_a,1)
如果arr_a(i,1)>=arr_t(i,1)-0.025_
或者arr_a(i,1)有很多小事情,比如范围中单元格
引用上的列不匹配
但主要的问题是,在将输出赋给变量后,您没有对其执行任何操作。将该变量设置为数组,然后将该数组指定给该范围
还要测试这些值,以确保首先它们不是错误(最有可能是类型不匹配的原因)或不是数字(第二个最有可能的原因)
子格式ColumnF()
Dim rngResult As范围
作为变型的尺寸阵列,作为变型的阵列
将工作作为工作表,我尽可能长
暗淡的玻璃和长的一样
设置wks=ActiveSheet
或者更好的名称,如“工作表(表1)”
有工作
'现在工作表已经定义,我们将找到最后一个行号
lngLastRow=.Cells.Find(What:=“*”,LookIn:=xlFormulas_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious).行
我们现在可以使用一个范围来获取所有类别数据
'跳过设置范围并直接分配给阵列
arr_a=.Range(.Cells(2,4),.Cells(lngLastRow,4))'1是a列,它应该与4匹配
arr_t=.Range(.Cells(2,3),.Cells(lngLastRow,3))'1是A列,它应该与3匹配
'为输出创建一个数组
Dim result()作为变量
ReDim结果(1到UBound(arr_a,1),1到1)作为变量
对于i=LBound(arr_a,1)到UBound(arr_a,1)
'确保arr_a和arr_t都不是错误和数字
如果不是IsError(arr_a(i,1))也不是IsError(arr_t(i,1)),那么
如果IsNumeric(arr_a(i,1))和IsNumeric(arr_t(i,1)),那么
'在数组中加载输出
“应该是而不是
如果arr_a(i,1)>=arr_t(i,1)-0.025_
arr_a(i,1)有很多小事情,比如范围内单元格
引用上的列不匹配
但主要的问题是,在将输出赋给变量后,您没有对其执行任何操作。将该变量设置为数组,然后将该数组指定给该范围
还要测试这些值,以确保首先它们不是错误(最有可能是类型不匹配的原因)或不是数字(第二个最有可能的原因)
子格式ColumnF()
Dim rngResult As范围
作为变型的尺寸阵列,作为变型的阵列
将工作作为工作表,我尽可能长
暗淡的玻璃和长的一样
设置wks=ActiveSheet
或者更好的名称,如“工作表(表1)”
有工作
'现在工作表已经定义,我们将找到最后一个行号
lngLastRow=.Cells.Find(What:=“*”,LookIn:=xlFormulas_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious).行
我们现在可以使用一个范围来获取所有类别数据
'跳过设置范围并直接分配给阵列
arr_a=.Range(.Cells(2,4),.Cells(lngLastRow,4))'1是a列,它应该与4匹配
arr_t=.Range(.Cells(2,3),.Cells(lngLastRow,3))'1是A列,它应该与3匹配
'为输出创建一个数组
Dim result()作为变量
ReDim结果(1到UBound(arr_a,1),1到1)作为变量
对于i=LBound(arr_a,1)到UBound(arr_a,1)
'确保arr_a和arr_t都不是错误和数字
如果不是IsError(arr_a(i,1))也不是IsError(arr_t(i,1)),那么
如果IsNumeric(arr_a(i,1))和IsNumeric(arr_t(i,1)),那么
'在数组中加载输出
“应该是而不是
如果arr_a(i,1)>=arr_t(i,1)-0.025_
那么arr_a(i,1)在哪一行你得到了错误?在哪一行你得到了错误?所以我运行了代码,我收到了我以前遇到的相同问题。新创建的列上唯一的输出是“on TARGET”,而不应该是@Scott CranerTry编辑时的情况。Or应该是和@tonytwotimeIt起作用的时间!非常感谢@Scott Craner你是一个卓越的天才@请通过点击复选标记来确认标记正确。所以我运行代码,我收到了我以前经历过的同样的问题。新创建的列上唯一的输出是“on TARGET”,而不应该是@Scott CranerTry编辑时的情况。Or应该是和@tonytwotimeIt起作用的时间!非常感谢@Scott Craner你是一个卓越的天才@请通过点击复选标记来正确地标记标记。
Sub FormatcolumnF()
Dim rngResult As Range
Dim arr_a As Variant, arr_t As Variant
Dim wks As Worksheet, i As Long
Dim lngLastRow As Long
Set wks = ActiveSheet
'or even better by name like "Worksheets(Table1)"
With wks
'Now that the Worksheet is defined, we'll find the last row number
lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
'We can now use a Range to grab all the category data
'Skip setting ranges and assign directly to the arrays
arr_a = .Range(.Cells(2, 4), .Cells(lngLastRow, 4)) 'the 1 is column A it should match the 4
arr_t = .Range(.Cells(2, 3), .Cells(lngLastRow, 3)) 'the 1 is column A it should match the 3
'Create an array for the output
Dim result() As Variant
ReDim result(1 To UBound(arr_a, 1), 1 To 1) As Variant
For i = LBound(arr_a, 1) To UBound(arr_a, 1)
'make sure both arr_a and arr_t are not error and numeric
If Not IsError(arr_a(i, 1)) And Not IsError(arr_t(i, 1)) Then
If IsNumeric(arr_a(i, 1)) And IsNumeric(arr_t(i, 1)) Then
'Load the output in the array
' Should be And not Or
If arr_a(i, 1) >= arr_t(i, 1) - 0.025 _
And arr_a(i, 1) <= arr_t(i, 1) + 0.025 Then
result(i, 1) = "ON TARGET"
ElseIf arr_a(i, 1) <= arr_t(i, 1) - 0.025 Then
result(i, 1) = "UNDER"
ElseIf arr_a(i, 1) >= arr_t(i, 1) + 0.025 Then
result(i, 1) = "OVER"
End If
Else
result(i, 1) = "Not Numeric"
End If
Else
result(i, 1) = "Error"
End If
Next i
'load the output array into the cells
.Range(.Cells(2, 6), .Cells(lngLastRow, 6)) = result
.Cells(1, 6) = "OVER/UNDER"
End With
End Sub