Remove empty cell from array

adnan1975

New Member
Joined
Aug 24, 2017
Messages
38
I am trying to remove empty cells from Array but the results are not I am expecting. The codes runs fine but repeat some values from row 11 to 14. Any help is highly appreciated.

VBA Code:
Dim a() As Variant

a = Data.Range("C2:C" & LR3).Value2

Dim j As Long, jj As Long: jj = 1
For j = 1 To UBound(a)
If a(j, 1) <> "" Then
a(jj, 1) = a(j, 1)
jj = jj + 1
End If
Next j

 Data.Range("E2").Resize(UBound(a)).Value2 = a

AAPL LEAP OPTION TEST_REVA.xlsm
CDE
1
2200
3100
420023.12
5100-46.24
6233.74
74.12
8
9
10
1123.1223.12
12-46.24-46.24
13233.74233.74
144.124.12
15
16
17
18
Sheet1
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
How about
Excel Formula:
Dim a As Variant, b As Variant

a = Data.Range("C2:C" & LR3).Value2
ReDim b(1 To UBound(a), 1 To 1)
Dim j As Long, jj As Long: jj = 1
For j = 1 To UBound(a)
If a(j, 1) <> "" Then
b(jj, 1) = a(j, 1)
jj = jj + 1
End If
Next j

 Data.Range("E2").Resize(jj).Value2 = b
 
Upvote 0
Solution
With loop

VBA Code:
Sub removeempty()
  Dim i As Long, j As Long
  j = 2
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value <> "" Then
      Range("E" & j).Value = Range("C" & i).Value
      j = j + 1
    End If
  Next
End Sub

Without loop

VBA Code:
Sub Macro2()
  Range("C2", Range("C" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, 23).Copy Range("E2")
End Sub

and with the array
VBA Code:
Sub macro3()
  Dim a As Variant
  Dim i As Long, j As Long
  Dim data As Worksheet
  
  Set data = ActiveSheet
  a = data.Range("C2", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) <> "" Then
      j = j + 1
      b(j, 1) = a(i, 1)
    End If
  Next
  data.Range("E2").Resize(j).Value = b
End Sub
 
Last edited:
Upvote 0
How about
Excel Formula:
Dim a As Variant, b As Variant

a = Data.Range("C2:C" & LR3).Value2
ReDim b(1 To UBound(a), 1 To 1)
Dim j As Long, jj As Long: jj = 1
For j = 1 To UBound(a)
If a(j, 1) <> "" Then
b(jj, 1) = a(j, 1)
jj = jj + 1
End If
Next j

 Data.Range("E2").Resize(jj).Value2 = b
Thank you so much it worked well.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
With loop

VBA Code:
Sub removeempty()
  Dim i As Long, j As Long
  j = 2
  For i = 2 To Range("C" & Rows.Count).End(3).Row
    If Range("C" & i).Value <> "" Then
      Range("E" & j).Value = Range("C" & i).Value
      j = j + 1
    End If
  Next
End Sub

Without loop

VBA Code:
Sub Macro2()
  Range("C2", Range("C" & Rows.Count).End(3)).SpecialCells(xlCellTypeConstants, 23).Copy Range("E2")
End Sub

and with the array
VBA Code:
Sub macro3()
  Dim a As Variant
  Dim i As Long, j As Long
 
  a = Range("C2", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) <> "" Then
      j = j + 1
      b(j, 1) = a(i, 1)
    End If
  Next
  Range("E2").Resize(j).Value = b
End Sub
Thank you Dante. it works too but I prefer arrays since my data sets are large and looping through each cell takes longer. Once again thank you for great help as always. 🙏
 
Upvote 0

Forum statistics

Threads
1,215,237
Messages
6,123,800
Members
449,127
Latest member
Cyko

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