简体   繁体   中英

Excel VBA Refuses to Activate sheet

Trying to Copy the Data from one workbook to the next, but whenever I try to select a worksheet I get a message saying Select Method of Range Class Failed

I need to use select because I need to copy paste special something (trying to keep the formatting)

but it wont let me select the worksheet and I have no idea why

Public Sub Worksheet_Export()

'Setting Dimensions for Current Workbook and New workbook 

Dim current_workbook As Workbook
Dim New_workbook As Workbook

Dim current_worksheet As Worksheet
Dim New_worksheet As Worksheet

Set current_workbook = ThisWorkbook
Set New_workbook = Workbooks.Add

Set current_worksheet = current_workbook.Sheets(2)
Set New_worksheet = New_workbook.Sheets(1)

'Copying Data From Current Workbook to CSV File Workbook

current_worksheet.Range("A:C").Select

End Sub

you need to activate the sheet before selecting the range

better solution:

current_worksheet.Range("A:C").Copy Destination:=New_worksheet.Range("A1")

Public Sub Worksheet_Export()

'Setting Dimensions for Current Workbook and New workbook

Dim current_workbook As Workbook
Dim New_workbook As Workbook

Dim current_worksheet As Worksheet
Dim New_worksheet As Worksheet

Set current_workbook = ThisWorkbook
Set New_workbook = Workbooks.Add

Set current_worksheet = current_workbook.Sheets(2)
Set New_worksheet = New_workbook.Sheets(1)




'Copying Data From Current Workbook to CSV File Workbook
current_worksheet.Activate 'you need to activate before selecting
current_worksheet.Range("A:C").Select



'better solution:
current_worksheet.Range("A:C").Copy Destination:=New_worksheet.Range("A1")

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM