Solved

Select range regardless of row count

Posted on 2016-09-02
4
60 Views
Last Modified: 2016-09-06
I have a macro that was originally recorded. It creates a Comment column. Then adds bottom borders to the cells in that column. It works. But, when the row count change, e.g. gets less, I end up with 'extra' cells bottom borders.

It seems the issue starts at the Range("B2:B10").Select line.

Would you please adjust the code to add borders only on rows that have values in the ColA column? I've included the sample worksheet and code.

Sub x()
'
' Add_Comment_Col Macro
'
'
   Range("A1").Select
    Selection.End(xlToRight).Select
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Comment"
    Range("B2").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("B2").Select
    Selection.Copy
    Range("A3").Select
    Selection.End(xlDown).Select
    ' Move one cell right
    ActiveCell.Offset(0, 1).Select
    Range(Selection, Selection.End(xlUp)).Select
    Range("B2:B10").Select
    Range("B10").Activate
    ActiveSheet.Paste
    Range("A1").Select
End Sub

Open in new window

CopyTest.xlsx
0
Comment
Question by:NVIT
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41782537
Try something like this......
Sub x()
Dim LastRow As Long, NextCol As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
NextCol = Cells(1, Columns.Count).End(xlToLeft).Column + 1
If LastRow < 2 Then Exit Sub
Cells(1, NextCol) = "Comment"
Cells(2, NextCol).Borders(xlEdgeBottom).LineStyle = xlContinuous
Cells(2, NextCol).Copy
Range(Cells(2, NextCol), Cells(LastRow, NextCol)).PasteSpecial xlPasteAll
Cells(1, NextCol).Select
Application.CutCopyMode = 0
End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41782560
If you use a Table then Formatting is automatic, no need for a macro.

Introduction to Excel Tables
0
 
LVL 24

Author Closing Comment

by:NVIT
ID: 41786663
Thanks, Subohd! It works!
0
 
LVL 29

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41786664
You're welcome. Glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question