print out last non blank column

gint32

Board Regular
Joined
Oct 8, 2014
Messages
139
Hi Everyone, can anybody throw some light on the below code,
I am trying to get the vba to msgbox out the contents of each cell headers which are in the first row and then stop, at the moment it runs on to the next rows msgboxing those cell contents also ...many thanks in advance

VBA Code:
Sub PrintToImmediateWindowEach()

 Dim example As Range
 Dim last_col As Variant
 
     last_col = Cells(1, Columns.Count).End(xlToLeft).Column
     last_col = Split(Cells(1, last_col).Address, "$")(1)
 
Set example = Range("A:" & last_col)
 
   For Each example In Range("A:" & last_col)
  
        MsgBox example
       
    Next
   
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi
Try to change to
VBA Code:
Set example = Range(last_col & 1)
 
Upvote 0
Sorry
TRy
VBA Code:
Sub PrintToImmediateWindowEach()

 Dim example As Range
 Dim last_col As Variant
 
     last_colc = Cells(1, Columns.Count).End(xlToLeft).Column
    
 
For i = 1 To last_colc

MsgBox Cells(1, i)
Next

   
End Sub
 
Upvote 0
Another option
VBA Code:
Sub PrintToImmediateWindowEach()

 Dim example As Range
 Dim last_col As Long
 
     last_col = Cells(1, Columns.Count).End(xlToLeft).Column
 
 
   For Each example In Range("A1").Resize(, last_col)
  
        MsgBox example
       
    Next
   
End Sub
 
Upvote 0
Solution
Thanks very much for everyones help as either one of these solutions is a great sort for me..have a great xmas
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,682
Messages
6,126,195
Members
449,298
Latest member
Jest

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