Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 23rd, 2005, 11:45 AM
Authorized User
 
Join Date: Oct 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shattered Send a message via Yahoo to shattered
Default

you might also want to consider sending me the data you are manipulating. The code seems to work fine on the test versions I've done so there must be something that we are missing..

 
Old November 23rd, 2005, 11:51 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

15 12 16
14 14
        16 12
14
16 12
56
45
         12 16
i just made this up to test
my sheets aren't protected(at least i didn't protect them so unless it is default..) and nothing is merged(unless thats default)
 
Old November 23rd, 2005, 12:31 PM
Authorized User
 
Join Date: Oct 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shattered Send a message via Yahoo to shattered
Default

ok, confused now, if you open a new workbook and type the above data into columns 1, 17 & 21 and then run the macro it works for me without any problem at all..

can you try the code in a new workbook and see what happens

also, what version of execl are you running ?



 
Old November 23rd, 2005, 01:17 PM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it gives me a runtime error-"Application defined or object defined error" when i press play then when i restart and press the command button it changes to the "select method of range class failed" error even when i did it in a new book. i'm running excell 2003
 
Old November 23rd, 2005, 10:46 PM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i probably should have mentioned this earlier, i don't know if it matters but i'm using visual basic for applications
The problem lies only with the range selecting, when i select a range such as "A1" it will select it and past without problems
however as you can guess that means it repastes in the same spot each time. Also, im running it off of a button. when i run it off of the run macro on the top toolbar it just gives me a msgbox"400",vbcritical
 
Old November 24th, 2005, 03:43 AM
Authorized User
 
Join Date: Oct 2004
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shattered Send a message via Yahoo to shattered
Default

still shouldn't make any difference, ran the code in excel 2003 without difficulty.. Have you modified the code at all - can your paste your exact code here if its not the same as above.


 
Old November 24th, 2005, 05:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Have you tried using code where you don't select any cells. Usually its not necessary to select cells / sheets in your code and will considerably speed up the macro when you cut out the selecting.

You might also consider turning Application.Calculation to xlCalculationManual while the model is running if the spredsheet is big and takes a long time toi recalculate after each paste operation.

The only other quirk with the code posted is that you cannot use the Paste method on a range object (annoyingly enough). You have to use the PasteSpecial method and then select Paste:=xlPasteAll. NB If you just want formulae / formats / values then have a look at the excel VBA help files for the other agruments that you can pass to this method.

Code:
Sub CopyData()

Dim shOrig As Worksheet
Dim shNew As Worksheet
Dim lngLoop As Long
Dim lngOutRow As Long

    ' Set up variables
    Set shOrig = ThisWorkbook.Sheets("Sheet1")
    Set shNew = ThisWorkbook.Sheets("Sheet2")
    lngOutRow = 1

    Application.ScreenUpdating = False

    ' Clear New sheet
    shNew.UsedRange.Clear

    ' Loop down Original sheet
    For lngLoop = 2 To 50000

        ' Test columns 1, 17 & 21 for criteria
        If shOrig.Cells(lngLoop, 1) > 0 And _
            shOrig.Cells(lngLoop, 17) = 0 And _
            shOrig.Cells(lngLoop, 21) = 0 Then

            ' Copy Row
            shOrig.Rows(lngLoop).Copy

            ' Paste Row
            shNew.Cells(lngOutRow, 1).PasteSpecial Paste:=xlPasteAll

            ' Clear the clipboard
            Application.CutCopyMode = False

            ' Increment Output Row counter
            lngOutRow = lngOutRow + 1

        End If

    Next lngLoop

    Application.ScreenUpdating = True

End Sub
 
Old November 25th, 2005, 01:11 AM
Authorized User
 
Join Date: Nov 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

shattered-thanks for the help i don't know what was wrong it looked good to me, and i was using your exact code(except the next iloop)
maccas- your's worked thanks alot





Similar Threads
Thread Thread Starter Forum Replies Last Post
pasting data from clipboard rahesh Visual Basic 2005 Basics 0 May 23rd, 2008 12:40 PM
Pasting into spreadsheets IainAL VB How-To 0 April 23rd, 2007 04:33 AM
Copy and Pasting jilly Beginning VB 6 1 March 8th, 2006 09:46 AM
Copieng and pasting with VBA vorax187 Access VBA 2 October 13th, 2005 11:58 AM
Very easy problem for an expert Nicky2k ASP.NET 2.0 Basics 3 April 21st, 2005 07:18 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.