Copy selected range to another worksheet

oteixeira

New member
Joined
Dec 2, 2015
Messages
2
Programming Experience
Beginner
Hello to all,

I?m very new to programming and I?m struggling to solve this problem and could not find the solution. I?m using this code to compare two Excel (2007) Worksheets. Whenever a line on sheet1 is not foud on sheet2 it must be copied to sheet3. Everything goes fine until the selection of the row on sheet1. My problem is that I don?t know how to copy the selected row.
I tried UsedRange but it selects all data...

VB.NET:
Do While Globals.ThisAddIn.Application.ActiveCell.Text <> ""
            Dim xlSR As Excel.Range
            procurar_por = Globals.ThisAddIn.Application.ActiveCell.Offset(0, 1).Text
            wb.Sheets(2).Activate()
            On Error Resume Next
            wb.Sheets(2).Range("B:B").Find(procurar_por).Select()
            If Err.Number <> 0 Then
                'c?digo para copiar para f3
                Clipboard.Clear()
                On Error GoTo 0
                wb.Sheets(1).Activate()
                r = Globals.ThisAddIn.Application.ActiveCell.Row
                ws.Range(r & ":" & r).Select()
                'HERE is my problem, the range is selected, how to copy it?
                cnt = cnt + 1
                'ws.UsedRange.Copy()
                'ws.Copy()
                
                wb.Sheets(3).Activate()
                Globals.ThisAddIn.Application.ActiveCell.PasteSpecial(Excel.XlPasteType.xlPasteAll)
                Globals.ThisAddIn.Application.ActiveCell.Offset(1, 0).Select()
            End If
            wb.Sheets(1).Activate()
            Globals.ThisAddIn.Application.ActiveCell.Offset(1, 0).Select()
        Loop

Thanks for any kind help.
Octavio
 
When you record a macro to copy something you get this vba code:
Selection.Copy
Selection object belongs to the Application (eg Application.Selection.Copy), and that is how you use it for interop automation also.
 
Back
Top