hughhuyton
New Member
- Joined
- Dec 11, 2016
- Messages
- 16
I'm trying to do a macro which has a loop in a loop that finds the highest value in each row of a table and then copies the highest value to another part of the sheet. It successfully finds the maximum value in the first row but after that it copies the wrong value (not the highest). This is what my code looks like:
Code:
Dim HighestValue As Integer
Dim VCounter As Integer
Dim HCounter As Integer
Sub maximumvalue()
For HCounter = 0 To 9
Range("A1").Offset(HCounter, 0).Select
HighestValue = 0
For VCounter = 1 To 20 Step 1
Range("A2").Offset(HCounter, VCounter).Select
If Range("A2").Offset(HCounter, VCounter).Value > HighestValue Then
HighestValue = Range("A2").Offset(HCounter, VCounter).Value
End If
If Range("A2").Offset(HCounter, VCounter).Value = HighestValue Then
HighestValue = Range("A2").Offset(HCounter, VCounter).Copy
End If
If Range("A2").Offset(HCounter, VCounter).Value = HighestValue Then
Range("A20").PasteSpecial Paste:=xlPasteValues
End if
Next VCounter
MsgBox "The highest Value is " & HighestValue
Next HCounter
End Sub
Last edited by a moderator: