Link to home
Start Free TrialLog in
Avatar of Billa7
Billa7

asked on

Modify Copy and Paste script

I need Expert help to fix this problem. The attached script is used to copy data from .xls workbook into Data sheet. The script only copy data which is start at 0600 and above; omit time before 0600. However, this rules only applicable for the start of the day, not after midnight. I hope Expert rectify this by allowing copy of the time after midnight. The removing of before 0600 only for start of the day. I have attached the workbook together with sample data files for Experts perusal.
Sub Copy_Paste()
    Dim wb As Workbook
    Dim fName As String
    Dim strFilePath, lcTargetCell
    Dim strPath As String
    Dim intSrcRows As Integer
    Dim intTgtRows As Integer
    Dim rng As Range
    Dim r As Range
    Dim rDelete As Range
    Dim objFileDLG As Office.FileDialog
    
    
    Application.ScreenUpdating = False
    
    strPath = "D:\\"
    
    
    
    'intTgtRows = 6
    intTgtRows = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row + 1
     
    Set objFileDLG = Application.FileDialog(msoFileDialogFilePicker)
 
    Do While True
        strFilePath = ""
        With objFileDLG
            .Filters.Add "Excel Files", "*.xls", 1
            .FilterIndex = 1
            .InitialFileName = strPath
            .AllowMultiSelect = False
            .Title = "Select The Workbook to copy From "
            If .Show() <> 0 Then
                strFilePath = .SelectedItems(1)
            End If
        End With
        
        If Trim(strFilePath) = "" Then Exit Do
        
        Set wb = Workbooks.Open(strFilePath)
                
        wb.Activate
        intSrcRows = wb.Worksheets(1).Cells(Cells.Rows.Count, "A").End(xlUp).Row
        wb.Worksheets(1).Range("A2:D" & intSrcRows).Copy
        
        lcTargetCell = "A" & intTgtRows
         
        ThisWorkbook.Worksheets(2).Activate
        Range(lcTargetCell).Select
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
                   xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        wb.Close
        Set wb = Nothing
        
        Set rng = Selection.Resize(Selection.Rows.Count, 1)
        For Each r In rng
            If r.Offset(0, 1).Value < "0600" Then
                Debug.Print r.Offset(0, 1).Value, "0600"
                If rDelete Is Nothing Then
                    Set rDelete = r
                Else
                    Set rDelete = Union(rDelete, r)
                End If
            End If
        Next r
        
        rDelete.EntireRow.Delete
        
        intTgtRows = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row + 1
        

        Set rDelete = Nothing
    Loop
    
    Application.ScreenUpdating = True
End Sub

Open in new window

CopyData.xls
Sample2.xls
Sample3.xls
Sample4.xls
Avatar of Norie
Norie

In your worksheet's the times before 0600 are at the start of the day.

Perhaps you can see that if you sort the data by date then time.

I think to do what you want you'll at least need to include dates.
Avatar of Billa7

ASKER

Hi Imnorie,

Is that any chance to copy data at 0600 and above from the source (sample), ignore data before 0600 when we start copying the data? If this is not possible, perhaps adding date would be the best option.
I've just had another look at Sample2,3 and 4.

In column A, header Date, the values are date/time but only for one day.

In the code you are looking at column B to check what is to be deleted.

Column B has the time from column A as text.

Does your actual data have both of these columns?
Avatar of Billa7

ASKER

Hi Imnorie,

Yes, you're right. Both columns are just mirror copy of my original data.
Avatar of Billa7

ASKER

Hi Imnorie,

Please let me know if you need more input from me.
ASKER CERTIFIED SOLUTION
Avatar of Rob Brockett
Rob Brockett
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
billa7

I still don't understand what the column with the time as text is for.

Also, it's still not clear what you want to copy.

You say it's anything after 0600  but you also want to keep time after midnight.

Time after midnight is on the next day and is going to be before 0600 on that day.

Perhaps I'm just missing something or being confused  by the data in the sample files not being sorted chronologically.
Avatar of Billa7

ASKER

Hi Imnorie,

If you noticed, all my sample especially first row of each file has started with previous date followed by set of data with time sequence; 0600 to 0555 (24 hrs). My main concern is to delete the first row of the data if the date (time) not concurrence with other data, e.g. sample 3, first row is 11-Feb-0320, but other data (time) referring to 12-Feb, we can considered that the 11-Feb (first row) data is actually overflowed data from the previous date and I need to delete this overflow data. Hope I'm not confusing again.

My main main objective is to delete the first row of the data if the date (time) not match with other data of the file.
So just delete any data where the time isn't on the same day as the rest of the data?

Or is it just the first row?
Avatar of Billa7

ASKER

Hi Imnorie,

Just the first row.
Avatar of Billa7

ASKER

Hi Imnorie,

Is a way to omit the first line when we copying the data into data sheet, instead of copy and then delete the row?
Avatar of Billa7

ASKER

Hi Rob,

Thanks a lot for the suggestion. Sorry for late reply.
hi Billa7,

I don't understand why you have given me the points for my single comment when Norie has been asking questions to clarify what you need so he can provide a solution.

Can you please post your final code, so we can see how you solved your problem?

Thanks
Rob