VBA帮助select最后一行和select范围
以下代码正在执行以下操作:
- 从A1开始,它search最后一个填充的单元格,然后select它后面的单元格。
- select第一个可用空白单元格后,将select整个行
- 整行然后用颜色索引16着色。
干得好:
Sub Macro1() Range("A1").End(xlDown).Offset(1, 0).Select Range(Selection, Selection.End(xlToRight)).Select With Selection.Interior .ColorIndex = 16 End With End Sub
这工作很好,但我很难在该行内select列A到H. 我不想select整个行。
有人可以帮帮我吗? 我不断收到错误,不知道如何解决它:S
非常感谢您的时间和考虑!
这是太多的select! 怎么样
Sub Macro1() currentRow = Range("A1").end(xldown).offset(1,0).row Range(cells(currentRow, 1), cells(currentRow,8)).Interior.ColorIndex = 16 cells(currentRow,1)= " " End Sub
那样有用吗?
你可以为你的select设置一个范围,这里也是从列的末尾查找最后一行的方法(版本1) – (Rows.Count,1)表示列A(Rows.Count,2)将表示列B等等…
这是第一个版本:从列A末尾开始寻找空行:
Dim rowNum As Integer rowNum = Cells(Rows.Count, 1).End(xlUp).Row + 1 Dim rng As Range Set rng = Range(Cells(rowNum, 1), Cells(rowNum, 8)) rng.Interior.ColorIndex = 16
这是版本二:从列A的开始(第一行)开始寻找空行:
Dim rowNum As Integer rowNum = Range("A1").End(xlDown).Offset(1, 0).Row Dim rng As Range Set rng = Range(Cells(rowNum, 1), Cells(rowNum, 8)) rng.Interior.ColorIndex = 16
尝试这个:
'Declare Variables Dim rowNum as integer Dim YourRange as range 'Find last row by counting cells not empty and adding 1 'Assumes that all cells from A1 down are filled. Change the +1 accordingly if not rowNum = worksheetfunction.counta(activesheet.columns(1))+1 'Set range object to column A through H of your row number Set YourRange = Activesheet.Range("A" & rowNum & ":H" & rowNum) 'Set interior color of the rnage object instead of using Selection YourRange.interior.colorindex=16