Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Select range regardless of row count

Posted on 2016-09-02
4
Medium Priority
?
80 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 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 22

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 25

Author Closing Comment

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

Expert Comment

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

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
New style of hardware planning for Microsoft Exchange server.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

721 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