new project: copy range without empty or blank rows

Keebler

Board Regular
Joined
Dec 1, 2021
Messages
167
Office Version
  1. 2021
Platform
  1. Windows
I am currently working on a project to copy a range from one worksheet to another removing empty or blank cells (rows) and pasting them into another ws at the bottom of the page.

VBA Code:
Sub copyto_test()
'define variables
Dim lrow As Long, srow As Long, erow As Long, crow As Long
Dim slist As String, srng As String, trng As String
Dim aws As Worksheet, sws As Worksheet, tws As Worksheet
Dim crg As Range

'set constants
Set aws = activesheet
Set tws = Sheets("INDEX")

lrow = tws.Range("e1") 'gets the last row of destination ws
If lrow <= 3 Then 'checks to make sure row is at least row 3
trng = tws.Range("a3").Address
Else
trng = tws.Range("a" & (lrow + 1))
End If
crow = aws.Range("e1") 'gets the last row of the current sheet
srow = aws.Range("h1") 'gets the first row of the current sheet
srng = Range("aa" & crow)
slist = ("k" & srow & ":" & srng)


Range(slist).Copy Range(trng).PasteSpecial(xlPasteValues)




End Sub
so the problem is the last line.

Range(slist).Copy Range(trng).PasteSpecial(xlPasteValues)

unable to get the pastespecial property of the range class

im sure it is something stupid im missing...

no other errors are showing up --- at this time (and no i havent tried the removal of the blank rows yet)
 
Did you try the new code I put on post #14. You had said it worked before but needed the filter removing and delete empty rows instead. The above code in post #14 does that b
MY BAD!!!
I failed to see your new post when I posted the 2 follow up posts.

I will run the revision and report back... (which ironically is quite similar to what you posted) (I see that you have added the clipboard clean, and the autofiltering turned off)
VBA Code:
Sub delblnkrows_test5() 'failed
Dim tws As Worksheet
Dim lrow As Long, i As Long
Dim rng As Range

Set tws = Sheets("INDEX")
'Set rng = ws.Range("a3:a20")
lrow = tws.Range("a" & tws.Rows.Count).End(xlUp).row
  

 ' Filter out blank or empty rows in the target worksheet
    Set rng = tws.Range("A3:A" & lrow)
    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    tws.AutoFilterMode = False
 
  
    Application.CutCopyMode = False

End Sub
result
same error

run time error '1004':
No cells were found

I can clearly see multiple empty rows

FLAC MASTERS 3 1 555b307.xlsm
A
18ABBA - I HAVE A DREAM - 100% ABBA {DISC 1} 2019 - 19 - FMA0101 [ALBUMS]
19ABBA - OUR LAST SUMMER - 100% ABBA {DISC 1} 2019 - 23 - FMA0101 [ALBUMS]
20ABBA - THE DAY BEFORE YOU CAME - 100% ABBA {DISC 1} 2019 - 24 - FMA0101 [ALBUMS]
21ABBA - ANGEL EYES - 100% ABBA {DISC 1} 2019 - 25 - FMA0101 [ALBUMS]
22
23
24ABBA - SUMMER NIGHT CITY - 100% ABBA {DISC 2} 2019 - 01 - FMA0101 [ALBUMS]
25ABBA - HEAD OVER HEELS - 100% ABBA {DISC 2} 2019 - 02 - FMA0101 [ALBUMS]
26ABBA - EAGLE (SHORT VERSION) - 100% ABBA {DISC 2} 2019 - 03 - FMA0101 [ALBUMS]
27ABBA - THE VISITORS - 100% ABBA {DISC 2} 2019 - 04 - FMA0101 [ALBUMS]
INDEX


