Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
My code is here i read all cell text but i don't known method for find and reaplce in excel.
VB
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim oExel As Excel.Application
    Dim oWorkbook As Excel.Workbook
    Dim oWorksheet As Excel.Worksheet
    Dim oRange As Excel.Range
    Dim rCnt As Integer
    Dim cCnt As Integer
    Dim Obj As Object
    Dim Obj1 As Object
    Dim sReplace As String = "ABC"
    oExel = CreateObject("Excel.Application")
    oWorkbook = oExel.Application.Workbooks.Open(TextBox1.Text)
    oExel.Application.Interactive = True
    oExel.Application.UserControl = True
    For Each oWorksheet In oExel.ActiveWorkbook.Worksheets
        oRange = oWorksheet.UsedRange
        For rCnt = 1 To oRange.Rows.Count
            For cCnt = 1 To oRange.Columns.Count
                Obj = CType(oRange.Cells(rCnt, cCnt), Excel.Range).Text
                If Obj <> Nothing Then
                    ' find and replace
                    'MessageBox.Show(Obj)
                End If
            
            Next
        Next
    Next
    oWorkbook.Save()
    oWorkbook.Close()
    oExel.Quit()
    oExel = Nothing
End Sub

please help me for solving this. Thanks in advance
Posted
Updated 1-Mar-16 17:28pm
v2

1 solution

I don't have VB installed but If I want to give you a general clue for implementing an operation in Interop manner first start recording a macro in excel and then do your desired job and then stop recording.

Then go to view > macros and then edit the newly recorded macro. You should call the exact method and parameters as they are arranged in that macro.

For example for replacing we should add a reference to Microsoft.Interop.Excel and then have a code like this :

C#
var oExel = new Excel.Application { DisplayAlerts = false };
oExel.Workbooks.Open(@"C:\abc.xlsx");

oExel.Application.Interactive = true;
oExel.Application.UserControl = true;
oExel.Visible = true;
oExel.Cells.Replace(What: "rain", Replacement: "snow", LookAt: XlLookAt.xlPart,
                    SearchOrder: XlSearchOrder.xlByRows
                    , MatchCase: false, SearchFormat: false, ReplaceFormat: false);


This code is in C# and It was converted from a macro which was recorded and they are almost exactly the same (replacement part I mean).
May be If you copy and paste that macro you do not need any change because you develop in VB language.


Hope It Helps
 
Share this answer
 
Comments
RICKY_Xxx 28-Nov-11 5:43am    
i read all text from all worksheets. and i checked it by messege Box.but when i replace the text it replace text in only one worksheet if excel file contain one sheet.if excel file contain more sheet it will give error that ur replace method contain some wrong parameter or replacement string is on protected sheets.please help me for solving it.thanks in advance!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900