VBA Excel:运行循环的IF-then语句
我是VBA新手,但我通过读书来提高自己。目前,我从列“A”中提取列,并将它们用作标识符,以便在另一列中运行IF-ELSEIF语句 基本上,在VBA Excel:运行循环的IF-then语句,vba,excel,Vba,Excel,我是VBA新手,但我通过读书来提高自己。目前,我从列“A”中提取列,并将它们用作标识符,以便在另一列中运行IF-ELSEIF语句 基本上,在范围(A1:A3)中,值“ERIC”将存在于每个单元格中[A1=ERIC,A2=ERIC…],而在范围(B1:B3)中,值“ERIC”将是三个不同的整数值[B1=2,B2=9…]。我需要找到范围“ERIC”中的较大整数,并将范围“ERIC”的最大值放入单元格(C1) 然后在范围(A4:A6)中对与整数范围(B4:B6)[B4=1,B5=4…]相关的值“Sal
范围(A1:A3)
中,值“ERIC”将存在于每个单元格中[A1=ERIC,A2=ERIC…],而在范围(B1:B3)
中,值“ERIC”将是三个不同的整数值[B1=2,B2=9…]。我需要找到范围“ERIC”中的较大整数,并将范围“ERIC”的最大值放入单元格(C1)
然后在范围(A4:A6)
中对与整数范围(B4:B6)[B4=1,B5=4…]相关的值“Sally”重复该过程。最大值将进入单元格(C4),我有大约40个名字
请帮忙。谢谢 这应该按照你的要求做。它假设您在
表1
,您的姓名在列A
,值在列B
Public Sub FindNameAndGreatestValue()
Dim nameColumnRowCount As Integer
Dim nameColumn As Integer
Dim valueColumn As Integer
Dim outputColumn As Integer
Dim currentName As String
nameColumnRowCount = Cells(Rows.Count, 1).End(xlUp).Row
currentName = ""
nameColumn = 1 '1 = A - change this to column that has names
valueColumn = 4 '4 = D - change this to the column that has values
outputColumn = 5 '5 = E - change this to column that should contain output
Dim currentLargestForName As Integer
Dim currentNameStartRow As Integer
currentLargestForName = -999
currentName = Cells(1, nameColumn).Value
currentNameStartRow = 1
Dim currentRow As Integer
For currentRow = nameColumn To nameColumnRowCount + 1
'if last known name is the same as the current row's name
If StrComp(currentName, Cells(currentRow, nameColumn).Value, vbTextCompare) = 0 Then
'if current rows number is larger than the last known largest number
If currentLargestForName < CInt(Cells(currentRow, valueColumn).Value) Then
currentLargestForName = CInt(Cells(currentRow, valueColumn).Value)
End If
Else
'drop into here if the names no longer match, meaning a new name was found.
'output the largest known number from the previous name into the first row of that name
Cells(currentNameStartRow, outputColumn).Value = currentLargestForName
currentNameStartRow = currentRow 'save the row this new name starts at for number output later
currentLargestForName = CInt(Cells(currentRow, valueColumn).Value)
currentName = Cells(currentRow, nameColumn).Value
End If
Next
End Sub
Public Sub-findname和greatestvalue()
Dim nameColumnRowCount为整数
Dim NAME列为整数
Dim valueColumn作为整数
Dim outputColumn作为整数
将currentName设置为字符串
nameColumnRowCount=单元格(Rows.Count,1).End(xlUp).Row
currentName=“”
nameColumn=1'1=A-将此更改为具有名称的列
valueColumn=4'4=D-将其更改为具有值的列
outputColumn=5'5=E-将此更改为应包含输出的列
Dim currentLargestForName为整数
将currentNameStartRow设置为整数
currentLargestForName=-999
currentName=单元格(1,名称列).Value
currentNameStartRow=1
将currentRow设置为整数
对于currentRow=nameColumn到nameColumnRowCount+1
'如果最后一个已知名称与当前行的名称相同
如果StrComp(currentName,Cells(currentRow,nameColumn).Value,vbTextCompare)=0,则
'如果当前行数大于上次已知的最大行数
如果currentLargestForName
之前
之后
到目前为止,您尝试了什么?我认为您应该提交一份答案,以便我们大家都能了解;)嘿,我给了你+1!写一个数组forula来完成你的代码需要花费我很多时间:)谢谢[特别是代码中的注释,有助于遵循你的逻辑],除了一个小问题之外,这段代码工作得非常好。。。除最后一个名称/编号外,所有名称/编号都将进行处理。我不明白为什么它不起作用,不管名字数是多少。哦,我道歉,那是我的错。我对代码进行了更改,在currentRow=nameColumn到nameColumnRowCount的
行中添加了+1
。现在应该可以用了。很乐意帮忙。如果您好奇的话,之所以有效,是因为我们需要在最后使用的行(例如,第14行)后面加一行,以便在名称不再匹配的情况下设置条件语句,然后按照预期输出最大的数字。祝你的项目顺利完成。