从B5,C5,D5开始....我有一个excel文件,如下所示:
请注意,我在前3行有一些标题和标题文本,所以我需要使用Macro来根据行的标题设置从第5行到结尾的每列的类型(标题仅用于提及所需的标题)你能告诉我如何在Excel VBA中做到这一点吗?
由于
答案 0 :(得分:1)
这是你在尝试什么?我正在展示Column B
的示例。做其余的事。
逻辑:
<强>代码强>:
Private Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, Header As Long
Header = 5 '<~~ Start row for formatting
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row
With .Range("B" & Header & ":B" & LastRow)
'
'~~> Change format here
'
'~~> Number with 5 decimal places.
.NumberFormat = "0.00000"
End With
End With
End Sub
从评论中跟进
谢谢,但这只是格式化B5单元格,你能不能让我知道我怎样才能完成从5到1000的其他行--Behseini 11秒前
哦,所以如果第5行之后没有值,并且你想硬编码最后一行,那么使用这段代码
Private Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long, Header As Long
Header = 5 '<~~ Start row for formatting
LastRow = 1000 '<~~ Last Row
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
With .Range("B" & Header & ":B" & LastRow)
'
'~~> Change format here
'
'~~> Number with 5 decimal places.
.NumberFormat = "0.00000"
End With
End With
End Sub