VBA formatting to last non-blank cell

lzopatti

New Member
Joined
Aug 17, 2010
Messages
4
I'm trying to write a macro to format a header row, but don't know how to do so until the last non-blank cell (files may have different numbers of columns). Here's what I have, clearly I don't need to go up to column XFD. how do I stop at a non-blank cell? thanks!!!

Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("B7:XFD7").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,934
Welcome to the Board!

Try:

Code:
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With range("B7",cells(7,columns.count).end(xltoleft)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
Hope that helps.
 

lzopatti

New Member
Joined
Aug 17, 2010
Messages
4
thanks - that was quick!

another thing - I'm trying to fill a formula down to the last non-blank row... this is what I have so far (and it's ugly!):

Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("E125").Select
Selection.Copy
Range("D125").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("E1048576").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveWindow.ScrollRow = 1048548
ActiveWindow.ScrollRow = 1044444
ActiveWindow.ScrollRow = 1021873
ActiveWindow.ScrollRow = 976730
ActiveWindow.ScrollRow = 923379
ActiveWindow.ScrollRow = 870028
ActiveWindow.ScrollRow = 800262
ActiveWindow.ScrollRow = 740755
ActiveWindow.ScrollRow = 695612
ActiveWindow.ScrollRow = 654573
ActiveWindow.ScrollRow = 611482
ActiveWindow.ScrollRow = 545820
ActiveWindow.ScrollRow = 498625
ActiveWindow.ScrollRow = 447326
ActiveWindow.ScrollRow = 389872
ActiveWindow.ScrollRow = 342677
ActiveWindow.ScrollRow = 293430
ActiveWindow.ScrollRow = 248287
ActiveWindow.ScrollRow = 199040
ActiveWindow.ScrollRow = 176469
ActiveWindow.ScrollRow = 141586
ActiveWindow.ScrollRow = 131326
ActiveWindow.ScrollRow = 121066
ActiveWindow.ScrollRow = 100547
ActiveWindow.ScrollRow = 86183
ActiveWindow.ScrollRow = 67715
ActiveWindow.ScrollRow = 59508
ActiveWindow.ScrollRow = 47196
ActiveWindow.ScrollRow = 41040
ActiveWindow.ScrollRow = 30780
ActiveWindow.ScrollRow = 22572
ActiveWindow.ScrollRow = 20521
ActiveWindow.ScrollRow = 18469
ActiveWindow.ScrollRow = 16417
ActiveWindow.ScrollRow = 12313
ActiveWindow.ScrollRow = 10261
ActiveWindow.ScrollRow = 8209
ActiveWindow.ScrollRow = 6157
ActiveWindow.ScrollRow = 4105
ActiveWindow.ScrollRow = 2053
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollRow = 2053
ActiveWindow.ScrollRow = 4105
ActiveWindow.ScrollRow = 6157
ActiveWindow.ScrollRow = 4105
ActiveWindow.ScrollRow = 2053
ActiveWindow.ScrollRow = 1
Range("D17").Select
ActiveWindow.LargeScroll Down:=1
Range("D45").Select
ActiveWindow.LargeScroll Down:=1
Range("D74").Select
ActiveWindow.LargeScroll Down:=1
Range("D103").Select
ActiveWindow.LargeScroll Down:=1
Range("D132").Select
ActiveWindow.LargeScroll Down:=1
Range("D161").Select
ActiveWindow.LargeScroll Down:=1
Range("D190").Select
ActiveWindow.LargeScroll Down:=1
Range("D219").Select
ActiveWindow.LargeScroll Down:=-1
Range("D190").Select
ActiveWindow.LargeScroll Down:=-1
Range("D161").Select
ActiveWindow.LargeScroll Down:=-1
Range("D132").Select
ActiveWindow.LargeScroll Down:=-1
Range("D103").Select
ActiveWindow.LargeScroll Down:=-1
Range("D74").Select
ActiveWindow.LargeScroll Down:=-1
Range("D45").Select
ActiveWindow.LargeScroll Down:=-1
Range("H28").Select
Application.CutCopyMode = False
Selection.Copy
Range("G28").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range("H1048576").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Hi and welcome to the board!!!
There may be a better way for your 2nd question!!
What is the formula you want and where do you want it??
lenze
 

lzopatti

New Member
Joined
Aug 17, 2010
Messages
4
Thanks! The formula is:

=CONCATENATE(C8," ",D8)

And I want to fill it down column E, to the last non-blank in column D.
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Assuming you want to start in Row 8 or Column "E"
Rich (BB code):
Sub AddFormula()
Dim LR As Long
LR = Cells(Rows.Count, "D").End(xlUp).Row
With Range("$E$8:$E" & LR) 'Change this line if needed
      .Formula = "=CONCATENATE(C8,"" "",D8)"
End With
End Sub
BTW: You can add a line to change the formula to Values
Rich (BB code):
Sub AddFormula()
Dim LR As Long
LR = Cells(Rows.Count, "D").End(xlUp).Row
With Range("$E$8:$E" & LR) 'Change this line if needed
     .Formula = "=CONCATENATE(C8,"" "",D8)"
     .Value = .Value
End With
End Sub
 
Last edited:

Forum statistics

Threads
1,082,168
Messages
5,363,537
Members
400,747
Latest member
monty_gl

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top