Find last column with value ignoring formulas

Krusty1

New Member
Joined
Dec 29, 2019
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Have learnt a lot from these forums, now my first time asking for help....

In the spreadsheet I'm building, I want to be able to find the last column of data and copy-paste values/formats overwriting existing formulas in that specific column on the Gap Tracker tab so the source data on the Gap Data tab can be replaced in the following week to fill in the next column.

The columns start at O and finish at AT on the Gap Tracker tab.

This is one of the more successful codes I have tried thus far however, it still responds to formulas even when 0 values are written as "".
I want the code to find the numerical value including 0, not the ongoing cells that are prepopulated with formulas.

Any help would be sincerely appreciated.
Thank you.

VBA Code:
Sub FindLastColumn()

Dim sht As Worksheet
Dim LastColumn As Long

Set sht = ThisWorkbook.Worksheets("Gap Tracker")

  LastColumn = sht.Range("A2").End(xlToRight).Column
  sht.Columns(LastColumn).Copy
  sht.Columns(LastColumn).PasteSpecial Paste:=xlPasteValues
  sht.Columns(LastColumn).PasteSpecial Paste:=xlPasteFormats
  Application.CutCopyMode = False
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the board!

Try this

VBA Code:
Sub FindLastColumn()
  Dim sht As Worksheet
  Dim LastColumn As Long, i As Long
  
  Set sht = ThisWorkbook.Worksheets("Gap Tracker")
  For i = sht.Range("A2").End(xlToRight).Column To 1 Step -1
    If sht.Cells(2, i).Value <> "" Then
      LastColumn = i
      Exit For
    End If
  Next
  If LastColumn = 0 Then LastColumn = 1
  sht.Columns(LastColumn).Copy
  sht.Columns(LastColumn).PasteSpecial Paste:=xlPasteValues
  sht.Columns(LastColumn).PasteSpecial Paste:=xlPasteFormats
  Application.CutCopyMode = False
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

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