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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:29:36  来源:igfitidea点击:

Finding The Last Column With Filled with Data in a Range

vbaexcel-vbaexcel

提问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

enter image description here

在此处输入图片说明

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 xlLeftor 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 A5to your last column and row.

这种颜色A5到你的最后一列和行。