我想知道是否有人知道如何获得dctest /使用VBA excel的百分比? 此示例的dctest只是一些经过测试的边界,而In可以被视为设置为进行直流测试的设备总数。
我提供了一张图片以便更好地理解。显示的两组结果都是在将值转换为%之前和之后。我需要的是找到除test1-test6之外的所有测试的百分比。分母将是In的值,即dctest / In。
我尝试了一些编码,但不太清楚如何获得值的百分比。
Sub macro1()
Dim testrow As Long, testcell As Range, lastrowval As Long
testrow = 1 'initialise
Do
Set testcell = Sheets("Summary").Cells(testrow, 1)
'To look for any test1-test6, if there is do nothing
If testcell = "test1" Or testcell = "test2" Or testcell = "test3" Or testcell = "test4" Or testcell = "test5" Or testcell = "test6" Then
'Do nothing
Exit Do
End If
testrow = testrow + 1
Loop
Do
Set testcell = Sheets("Summary").Cells(testrow, 1)
If testcell = "Resultant" Then
lastrowval = testrow
Exit Do
End If
testrow = testrow + 1
Loop
End Sub
请注意,我知道使用excel可以做的固定配方,但有时某些测试如ACtest不会在那里或者会有额外的测试,例如早上测试,这会影响固定的公式。
还要注意test1 - test6,有时候所有6个测试都会出现,有时只会进行1次测试。
我担心的是,如果有一个额外的测试说添加了test1,来自dctest的行......结果将向下移动1行。
如果有测试说test6被删除了,那么来自dctest ....的行将被向上移动1行。我不太清楚可以做些什么来确保这个问题能够得到解决。
跟进
答案 0 :(得分:1)
我采用了图中所示的相同示例。与上一个问题一样,您可以使用公式或代码。
<强>式强>
在Cell J7中,输入此公式,然后右键单击单元格以格式化为%age。
=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(I7,$A$1:$A$11,0),1,0),1,1,1)),0,1)/$B$1
如果您需要,则只需复制公式
<强>代码强>
Sub Sample()
With Range("J7")
.FormulaR1C1 = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(RC[-1],R1C1:R11C1,0),1,0),1,1,1)),0,1)/R1C2"
.NumberFormat = "0.00%"
End With
End Sub
OR
Sub Sample()
With Range("J7")
.Formula = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(I7,$A$1:$A$11,0),1,0),1,1,1)),0,1)/$B$1"
.NumberFormat = "0.00%"
End With
End Sub
<强>后续强>
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim SearchText As String, Excludetext As String
Dim LastRow As Long, i As Long, j As Long
Dim MyArray() As String
Dim boolContinue As Boolean
'~~> Add/Remove the text here which you want to ignore
Excludetext = "In,Test1,Test2,Test3,Test4,Test5,Test6"
MyArray = Split(Excludetext, ",")
Set ws = Sheets("Sheet1")
LastRow = ws.Range("I" & Rows.Count).End(xlUp).Row
For i = 1 To LastRow
boolContinue = True
For j = 0 To UBound(MyArray)
SearchText = Ucase(Trim(MyArray(j)))
If Ucase(Trim(ws.Range("I" & i).Value)) = SearchText Then
boolContinue = False
Exit For
End If
Next j
If boolContinue = True Then
With Range("J" & i)
.Formula = _
"=OFFSET(INDIRECT(ADDRESS(INDEX(MATCH(I" & i & _
",$A$1:$A$11,0),1,0),1,1,1)),0,1)/$B$1"
.NumberFormat = "0.00%"
End With
End If
Next i
End Sub
HTH
西特
答案 1 :(得分:0)
为什么不把它变成百分比/整数= x / 100