Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2114|回复: 7

[已解决]恳请高手看下这个难题用vba怎么解决,谢谢

[复制链接]
发表于 2011-11-5 17:23 | 显示全部楼层 |阅读模式
我附件上有一个表,我想实现的功能是当表格中的f列为非空的时候,则对应的a列到n列有边框。例如f1上有数据的时候,则a1到ni有边框,谢谢高手的帮忙。
最佳答案
2011-11-5 18:36
本帖最后由 zjdh 于 2011-11-5 18:42 编辑

不要这样复杂吧:
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.   Cells.Borders.LineStyle = 0
  3.   For I = 1 To Range("F65536").End(3).Row
  4.         If Range("F" & I) <> "" Then Range("A" & I & ":N" & I).Borders.LineStyle = 1
  5.   Next
  6. End Sub
复制代码
边框线1.rar (12.45 KB, 下载次数: 9)

2.rar

9.29 KB, 下载次数: 6

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-11-5 18:14 | 显示全部楼层
本帖最后由 BAIXINGR 于 2011-11-5 18:17 编辑

Sub Macro2()
Dim x, y, z As Integer
x = 1
y = Range("f65536").End(xlUp).Row
For z = 1 To y
If Cells(x, 6) <> "" Then
    Range(Cells(x, 1), Cells(x, 14)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    x = x + 1
    Else
    Range(Cells(x, 1), Cells(x, 14)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeTop).LineStyle = xlNone
    Selection.Borders(xlEdgeBottom).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
    x = x + 1
    End If
   
  
    Next z
End Sub
回复

使用道具 举报

发表于 2011-11-5 18:36 | 显示全部楼层    本楼为最佳答案   
本帖最后由 zjdh 于 2011-11-5 18:42 编辑

不要这样复杂吧:
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.   Cells.Borders.LineStyle = 0
  3.   For I = 1 To Range("F65536").End(3).Row
  4.         If Range("F" & I) <> "" Then Range("A" & I & ":N" & I).Borders.LineStyle = 1
  5.   Next
  6. End Sub
复制代码
边框线1.rar (12.45 KB, 下载次数: 9)
回复

使用道具 举报

发表于 2011-11-5 18:38 | 显示全部楼层
其实可以用数据有效性自动添加更方便:
边框线.rar (12.96 KB, 下载次数: 17)
回复

使用道具 举报

发表于 2011-11-5 19:22 | 显示全部楼层
还是用zjdh 的方法简单些。
回复

使用道具 举报

发表于 2011-11-5 19:39 | 显示全部楼层
  真简单                           
回复

使用道具 举报

发表于 2011-11-5 21:14 | 显示全部楼层
zjdh 发表于 2011-11-5 18:38
其实可以用数据有效性自动添加更方便:

请问怎么设置的数据有效性呀
回复

使用道具 举报

发表于 2011-11-6 07:44 | 显示全部楼层

评分

参与人数 1 +3 收起 理由
Select + 3 看到4楼说数据有效性,呵呵

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 06:23 , Processed in 0.386423 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表