Macro VBA - ClearContents problem

guscrouchend

Board Regular
Joined
May 11, 2010
Messages
94
Hi,

I'm working with the following:

Sub SendtoInvoice()

With Worksheets("Invoices")

.Range("$A$18:$I" & Cells(Rows.Count, "I").End(xlUp).Row).ClearContents

End With

With Worksheets("Combined Accounts")

.Range("A1:I" & .Cells(Rows.Count, 1).End(xlUp).Row).Copy Worksheets("Invoices").Range("A18")

End With

End Sub

My problems is that the macro :confused:sometimes:confused: clears the contents of all cells in the Invoices worksheet, rather than only the cells from line 18 onwards.

Is there a way to change the clear contents line so that it only ever clears rows 18+?

Thanks

Angus
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Dim LR As Long
With Worksheets("Invoices")
    LR = WorksheetFunction.Max(.Cells(Rows.Count, "I").End(xlUp).Row, 18)
    .Range("$A$18:$I" & LR).ClearContents
End With
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
I should think Peter's would work quite well; just a possible alternative:
Rich (BB code):
    With Worksheets("Invoices")
        If .Cells(.Rows.Count, "I").End(xlUp).Row >= 18 Then
            .Range("A18:I" & .Cells(.Rows.Count, "I").End(xlUp).Row).ClearContents
        End If
    End With
Not tested, so in a junk copy of your workbook. I would personally qualfy .Cells as belonging to the sheet, lest another sheet is active and things go KABOOM!. I haven't worked in 2007/2010, but have read implications that unqualified Rows.Count can likewise cause issues. Not sure about that, but the qualification certainly cannot hurt.

@VoG

Hi Peter,

I see that you are now using 2010 at home. Have you by chance ever tested an unqualified Rows.Count at home, on a wb written at work?

Mark
 
Last edited:

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Hi Mark

No I haven't had an issue with unqualified Rows.Count.

With the OP's code I thought that it could clear the wrong range if Cells was not qualified and/or column I was filled to less than Row 18.
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
With the OP's code I thought that it could clear the wrong range if Cells was not qualified and/or column I was filled to less than Row 18.
Yes sir, I utterly agree

No I haven't had an issue with unqualified Rows.Count.
If you happen to take a wb home (written in 2000 at work) and find this glitch, would you let me know? A description is here at post#1:

http://vbaexpress.com/forum/showthread.php?t=18978

Thank you Peter, and of course, a great day to your and yours,

Mark
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,747
Messages
5,513,153
Members
408,939
Latest member
jessica116

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