vba 查找区域中填充数据的最后一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20812827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding The Last Column With Filled with Data in a Range
提问by Behseini
I know we can get the last row with data with following code:
我知道我们可以使用以下代码获取包含数据的最后一行:
LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
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
但是我在获取带有数据的最后一列时遇到了麻烦。这是我尝试过的 bu,正如您从未通过的图像中看到的那样
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
Can you please let me know hoe I can fix this? thanks
你能告诉我我能解决这个问题吗?谢谢
回答by L42
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.
我不确定它是xlLeft
还是xlToLeft
。自己试试吧。
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.
这种颜色A5
到你的最后一列和行。