Last Blank cell

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
The code is all perfect but it is not applying the formula till the end, not sure what is wrong.

this line from the code is not picking the lastnonblank cell dut to this the formual is not applied till last
VBA Code:
lastNonBlankRow = destinationRange.Columns(destinationColumn).Cells(destinationRange.Rows.Count).End(xlUp).Row

VBA Code:
    Dim lastRowM As Long
    Dim ws As Worksheet
    Dim currentWorkbook As Workbook
    Dim destinationColumn As Integer
    Dim destinationRange As Range
    Dim lastNonBlankRow As Long
   
    Set currentWorkbook = ThisWorkbook
    
    destinationColumn = 1
    
    Set destinationRange = currentWorkbook.Sheets("ALPCal Final").Cells(1, destinationColumn)
    
    currentWorkbook.Sheets("ALPCal Final").Range("A1:BA509728").Clear

currentWorkbook.Sheets("ALPCal Final").Select
Application.Wait (Now + TimeValue("0:00:02"))


Range("A1").Select

For Each ws In currentWorkbook.Sheets
        If Left(ws.Name, 3) = "ALp" Then
            ' Find the last row in the ALp sheet
            lastRowM = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row

            ' Copy data from ALp sheet to the "ALPCal Final" sheet
            ws.Range("C1:C" & lastRowM).Copy destinationRange.Cells(1, destinationColumn)

            ' Remove duplicates in the destination column
            destinationRange.Columns(destinationColumn).RemoveDuplicates Columns:=1, header:=xlYes
            
            'find the lastnonblank cell from current destinationColumn
            lastNonBlankRow = destinationRange.Columns(destinationColumn).Cells(destinationRange.Rows.Count).End(xlUp).Row
            
            ' Apply the COUNTIF formula to count occurrences up to the last non-blank row in the current column
            With destinationRange.Offset(1, destinationColumn).Resize(IIf(lastNonBlankRow > 1, lastNonBlankRow - 1, 1), 1)
            .Formula = "=COUNTIF('" & ws.Name & "'!C:C, A2)"
            .Value = .Value
            End With
            
           ' Move to the next column for the next ALp sheet
            destinationColumn = destinationColumn + 3
            destinationRange.Columns(destinationColumn).Select
        End If
        lastRowM = 0
        lastNonBlankRow = 0
        
    Next ws

    ' Clear the clipboard
    Application.CutCopyMode = False
End Sub

ALP Auotmation Weekly - V1.xlsm
ABCDEFGH
1Speed_charSpeed_charSpeed_char
230016810530016958230020839
3100100100
4500500500
5200200200
6 1.02400000000000E 00310 1.02400000000000E 003
720410
810 1.02400000000000E 0032
9405040
10504050
11808020
12252080
13224
1442525
15302.50E-011
163502.50E-01
1715.00E-01
ALPCal Final
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Like this:

VBA Code:
lastNonBlankRow = destinationRange.Columns(destinationColumn).Cells(currentWorkbook.Sheets("ALPCal Final").Rows.Count).End(xlUp).Row
 
Upvote 0
Like this:

VBA Code:
lastNonBlankRow = destinationRange.Columns(destinationColumn).Cells(currentWorkbook.Sheets("ALPCal Final").Rows.Count).End(xlUp).Row
Tryied this but same result no luck
 
Upvote 0
I suspect there is a flaw in how you are using the column number. Try this code:
VBA Code:
Sub Examples()
    Dim destinationColumn As LongPtr
    Dim destinationRange As Range
    
    destinationColumn = 2
    MsgBox Worksheets("Sheet1").Columns(destinationColumn).Address
    Set destinationRange = Worksheets("Sheet1").Columns(destinationColumn)
    MsgBox destinationRange.Columns(destinationColumn).Address
End Sub
 
Upvote 0
I suspect there is a flaw in how you are using the column number. Try this code:
VBA Code:
Sub Examples()
    Dim destinationColumn As LongPtr
    Dim destinationRange As Range
   
    destinationColumn = 2
    MsgBox Worksheets("Sheet1").Columns(destinationColumn).Address
    Set destinationRange = Worksheets("Sheet1").Columns(destinationColumn)
    MsgBox destinationRange.Columns(destinationColumn).Address
End Sub

I modified this part .End(xlUp) to .End(xlDown)
VBA Code:
lastNonBlankRow = destinationRange.Columns(destinationColumn).Cells(destinationRange.Rows.Count).End(xlUp).Row

this is working now
 
Upvote 0
Solution
destinationRange was 1 cell (A1), adding .Columns to it still leaves it as 1 cell.
rows.count of 1 cell is 1 row so it is still A1.
xlup on A1 has nowhere to go.

Something like this would work:
VBA Code:
    With currentWorkbook.Sheets("ALPCal Final")
        lastnonblankrow = .Cells(.Rows.Count, destinationColumn).End(xlUp).Row
    End With
 
Upvote 0
@Alex Blakenburg but yours is different from his, as he is working relative to the destinationRange and you are working relative to cell A1.

Is it though ? Is this not A1 ? 😉

VBA Code:
destinationColumn = 1 
Set destinationRange = currentWorkbook.Sheets("ALPCal Final").Cells(1, destinationColumn)

And being a single cell the following returns just that 1 cell (A1)

VBA Code:
destinationRange.Columns(destinationColumn).Cells(destinationRange.Rows.Count)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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