FLAC MASTERS 3 1 555b307.xlsm
A
50ABBA - BUMBLEBEE - VOYAGE 2021 - 08 - FMA0101 [ALBUMS]
51ABBA - ODE TO FREEDOM - VOYAGE 2021 - 10 - FMA0101 [ALBUMS]
52
53
54
55
56ACE OF BASE - WONDERFUL LIFE - DA CAPO [JAPAN PROMO] 2003 - 07 - FMA0101 [ALBUMS]
57
58
59ACE OF BASE - TRAVEL TO ROMANTIS - FLOWERS 1998 - 04 - FMA0101 [ALBUMS]
60ACE OF BASE - DONNIE - FLOWERS 1998 - 13 - FMA0101 [ALBUMS]
61ACE OF BASE - CRUEL SUMMER {BONUS MIX} - FLOWERS 1998 - 14 - FMA0101 [ALBUMS]
62
63
64ACE OF BASE - EVERYTIME IT RAINS - GREATEST HITS 2000 - 03 - FMA0101 [ALBUMS]
65ACE OF BASE - BEAUTIFUL LIFE {JUNIOR VASQUEZ MIX} - GREATEST HITS 2000 - 12 - FMA0101 [ALBUMS]
66
67
68ACE OF BASE - CRUEL SUMMER {BONUS MIX} - GREATEST HITS 2008 - 12 - FMA0101 [ALBUMS]
69
70
71ACE OF BASE - EVERYTIME IT RAINS - PLAYLIST THE VERY BEST OF ACE OF BASE 2011 - 09 - FMA0101 [ALBUMS]
72ACE OF BASE - ALWAYS HAVE, ALWAYS WILL - PLAYLIST THE VERY BEST OF ACE OF BASE 2011 - 12 - FMA0101 [ALBUMS]
73
74
75ACE OF BASE - LOVE IN DECEMBER - SINGLES OF THE 90S 1999 - 06 - FMA0101 [ALBUMS]
76ACE OF BASE - CRUEL SUMMER - SINGLES OF THE 90S 1999 - 11 - FMA0101 [ALBUMS]
77ACE OF BASE - NEVER GONNA SAY I'M SORRY - SINGLES OF THE 90S 1999 - 14 - FMA0101 [ALBUMS]
78
79
80ACE OF BASE - RAVINE - THE BRIDGE [JAPAN EDITION] 1995 - 06 - FMA0101 [ALBUMS]
81ACE OF BASE - WHISPERS IN BLINDNESS - THE BRIDGE [JAPAN EDITION] 1995 - 09 - FMA0101 [ALBUMS]
82ACE OF BASE - YOU AND I - THE BRIDGE [JAPAN EDITION] 1995 - 11 - FMA0101 [ALBUMS]
INDEX
 
Last edited:
Upvote 0
what i can not understand is why this works

VBA Code:
Sub delblnkrows_test4() 'worked
Dim ws As Worksheet
Dim lrow As Long
Dim rng As Range

Set ws = Sheets("INDEX")
Set rng = ws.Range("a3:a20")

'rng.SpecialCells(xlCellTypeBlanks).EntireRow.Select
rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub

FLAC MASTERS 3 1 555b307.xlsm
A
3test
4test
5
6
7test
8test
9test
10test
11
12test
13test
14
15
16
17
18test
19test
INDEX


FLAC MASTERS 3 1 555b307.xlsm
A
3test
4test
5test
6test
7test
8test
9test
10test
11test
12test
13
14
15
16
17
18
19
INDEX
 
Upvote 0
Try this.

VBA Code:
Sub delblnkrows_test4()
    Dim ws As Worksheet
    Dim lrow As Long
    Dim rng As Range
    Dim blankCells As Range

    Set ws = Sheets("INDEX")
    Set rng = ws.Range("A3:A20")

    On Error Resume Next
    Set blankCells = rng.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If Not blankCells Is Nothing Then
        ' Delete entire rows containing blank cells
        blankCells.EntireRow.Delete
    Else
        MsgBox "No blank cells found in the specified range."

    End If
End Sub

This code checks for blank cells in A3:A20 before attempting to delete rows. If no blank cells are found, it displays a message box stating that no blank cells were found.
 
Upvote 0

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.
SAME ERROR (no blank cells found)
just a message box this time

I DID change the range to a3:a30 no change same error

VBA Code:
Sub delblnkrows_test6()
    Dim ws As Worksheet
    Dim lrow As Long
    Dim rng As Range
    Dim blankCells As Range

    Set ws = Sheets("INDEX")
    Set rng = ws.Range("A3:A30")

    On Error Resume Next
    Set blankCells = rng.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    If Not blankCells Is Nothing Then
        ' Delete entire rows containing blank cells
        blankCells.EntireRow.Delete
    Else
        MsgBox "No blank cells found in the specified range."

    End If
End Sub
FLAC MASTERS 3 1 555b307.xlsm
A
15ABBA - FERNANDO - 100% ABBA {DISC 1} 2019 - 16 - FMA0101 [ALBUMS]
16ABBA - THE NAME OF THE GAME - 100% ABBA {DISC 1} 2019 - 17 - FMA0101 [ALBUMS]
17ABBA - CHIQUITITA - 100% ABBA {DISC 1} 2019 - 18 - FMA0101 [ALBUMS]
18ABBA - I HAVE A DREAM - 100% ABBA {DISC 1} 2019 - 19 - FMA0101 [ALBUMS]
19ABBA - OUR LAST SUMMER - 100% ABBA {DISC 1} 2019 - 23 - FMA0101 [ALBUMS]
20ABBA - THE DAY BEFORE YOU CAME - 100% ABBA {DISC 1} 2019 - 24 - FMA0101 [ALBUMS]
21ABBA - ANGEL EYES - 100% ABBA {DISC 1} 2019 - 25 - FMA0101 [ALBUMS]
22
23
24ABBA - SUMMER NIGHT CITY - 100% ABBA {DISC 2} 2019 - 01 - FMA0101 [ALBUMS]
25ABBA - HEAD OVER HEELS - 100% ABBA {DISC 2} 2019 - 02 - FMA0101 [ALBUMS]
26ABBA - EAGLE (SHORT VERSION) - 100% ABBA {DISC 2} 2019 - 03 - FMA0101 [ALBUMS]
27ABBA - THE VISITORS - 100% ABBA {DISC 2} 2019 - 04 - FMA0101 [ALBUMS]
28ABBA - ON AND ON AND ON - 100% ABBA {DISC 2} 2019 - 06 - FMA0101 [ALBUMS]
29ABBA - I WONDER (DEPARTURE) - 100% ABBA {DISC 2} 2019 - 09 - FMA0101 [ALBUMS]
30ABBA - LOVELIGHT {ORIGINAL VERSION} - 100% ABBA {DISC 2} 2019 - 11 - FMA0101 [ALBUMS]
INDEX



