Consulting

Results 1 to 13 of 13

Thread: Solved: Delete empty rows and copy to another worksheet

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location

    Solved: Delete empty rows and copy to another worksheet

    I've got about 2K row of data inported from .txt format (so there are empty rows throughout). I need to delete the empty rows and then copy the contguous data to another spreadsheet. I've got this code, but am wondering if it is the most affective way to do that?

    [VBA]
    Option Explicit
    Sub DeleteEmptyRows()
    Dim i As Long
    Application.ScreenUpdating = False
    For i = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
    If Application.WorksheetFunction.CountA(Sheets(1).Cells(i, 1).EntireRow) = 0 Then
    Sheets(1).Cells(i, 1).EntireRow.Delete
    End If
    Next
    i = 0
    For i = 1 To Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row
    If Not Sheets(1).Cells(i, 1).EntireRow Is Nothing Then
    Sheets(1).Cells(i, 1).EntireRow.Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp).Offset(1)
    End If
    Next

    Application.ScreenUpdating = True
    End Sub
    [/VBA]

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hey Mark,

    Define "blank rows". Is that, where there is no data in the row? Is it when there is no data in column A of each row? We can do either without looping.

  3. #3
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    Between the data, some of the entire row is blank when it comes in, so the 2K rows of data are spread out over about 3K+ of actual space on the sheet.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Headers on row 1 with data on row 2?

  5. #5
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    Hey Zack,

    No headers on the text file, only the data. I open the text files (access reports exported as text format) to retrieve the data and append into another sheet, then close the text file without saving. The headers are on the sheet the text file data is getting copied to, which is why I put the Offset on the copy.

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hmm, why even come out of Access? Anyway, this might get you what you want...

    [vba]Sub DeleteBlanksAndCopyNons()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rngFormula As Range, strLastCol As String
    Set ws1 = Sheets(1)
    Set ws2 = Sheets(2)
    'Delete wholly blank rows
    ws1.Columns(1).Insert
    Set rngFormula = ws1.Range("A2:A" & ws1.Cells(ws1.Rows.Count, "B").End(xlUp).Row)
    strLastCol = ws1.Cells(2, ws1.Columns.Count).Address(0, 0)
    rngFormula.Formula = "=COUNTA(B2:" & strLastCol & ")"
    ws1.Columns(1).AutoFilter 1, 0
    rngFormula.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ws1.Columns(1).Delete
    ws1.AutoFilterMode = False
    'Copy the other values to next sheet
    ws1.Range("A2:F" & ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row).Copy ws1.Cells(1, 1)
    Application.CutCopyMode = False
    End Sub[/vba]

    Try it on test data first. Also, ensure the ranges are correct. I guessed that column F is the last column, change as necessary.

  7. #7
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    Thanks Zack. I have to go through access to obtain the data off our server (TN). There is probably a way to do that via VBA, but that is WAY over my head

    I tried your code, and at first it didn't work (deleted the rows but didn't copy). Then I noticed your copy sequence was from Ws1 to Ws1. I changed Ws1 to Ws2 and it worked great.

    Thanks for your time

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Ah, thanks for the catch! Glad it worked for you after you cleaned up my mess.

    You should start another thread about obtaining data from your server.

  9. #9
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    CodeMakr,
    You most likely could have revised the access report SQL, and added a condition <>"" in one of the columns to eliminate it at the source. Let me know if you are interested in understanding how to do this? or if it's just a one time thing for you.

    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  10. #10
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    Cal,
    In the access report there are no blank rows. They only appear in the text file I export (i.e., you don't see them in printed form, only when you open the text file in excel). Not sure why this happens? I guess my question is.....is this an access export/setup issue or an excel import/load issue?

  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Can you export from Access to Excel instead of an intermediary text file? Or how about both?

  12. #12
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    The report must be based on a query in the query screen. The report most likely formats the data into a presentable manor, but this format does not translate to excel very well, which is most likely why the data is messed up in excel. I would suggest you find the underlying query and do an export of that.

    HTH
    Cal
    The most difficult errors to resolve are the one's you know you didn't make.


  13. #13
    VBAX Regular
    Joined
    Dec 2006
    Posts
    69
    Location
    I am connecting via terminal server to the database, and there is no excel. The lag is pretty bad, so the export in .txt format turned out to be the fastest way to go. I'll try exporting the query and see what it gives me.

    Thanks for all the help Zack/Cal

Posting Permissions

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