Hi,
I have a sheet with formulas that sometimes get an #N/A as result.
I wanted to write a macro that finds those values when i activate the sheet, tells me to give the correct number and then copy the correct value to another sheet for future reference.
The macro works as intended, although poorly written, BUT, it only works once.
The loop doesn't work even if there is more #N/A to change.
What have i missed?
I have a sheet with formulas that sometimes get an #N/A as result.
I wanted to write a macro that finds those values when i activate the sheet, tells me to give the correct number and then copy the correct value to another sheet for future reference.
The macro works as intended, although poorly written, BUT, it only works once.
The loop doesn't work even if there is more #N/A to change.
What have i missed?
Code:
Private Sub Worksheet_Activate()
Dim rngToSearch As Range
Dim rngFound As Range
Dim strFirst As String
Dim msgResult As VbMsgBoxResult
Set tgt = ThisWorkbook.Sheets("TPS saknade priser")
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Set rngToSearch = Range("G1:G" & lastRow)
Set rngFound = rngToSearch.Find(What:="#N/A", _
LookAt:=xlWhole, _
LookIn:=xlValues, _
MatchCase:=True)
If rngFound Is Nothing Then
Exit Sub
Else
strFirst = rngFound.Address
Do
msgResult = MsgBox("#NA i cell " & rngFound.Address & vbCrLf & "Rätta värdet?( Ja / Nej )", vbYesNo)
If msgResult = vbYes Then
rngFound.Select
rngFound = InputBox("Ange värde")
rngFound.Offset(, 1) = rngFound
'Kopiera nya värdet
Set copyR = rngFound
Set copyMat = rngFound.Offset(, -4)
copyMat.Copy tgt.Range("A65636").End(xlUp).Offset(1, 0)
copyR.Copy tgt.Range("B65536").End(xlUp).Offset(1, 0)
'Räkna ut nya värdet samt ta bort formeln
ActiveCell.Offset(, -1).FormulaR1C1 = "=RC[-1]*RC[1]"
ActiveCell.Previous.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Exit Sub
ElseIf msgResult = vbNo Then
Exit Sub
Else
Set rngFound = rngToSearch.FindNext(rngFound)
End If
Loop Until rngFound.Address = strFirst
End If
End Sub