writing better code

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,104
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All

I am trying to re-write my code better, rather than posting my entire code, I am adjusting what I know I can do.
I have just got stuck at this bit.

I was using this
Code:
Sheets("GET MEETING FROM RP").Select: Range("A1").Select
ActiveSheet.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False: Range("A1").Select

But i think it may be better written something like this.

Code:
With Worksheets("GET MEETING FROM RP")
.Pictures.Delete
.Columns("A:AA").Clear
IE1.ExecWB 17, 0: IE1.ExecWB 12, 2
Range("A1").PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False: Range("A1").Select

I know i have extra code on this one, i just wanted you to see the with worksheets part.

Any one know what i am doing wrong???

Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,

With's need End With's.

Also the Range needs to be dotted to "chain" it to the worksheet you are "withing".
Code:
[COLOR="Navy"]With[/COLOR] Worksheets("GET MEETING FROM RP")
    .Pictures.Delete
    .Columns("A:AA").Clear
    IE1.ExecWB 17, 0
    IE1.ExecWB 12, 2
    .Range("A1").PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
 
Upvote 0
xenou

great, many thanks, i knew i need the end with but it was the dot, fantastic. I will test it.

dave
 
Upvote 0
xenou

Im am getting an error, object defined error, since i changed.

Any ideas
 
Upvote 0
Not sure the IE1 object could be unset. What line does it error on?
 
Upvote 0
It errors here

Code:
.Range("A1").PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False</pre>
 
Upvote 0
Okay. I don't see Format as a valid parameter for PasteSpecial

Maybe try
.Range("A1").PasteSpecial
 
Upvote 0
Actually, it appears you want the worksheet pastespecial method (not the Range pasteSpecial method). For this, the worksheet must be activated:


Code:
.Activate
.Range("A1").Activate
.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False
 
Upvote 0
xenou

Sorry for the delayed response.

For some reason, the new code is not pasting my entire data, something to do with paste area does not match in size.
After reading about activating, is it not the same a sheet selection?

I am trying to avoid selecting sheets as I read it can slow code down.

I thank you for your help.

Dave
 
Upvote 0
THis is a case where you cannot avoid selection (unless you know for sure the sheet is active and you know for sure that you are at Cell A1 on that sheet (where you want to paste) -- as in your original code from post 1. I dont' know what the IE1 stuff is all about so that's a wildcard.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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