VBA code for indeterminate ranges containing blank rows

jardenp

Active Member
Joined
May 12, 2009
Messages
344
I have "entries" that consist of a row of data and then a blank row below. The first entry is in A2:K3. The number of entries is indeterminate. I have formatting (borders and such) that I want to copy from the first to all the other entries--A4:K5, A6:K7, etc.

My modified code from the macro recorder is:
Code:
'Copy Box Format to Remainder of Entries
    Range("A2:K3").Select
        Selection.Copy
    Range("A2:K43").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
I normally would use something along the lines
Code:
Range("A2:K" & Range("A2").End(xlDown).Row).Select
to replace
Code:
Range("A2:K43").Select
but I get an error. I suspect because the data cells in column A aren't contiguous.

Any thoughts?
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,453
Try using xlUp instead of down. (and you don't need to select the range to work with it.) ie.
Code:
Range("A2:K3").Copy
Range("A2:K" & Cells(Rows.Count, "A").End(xlUp).Row).PasteSpecial Paste:=xlFormats, Operation:=xlNone, _
  SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Does that help?

BTW, your original code (the xlDown) did not give me an error, it just selected rows 2:4, which is the expected result for your data layout. (but I suspect not what you're looking to do.)
 

jardenp

Active Member
Joined
May 12, 2009
Messages
344
I apologize: I misspoke (or mistyped?). I didn't mean "error," just that it didn't do what I wanted.

Your code works great. Thank you.

The only problem is that the bottom border on the last entry doesn't transfer. It's not really a "problem," but would you know how to fix that?

The formatting I applied earlier in the code is:
Code:
'Format Line Boxes
    Range("A2:K3").Select
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    
    'Underline Data
    Range("A2:J2").Select
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
Thanks! (And if you're an Alaskan who fishes, I'm jealous!)
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,565
Messages
5,512,090
Members
408,879
Latest member
dilipsny

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top