我从这篇文章中复制了这段代码: stack post
Sub FindColumn()
Dim rngAddress As Range
Set rngAddress = Range("A1:BZ1").Find("STVCNTY CODE Mailing")
If rngAddress Is Nothing Then
MsgBox "STVCNTY CODE Mailing column was not found."
Exit Sub
End If
Range(rngAddress, rngAddress.End(xlDown)).Select
End Sub
我似乎工作除了一个问题,它停止了选择'当它遇到一个空单元格时。见图
如何修改此代码以选择整个列并将格式更改为000?
我已经完成这项工作已经认为我要找的专栏是静态的,但现在我告诉专栏的位置因报告而异,即星期一报告中的列AN,星期二列BA,等等
这是我已经拥有的简单代码:
' formats column to 000
Columns("AN:AN").Select
Selection.NumberFormat = "000"
任何帮助都会得到帮助
答案 0 :(得分:0)
试试这个。在列的底部进行处理(无需选择)。
Sub FindColumn()
Dim rngAddress As Range
Set rngAddress = Range("A1:BZ1").Find("STVCNTY CODE Mailing")
If rngAddress Is Nothing Then
MsgBox "STVCNTY CODE Mailing column was not found."
Exit Sub
End If
'just used range
Range(rngAddress, Cells(Rows.Count, rngAddress.Column).End(xlUp)).NumberFormat = "000"
'OR entire column
rngAddress.EntireColumn.NumberFormat = "000"
Range(rngAddress.Offset(1), rngAddress.Offset(199)).Formula = "=IFERROR(VLOOKUP(" & rngAddress.Offset(1, -1).Address(0, 0) & ",[CountyCodes2.xlsm]Sheet1!CountyTable,2,FALSE),"""")"
End Sub