How to get the Last Column number which have data within a specific range ?

Pramodpandit123

New Member
Joined
Apr 18, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
I wanted to get Multiple Rows & Columns Data and stack it into single column in Excel.
Below is the code i used :
VBA Code:
Sub SingleColumn()
r = 3
lastrow = Cells(Rows.Count, "D").End(xlUp).Row
For i = 1 To lastrow
    lastcol = Cells(i + 2, Columns.Count).End(xlToLeft).Column 

    For j = 1 To lastcol
        If Cells(i + 2, j + 3).Value <> "" Then
        Cells(i + 2, j + 3).Copy Range("M" & r)
        r = r + 1
        Else
        Exit For
        End If
    Next j
Next i
End Sub
The problem is lastcol give the last cell having the data whereas i want the number of datas within the specific range/Column.
For eg: My datas are within Column D to Column F only. Beyound column F there are unrelated datas.
So i wanted to get number of columns having data within Column D to Column F only.
Capture.JPG
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try replacing your line with
VBA Code:
lastcol = Application.WorksheetFunction.CountA(Cells(i + 2, "D").Resize(1, 3))
 
Upvote 0
VBA Code:
lc = Range("D3").CurrentRegion.Columns.Count

By the way, using array should help code faster:
VBA Code:
Option Explicit
Sub SingleColumn()
Dim i&, j&, k&, rng, arr(1 To 10000, 1 To 1)
rng = Range("D3").CurrentRegion.Value
For i = 1 To UBound(rng)
    For j = 1 To UBound(rng, 2)
        If rng(i, j) <> "" Then
            k = k + 1
            arr(k, 1) = rng(i, j)
        End If
    Next
Next
Range("M3").Resize(k, 1).Value = arr
End Sub
 
Upvote 0
Solution
Wow ! The array seems to work smoothly.But my datas are within Column D to Column F and it also have Headers above the data and at the side of the data as in the attched picture ! So the array code seems to pick the Headers as well and arrange it as shown in output. How could the headers be avoided in output through your code ?
 

Attachments

  • Capture.JPG
    Capture.JPG
    71.8 KB · Views: 22
Upvote 0
Actually, Range("D3").CurrentRegion refer to the current region that D3 is in (C2:F7). To refer to D3:F7 only (move down 1 row and right 1 column)
Change
VBA Code:
rng = Range("D3").CurrentRegion.Value

to
VBA Code:
rng = Range("D3").CurrentRegion.Offset(1, 1).Value
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
Members
452,302
Latest member
TaMere

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