vba remove blank cells from different columns

dappy

Board Regular
Joined
Apr 23, 2018
Messages
124
Office Version
  1. 2013
Platform
  1. Windows
Hi folks,

I have this situation, where i need to copy the whole sheet to a blank sheet but only the cells with information. i have the below code but i cant get it to work correctly. i've tried to do it per column but that doesnt work either.

any help greatly appreciated.


test_rc1.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1startfirstsecthroperstart firsthapsoperstartsecthirdstartfirstsecthroperstart firsthapsoperstartsecthird
2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
11xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
12xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
13xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
14xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
15xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
16xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
17xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
18xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
19xxxxxxxxxxxxxxxxxxxxxxxx
20xxxxxxxxxxxxxxxxxxxxxxxx
21xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
22xxxxxxxxxxxxxxxxxxxxxx
23xxxxxxxxxxxxxx
24xxxxxxxxxxxxxxxxxxxxxx
25xxxxxxxxxxxxxxxxxxxxxx
26xxxxxxxx
27xxxxxxxx
28xxxxxxxx
29xxxxxxxx
30xxxxxxxx
Sheet1



VBA Code:
Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
  
   Ary = Intersect(Sheets("sheet1").UsedRange, Sheets("sheet1").Range("A:K"))
   ReDim Nary(1 To UBound(Ary), 1 To 16)
   For r = 1 To UBound(Ary)
      If Ary(r, 1) <> "" Then
         nr = nr + 1
         For c = 1 To UBound(Ary, 2)
            Nary(nr, c) = Ary(r, c)
         Next c
      End If
   Next r
   Sheets("sheet2").Range("A1").Resize(nr, 16).Value = Nary
   '  End Sub
'Private Sub CopyItOver()
  Set NewBook = Workbooks.Add
  Workbooks("test_rc1.xlsm").Worksheets("sheet2").Range("A1:P1000").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
 
Last edited by a moderator:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Cancel that folks, figured it out. thanks anyway
 
Upvote 0
Cancel that folks, figured it out. thanks anyway

Glad to hear you figured it out.

Do you mind posting about your solution? Then it is perfectly fine to mark your post as the solution to help future readers.

Thanks.
 
Upvote 0
so, somewhat untidy i know but this works for me. for each set of columns have a set of declared variables. below is a selection of three sets, should be self explanatory. with my data the sets of columns have the same count, i think that's where i was going wrong.

VBA Code:
Sub delete_blank_2()

Dim Ary As Variant, Nary As Variant
Dim r As Long, c As Long, nr As Long
Dim AryQ As Variant, NaryQ As Variant
Dim rQ As Long, cQ As Long, nrQ As Long
Dim Aryw As Variant, Naryw As Variant
Dim rw As Long, cw As Long, nrw As Long

Sheets("sheet2").Cells.Clear

Ary = Intersect(Sheets("sheet1").UsedRange, Sheets("sheet1").Range("b:c"))
ReDim Nary(1 To UBound(Ary), 1 To 16)
For r = 1 To UBound(Ary)
If Ary(r, 1) <> "" Then
nr = nr + 1
For c = 1 To UBound(Ary, 2)
Nary(nr, c) = Ary(r, c)
Next c
End If
Next r
Sheets("sheet2").Range("b1").Resize(nr, 16).Value = Nary




AryQ = Intersect(Sheets("sheet1").UsedRange, Sheets("sheet1").Range("e:g"))
ReDim NaryQ(1 To UBound(AryQ), 1 To 16)
For rQ = 1 To UBound(AryQ)
If AryQ(rQ, 1) <> "" Then
nrQ = nrQ + 1
For cQ = 1 To UBound(AryQ, 2)
NaryQ(nrQ, cQ) = AryQ(rQ, cQ)
Next cQ
End If
Next rQ
Sheets("sheet2").Range("e1").Resize(nr, 16).Value = NaryQ



Aryw = Intersect(Sheets("sheet1").UsedRange, Sheets("sheet1").Range("i:j"))
ReDim Naryw(1 To UBound(Aryw), 1 To 16)
For rw = 1 To UBound(Aryw)
If Aryw(rw, 1) <> "" Then
nrw = nrw + 1
For cw = 1 To UBound(Aryw, 2)
Naryw(nrw, cw) = Aryw(rw, cw)
Next cw
End If
Next rw
Sheets("sheet2").Range("i1").Resize(nr, 16).Value = Naryw

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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