What is wrong with my VB codes? Please help!

plainme

Board Regular
Joined
Mar 23, 2010
Messages
60
I want to copy and paste the Range of A4:H2004 in the tab of DataA to the Range of (A1:H2001) in the tab of DataB. Please see my code below.

Thank you!

Sub CopyPasteNewDataToNewTab()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim A As Range<o:p></o:p>
<o:p> </o:p>
Workbooks("2011Data.xls").Activate<o:p></o:p>
A = Workbooks("2011Data.xls").Sheets("DataA").Range("A4:H2004").Value<o:p></o:p>
Workbooks("2011Data").Sheets("DataB").Range("A1:H2001").Value = A<o:p></o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You can do it in one line:

Sheets("DataA").Range("A4:H2004").Copy Sheets("DataB").Range("A1")

HTH,
 
Upvote 0
You can do it in one line:

Sheets("DataA").Range("A4:H2004").Copy Sheets("DataB").Range("A1")

HTH,

Thanks! It works.

Do you have any idea why the sorting code below does not work?


Range("A1:2001").Select
Selection.Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
 
Upvote 0
Your missing a letter here.
and remove Select.Selection text

Rich (BB code):
Range("A1:A2001").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
 
Upvote 0
Your missing a letter here.
and remove Select.Selection text

Rich (BB code):
Range("A1:A2001").Sort Key1:=Range("K2"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Thanks. Still doesn't work.
 
Upvote 0
Maybe you should post the entire sub.
Are you getting an error or nothing happening at all ?
Have you tried stepping through the code line by line using F8 ?
Usually the sort key is the first cell in the selected range ??, why "K2" ??
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
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