Home > OS >  VB Compiler can not recognize xlPasteValues
VB Compiler can not recognize xlPasteValues

Time:11-27

I have this simple VB.NET code to copy range from a worksheet and paste to another worksheet, paste values only :

    Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application

Dim xlWorkbook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Open(in_Path)

'Define the Excel Source Sheet
Dim xlWorkSheetSource As Microsoft.Office.Interop.Excel.WorkSheet
xlWorkSheetSource = CType(xlWorkbook.Sheets(in_WorkSheetSource),Microsoft.Office.Interop.Excel.WorkSheet)
'Activate the Source Worksheet
xlWorkSheetSource.Activate()

Dim xlRange1 As Microsoft.Office.Interop.Excel.Range
xlRange1 = xlWorkSheetSource.Range("A13:O20")
xlRange1.Copy()

'Define the Excel Destination Sheet
Dim xlWorkSheetDestination As Microsoft.Office.Interop.Excel.WorkSheet
xlWorkSheetDestination = CType(xlWorkbook.Sheets(in_WorkSheetDestination),Microsoft.Office.Interop.Excel.WorkSheet)
'Activate the Source Worksheet
xlWorkSheetDestination.Activate()

xlWorkSheetDestination.Range("A1").PasteSpecial(xlPasteValues)

xlWorkbook.Save
xlWorkbook.Close

But I have this error :

No compiled code to run
error BC30451: 'xlPasteValues' is not declared. It may be inaccessible due to its protection level. At line 21

Obviously, xlPasteValues is not a var to declare.

Is there any other way to get it done ?

I am using the namespace :

Microsoft.Office.Interop.Excel

CodePudding user response:

In VB.Net (unlike VBA) you have to qualify enumerated values. So change this line:

xlWorkSheetDestination.Range("A1").PasteSpecial(xlPasteValues)

to this:

xlWorkSheetDestination.Range("A1").PasteSpecial(xlPasteType.xlPasteValues)
  • Related