Results 1 to 4 of 4

Issues Copying and Pasting (using Copy method or using Cells().Value)

Threaded View

joe31623 Issues Copying and Pasting ... 12-29-2015, 04:21 PM
JLGWhiz Re: Issues Copying and... 12-29-2015, 08:59 PM
joe31623 Re: Issues Copying and... 12-30-2015, 12:09 AM
Marc L Hi ! There is no issue... 12-29-2015, 09:27 PM
  1. #1
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Issues Copying and Pasting (using Copy method or using Cells().Value)

    I've been struggling with the following code that should be easy.

    Option Explicit
    
    Sub copydata()
    Const DataShtName As String = "Get Data"
    Const PasteShtName As String = "F-28"
    
    Dim rowNum As Integer
    Dim NumOfXs As Integer
    Dim LastCol As Integer
    Dim copyrow As Integer
    Dim RowsToCopy() As Integer
    
    Dim sht As Worksheet
    Dim DestSht As Worksheet
    NumOfXs = 0
    
    
    
    For Each sht In ThisWorkbook.Sheets
        If sht.Name = PasteShtName Then
            Set DestSht = sht
            Exit For
        End If
    Next sht
    
    For Each sht In ThisWorkbook.Sheets
        If sht.Name = DataShtName Then
            LastCol = sht.Range("A1").End(xlToRight).Column
            
            For rowNum = 2 To sht.Range("A1").End(xlDown).Row
                If Cells(rowNum, 9) = "X" Then
                    ReDim Preserve RowsToCopy(NumOfXs)
                    RowsToCopy(NumOfXs) = rowNum
                    NumOfXs = NumOfXs + 1
                End If
            Next rowNum
    'Note: I use rowNum as RowsToCopy element number instead of rowNum below
            copyrow = 2
            If NumOfXs > 0 Then
                For rowNum = 0 To UBound(RowsToCopy)
                
                    DestSht.Range(Cells(copyrow, 1), Cells(copyrow, LastCol)).Value = sht.Range(Cells(RowsToCopy(rowNum), 1), Cells(RowsToCopy(rowNum), LastCol)).Value
                    copyrow = copyrow + 1
                    MsgBox (DestSht.Name)
                Next rowNum
            End If
        End If
        
        Exit For
    Next sht
    
    End Sub
    It fails at line:
    DestSht.Range(Cells(copyrow, 1), Cells(copyrow, LastCol)).Value = sht.Range(Cells(RowsToCopy(rowNum), 1), Cells(RowsToCopy(rowNum), LastCol)).Value
    ...I get an error message using the Range method on the DestSht; however, my locals window shows DestSht as a valid worksheet.

    I also attempted:
    sht.Range(Cells(RowsToCopy(rowNum), 1), Cells(RowsToCopy(rowNum), LastCol)) Copy Destination:= DestSht.Range(Cells(copyrow, 1), Cells(copyrow, LastCol))
    ...without success.

    There must be something I've been missing b/c I've been having similar problems when I help others on the forum and when I attempt to use similar code within my own projects.

    Thanks in advance...
    Last edited by joe31623; 12-29-2015 at 04:23 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: 07-27-2013, 01:59 PM
  2. Replies: 0
    Last Post: 05-15-2013, 03:22 AM
  3. Issues copy and pasting dates from Excel to Access
    By srb104 in forum Excel General
    Replies: 1
    Last Post: 05-03-2012, 03:52 PM
  4. Copy and pasting issues with mouse connected
    By kas106 in forum Excel General
    Replies: 2
    Last Post: 04-11-2012, 04:52 AM
  5. Copying and Pasting Cells
    By JoshF in forum Excel General
    Replies: 3
    Last Post: 01-04-2012, 08:38 PM
  6. Issues with Pasting Cells
    By sarahwilson1000 in forum Excel General
    Replies: 0
    Last Post: 06-22-2011, 02:13 PM
  7. Copying and Copy and Pasting Formula with Dynamic number of rows
    By patelh9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2010, 11:33 PM
  8. Copy method of worksheet class failed - loop copying worksheets
    By lif in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-31-2006, 05:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1