1

I know we can get the last row with data with following code:

LastRow = .Range("D" & .Rows.Count).End(xlUp).Row

But I am having trouble on getting the last Column with data. Here is what i tried bu as you can see from the image it didn't go through

Set ws = ThisWorkbook.ActiveSheet
With ws 
 Header = 5
 LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
 LastCol = .Range(5 & .ColumnCount).End(xlLeft).Column
    With .Range("A" & Header & LastCol & LastRow)
         .Interior.ColorIndex = 16
    End With
End With

enter image description here

Can you please let me know hoe I can fix this? thanks

3
  • Use Cells instead of Range. Commented Dec 28, 2013 at 9:20
  • 1
    Use Columns.Count instead of Column.Count Commented Dec 28, 2013 at 9:24
  • Hi Tony, Thanks for comment I fix the mistake you said but still not working Commented Dec 28, 2013 at 9:37

1 Answer 1

4

Try this as i've commented:

Lastcol = .Cells(5, Columns.Count).End(xlToLeft).Column

i'm not sure if its xlLeft or xlToLeft. Try it yourself.

Use this to color the entire range:

With .Range(Cells(1,5),Cells(Lastrow,Lastcol)
    .Interior.ColorIndex = 16
End With

this colors A5 to your last column and row.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks L42, but seems there is still something missing! I am not getting any errors but after running the code nothing change!
What row do you want to get the last column and what do you want your code to accomplish? You want to change color of what range?
I was thinking to change the color of wole used range!
I mean already used something like this With .Range("A" & Header & ":C" & LastRow) .Interior.ColorIndex = 16 End With
Great , thanks it works now, what a big difference! I still do not know why you chosed Cells(1,5) why not Cells(1,1)? was there any reason?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.