Column Select not working in VBA

Ignition1

New Member
Joined
Feb 22, 2011
Messages
49
No idea why VBA is throwing out a run-time error with this bit of code -

Workbooks("Prospects.xls").Activate
ActiveSheet.Range("J2:J2000").Select
Selection.Copy
ThisWorkbook.Activate
Worksheets("BNIREF").Activate
ActiveSheet.Range("C2").Select
ActiveSheet.Paste
Worksheets("BNI All Prospects").Activate
ActiveSheet.Range("C2").Select
ActiveSheet.Paste 'Copy to All Pros
Application.CutCopyMode = False
Worksheets("BNIREF").Activate
Columns("B:C").Select

Basically after I select those Columns I'm just doing a simple Replace, I have the code for that -

Selection.replace What:="1", Replacement:=""

So why doesn't it like Columns("B:C").Select ??
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Glad you figured it out.

Here is more optimized code for you to look at. It will do the same as yours, but the screen will not flicker (a.o.)

Code:
Sub optimized_code()

    With Workbooks("Prospects.xls").Worksheets("NAMEOFTHESHEET").Range("J2:J2000")
        .Copy Destination:=ThisWorkbook.Worksheets("BNIREF").Range("C2")
        .Copy Destination:=ThisWorkbook.Worksheets("BNI All Prospects").Range("C2")
    End With
    
    ThisWorkbook.Worksheets("BNIREF").Columns("B:C").Replace What:="1", Replacement:=""

End Sub

Replace "NAMEOFTHESHEET" with the actual sheet name from which you want to copy.

Wigi
 
Upvote 0
Nice, thanks very much. I'm not that familiar with some of the functions - like With.

I optimised instead doing -

Workbooks("Prospects.xls").ActiveSheet.Range("A2:A2000").Copy ThisWorkbook.Worksheets("BNIREF").Range("A2")
Workbooks("Prospects.xls").ActiveSheet.Range("A2:A2000").Copy ThisWorkbook.Worksheets("BNI All Prospects").Range("A2")

And also threw in the - Application.ScreenUpdating = False - at the start.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top