Copy Data from Sheet1 to Sheet2

Shamsuddeen

Active Member
Joined
Feb 16, 2002
Messages
292
Hi All,

I need a macro to copy data from Sheet1 to Sheet2. the data in sheet1 houses in Range A5:D10.

But there could be some blank cells within the above range and those rows should not be copied to sheet2.



Thanks.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
In sheet2, the data from sheet1 should be copied in such a way that there should be no blank rows in between the records.
 
Upvote 0
Try This:
Code:
Sub CopyData()
Dim DataArray(10, 4) As Variant
Dim TheLen As Long
Dim Fnd As Integer
Dim X, Y As Long

Sheets("Sheet1").Select
For X = 5 To 10
    For Y = 1 To 4
        DataArray(X, Y) = Cells(X, Y).Value
    Next
Next

Sheets("sheet2").Select
For X = 5 To 10
    Let TheLen = 0
    For Y = 1 To 4
        Let TheLen = TheLen + Len(DataArray(X, Y))
    Next
    If TheLen = 0 Then
    Else
        Fnd = Fnd + 1
        For Y = 1 To 4
            Cells(Fnd, Y).Value = DataArray(X, Y)
        Next
    End If
Next


End Sub
 
Upvote 0
Thanks. It works perfectly.

How about if I want to ignore copying the rows from sheet1 whose any one or more cells are empty.

Thanks
 
Upvote 0
This would do that:
Code:
Sub CopyData()
Dim DataArray(10, 4) As Variant
Dim TheLen As Long
Dim Fnd As Integer
Dim X, Y As Long

Sheets("Sheet1").Select
For X = 5 To 10
    For Y = 1 To 4
        DataArray(X, Y) = Cells(X, Y).Value
    Next
Next

Sheets("sheet2").Select
For X = 5 To 10
    Let TheLen = 0
    For Y = 1 To 4
        If Len(DataArray(X, Y)) < 1 Then
            TheLen = 0
            Exit For
        Else
            Let TheLen = 1
        End If
    Next
    If TheLen = 0 Then
    Else
        Fnd = Fnd + 1
        For Y = 1 To 4
            Cells(Fnd, Y).Value = DataArray(X, Y)
        Next
    End If
Next


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,858
Messages
6,121,960
Members
449,057
Latest member
FreeCricketId

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