Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
xlWorkBook = xlApp.Workbooks.Open("\\DMVL\Receptie\excellijsten david\Agen.xls")
'~~> Replace Sheet1 below by the relevant sheet where you want to do a replace
xlWorkSheet = xlWorkBook.Sheets("Lijst WN")
'~~> Display Excel
xlApp.Visible = True
ToFind = LblWnnr.Text
ToReplace = "succes"
ReplaceInValuesAndFormulas()
'~~> Save the file
xlWorkBook.Save()
'~~> Close the File
xlWorkBook.Close()
'~~> Quit the Excel Application
xlApp.Quit()
'~~> Clean Up
releaseObject(xlApp)
releaseObject(xlWorkBook)
End Sub
'~~> Sub to replace in values/formulas
Sub ReplaceInValuesAndFormulas()
'~~> Note: If the text is not found then Excel will display a popup box
'xlWorkSheet.Cells.Replace(ToFind, ToReplace, 2, 1, False, False, False)
xlWorkSheet.Cells.Replace(ToFind, ToReplace, 2, 1, False, False, False)
MsgBox("vervangen", MsgBoxStyle.Information, Text)
End Sub
'~~> Release the objects
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub