Macro is not copying over data correctly.

ghrek

Active Member
Joined
Jul 29, 2005
Messages
427
Hi

I have this macro thats supposed to ask me for a new sheet name ( which it does) , Copy over the data to the new sheet and this is where it goes wrong.

As stated it copies the data over to the new sheet but only in all columns 1-9 as there is data in them rows. Im trying to copy everything even if there is no data in the cells as then its not deleting rows fully.

Any Ideas?

VBA Code:
Option Explicit

Sub TransferData()
Dim lngLastRow As Long
Dim lngRow As Long
Dim strSheetName As String
Dim wsSource As Worksheet

lngLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set wsSource = ActiveSheet

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With

strSheetName = InputBox("Please enter the name of the new worksheet", "Transfer Data")
If StrPtr(strSheetName) = 0 Then
    MsgBox "No worksheet name entered"
    Exit Sub
End If

Sheets.Add(After:=Sheets(Sheets.Count)).Name = strSheetName
wsSource.Range("A1:Y" & lngLastRow).Copy
With ActiveSheet
    .Range("A1").PasteSpecial
    .Range("G10:Y" & lngLastRow).Clear
    For lngRow = lngLastRow To 1 Step -1
        If Not IsEmpty(.Cells(lngRow, "Y")) Then
            If .Cells(lngRow, "Y") = 0 Then
                .Cells(lngRow, "Y").EntireRow.Delete
            End If
        End If
    Next
End With

With Application
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
    .ScreenUpdating = True
End With


End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
wsSource.Range("A1:Y" & lngLastRow).Copy
Here you are copying range A:Y to last row, why di you say it was only columns 1-9 ?
With ActiveSheet .Range("A1").PasteSpecial
Here you paste from cell A1. This looks okay.
.Range("G10:Y" & lngLastRow).Clear
Then you clear the range only from G10 to Y lastrow. This too seems okay.
For lngRow = lngLastRow To 1 Step -1
If Not IsEmpty(.Cells(lngRow, "Y")) Then
If .Cells(lngRow, "Y") = 0 Then
.Cells(lngRow, "Y").EntireRow.Delete
Now you are looping, from last row to top, on column Y and checking if the cells in column Y are not empty; if they are not empty you then check if the cell's value and only if it is 0 you are deleting the entire row.
Is this what you are supposted to be doing ?
 
Upvote 0
Here you are copying range A:Y to last row, why di you say it was only columns 1-9 ?
Here you paste from cell A1. This looks okay.
Then you clear the range only from G10 to Y lastrow. This too seems okay.
Now you are looping, from last row to top, on column Y and checking if the cells in column Y are not empty; if they are not empty you then check if the cell's value and only if it is 0 you are deleting the entire row.
Is this what you are supposted to be doing ?
AAAHHHHHH

Think I may of found answer. If I am reading your reply right I think the issue is .Range("G10:Y" & lngLastRow).Clear

Its clearing the range from G10 to Y last row and then looking for the zero values? I only need it to delete the entire row if the value in a cell of column Y is zero.

How do I get round that?
 
Upvote 0
Well, it seems to me that all you have to do is get rid of that line; so rem the line .Range("G10:Y" & lngLastRow).Clear and have another try.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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