Automatically select the last column with data - 2 Back columns

sofas

Active Member
Joined
Sep 11, 2022
Messages
493
Office Version
  1. 2019
Platform
  1. Windows
Hello...Please correct the code, I have a datasheet whose number of columns is not fixed, it could be 3, 5, or maybe 10, I want to select the last column always - 2 columns back so that I can execute the following code

VBA Code:
Sub Copy_n_Paste()
Dim wsDest As Worksheet: Set wsDest = sheet3

Dim lr As Long, Lastcol As Long

Dim SrchRng As Range, cel As Range

lr = wsDest.Cells(Rows.Count, "a").End(xlUp).Row - 1

Lastcol = wsDest.Cells(5, wsDest.Columns.Count).End(xlToLeft).Column
  
Set SrchRng = wsDest.Cells(5, Lastcol - 2 & lr)
For Each cel In SrchRng
    If cel.Value <> "" Then
    cel.Offset(1, 1).Value = cel.Value
    cel.Offset(1, 1).NumberFormat = "###0.00"
    
    
    End If
Next cel
        

End Sub
 

Attachments

  • tr.PNG
    tr.PNG
    15.7 KB · Views: 4

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
As a bit of a guess what you want...
VBA Code:
Set SrchRng = wsDest.Range(wsDest.Cells(5, Lastcol - 2), wsDest.Cells(lr, Lastcol - 2))
 
Upvote 0
Solution
As a bit of a guess what you want...
VBA Code:
Set SrchRng = wsDest.Range(wsDest.Cells(5, Lastcol - 2), wsDest.Cells(lr, Lastcol - 2))
Very cool. Thank you, this is what I was looking for.

Can I find a faster code than it that works, but it's rather slow?

VBA Code:
Sub Copy_n_Paste()
Dim wsDest As Worksheet: Set wsDest = sheet3

Dim lr As Long, Lastcol As Long

Dim SrchRng As Range, cel As Range

lr = wsDest.Cells(Rows.Count, "a").End(xlUp).Row - 1

Lastcol = wsDest.Cells(5, wsDest.Columns.Count).End(xlToLeft).Column
Set SrchRng = wsDest.Range(wsDest.Cells(6, Lastcol - 2), wsDest.Cells(lr, Lastcol - 2))
For Each cel In SrchRng
    If cel.Value <> "" Then
    cel.Offset(1, 1).Value = cel.Value
    cel.Offset(1, 1).NumberFormat = "###0.00"
    'cel.Offset(1, 1).NumberFormat = "#,##0.00"
    cel.Offset(1, 1).Font.Bold = True
    cel.Offset(1, 1).Font.Color = vbRed
    cel.Offset(1, 1).EntireColumn.AutoFit
    
    End If
Next cel
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,847
Messages
6,127,270
Members
449,372
Latest member
charlottedv

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