vba 如果值已输入到同一行的另一个单元格中,则 Excel 宏可更改单元格对齐方式、边框和换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25308905/
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
Excel Macro to change cell alignment, borders and wrap text if a value has been entered into another cell in the same row
提问by user3934597
I am trying to create an excel macro which updates the cell alignment, wrap text and borders of cells in each row if a value is entered into one cell within that specific row. For example, if a value is entered into cell A1, then I want the macro to update the wrap text, cell alignment and borders of cells A1:O1. Unfortunately, applying conditional formating to each row within the spreadsheet is rather cumbersome and only will handle updating the cell borders, so I think that a macro that can update all 3 cell formatting elements and dynamically searches the entire worksheet, would be best.
我正在尝试创建一个 excel 宏,如果将值输入到该特定行中的一个单元格中,它会更新单元格对齐方式、换行文本和每行单元格的边框。例如,如果在单元格 A1 中输入一个值,那么我希望宏更新单元格 A1:O1 的换行文本、单元格对齐方式和边框。不幸的是,对电子表格中的每一行应用条件格式相当麻烦,并且只能处理更新单元格边框,所以我认为可以更新所有 3 个单元格格式元素并动态搜索整个工作表的宏将是最好的。
Thanks for your assistance!
谢谢你的协助!
采纳答案by brainac
I don't know how you want to trigger this macro, nor what exact formatting you want to apply, but here's what I would do:
我不知道你想如何触发这个宏,也不知道你想应用什么确切的格式,但我会这样做:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
With Range(Target, Target.Offset(0,14)
.HorizontalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End with
end if
End Sub
Edit:Add button and Assign Macro window should appear. Select New and put the code there.
编辑:应该出现添加按钮和分配宏窗口。选择新建并将代码放在那里。
For Each Target in Range(Cells(1,1), Cells(65536, 1).End(xlUp))
If Target <> "" Then
With Range(Target, Target.Offset(0,14)
.HorizontalAlignment = xlCenter
.WrapText = True
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
End If
Next