is it at all possible that the errors are a result of the formatting? (i ask ,because cells with one word "test" work fine) the data is copied from another sheet (values only) with no formulas or extra nonsense
or does the VBA code not care what is actually in the cells being evaluated? barring a blank or empty cell

i changed the range to a20:a30 (just to try it)
result: same error
(no blank cells found in the specified range)
 
Last edited:
Upvote 0
FLAC MASTERS 3 1 555b307.xlsm
A
135
136
137
138
139
140test
141test
142
143
144
145
146test
147test
148test
149test
150
151
152test
153test
154
155
INDEX


the only change
Set rng = ws.Range("A135:A155")

FLAC MASTERS 3 1 555b307.xlsm
A
135test
136test
137test
138test
139test
140test
141test
142test
143
144
145
146
147
148
149
150
151
152
153
154
155
INDEX


this doesnt make any sense to me..
can you please explain this to me?
 
Upvote 0
i tried changing the range
Set rng = ws.Range("A3:j60") I wanted to rule out the range not seeing the "whole" line even though it is all in the A column

this had a TERRIBLE result
deleted EVERYTHING above a60 and moved everything up to a3

is it possible that the VBA code is somehow not seeing the A column and looking elsewhere?
 
Upvote 0
I really do not understand what is happening here. Let's try a different approach.
The following code will copy the range from one worksheet to another removing empty or blank rows and then paste them into another sheet.
VBA Code:
Sub copyto_test()

    Dim lrow As Long, srow As Long, erow As Long, crow As Long
    Dim slist As String, srng As String, trng As String
    Dim aws As Worksheet, sws As Worksheet, tws As Worksheet
    Dim crg As Range

    Set aws = ActiveSheet
    Set tws = Sheets("INDEX")

    lrow = tws.Cells(tws.Rows.Count,"E").End(xlUp).Row

    If lrow <= 3 Then
        trng = tws.Range("A3").Address
    Else
        trng = tws.Cells(lrow + 1, 1).Address
    End If

    crow = aws.Cells(aws.Rows.Count, "E").End(xlUp).Row
    srow = aws.Cells(aws.Rows.Count, "H").End(xlUp).Row
    srng = "AA" & crow
    slist = "K" & srow & ":" & srng

 aws.Range(slist).SpecialCells(xlCellTypeConstants).Copy
    tws.Range(trng).PasteSpecial xlPasteValues

    Application.CutCopyMode = False
End Sub
 
Upvote 0
so, i tried the new vba code
VBA Code:
Sub copyto_test_b9()

    Dim lrow As Long, srow As Long, erow As Long, crow As Long
    Dim slist As String, srng As String, trng As String
    Dim aws As Worksheet, sws As Worksheet, tws As Worksheet
    Dim crg As Range

    Set aws = activesheet
    Set tws = Sheets("INDEX")

    lrow = tws.Cells(tws.Rows.Count, "a").End(xlUp).row

    If lrow <= 3 Then
        trng = tws.Range("A3").Address
    Else
        trng = tws.Cells(lrow + 1, 1).Address
    End If

crow = aws.Range("e1") 'gets the last row of the current sheet
srow = aws.Range("h1") 'gets the first row of the current sheet
    srng = "k" & crow
    slist = "K" & srow & ":" & srng

 aws.Range(slist).SpecialCells(xlCellTypeConstants).Copy
    tws.Range(trng).PasteSpecial xlPasteValues

    Application.CutCopyMode = False
End Sub

as you can see, i had to change a couple lines to more closely fit what I have (also I changed the range to slim it down to only one column)
the code failed but the range variables all reported the correct values (k8:k914), and the fullrange (slist) showed everything correct;y


Run-time error '1004':
No cells were found

im at a loss as to why NOW I am getting the no cells fund error...


note:
my original vba code works just fine copying the data from one sheet to the INDEX page
 
Last edited:
Upvote 0
i think the issue with the revised VBA code is the
aws.Range(slist).SpecialCells(xlCellTypeConstants).Copy

specifically the xlCellTypeConstants

so, for clarification : what is a constant within the scope of EXCEL?
 
Upvote 0

Forum statistics

Threads
1,216,128
Messages
6,129,030
Members
449,482
Latest member
al mugheen

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