Excel精英培训网

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

[已解决]关于区域设置问题

[复制链接]
发表于 2014-6-16 02:12 | 显示全部楼层 |阅读模式
关于区域设置问题

Sub test()
    Dim rng As Range
    On Error Resume Next
    Set rng = Application.InputBox("请选择", "提示", , , , , Type:=8)
    If rng Is Nothing Then Exit Sub

'假如我选择的是B5,当前工作表中,最后一个最非空单元格地址是U60
'那么就将B5:U60这个区域自动列宽,字体排列水平居中,字号12号。
'请朋友们把我完善下这个代码,最后一个非空单元格地址不确定(用代码来判断)。

End Sub

谢谢大家了!

最佳答案
2014-6-16 11:33
  1. Sub Macro1()
  2. Dim rng As Range
  3.     On Error Resume Next
  4.     Set rng = Application.InputBox("请选择", "提示", , , , , Type:=8)
  5.     If rng Is Nothing Then Exit Sub
  6.     Set c1 = Cells.Find("*", , , , xlByRows, xlPrevious)
  7.     Set c2 = Cells.Find("*", , , , xlByColumns, xlPrevious)
  8.     With Range(rng, Cells(c1.Row, c2.Column))
  9.         .Font.Size = 12
  10.         .HorizontalAlignment = xlCenter
  11.         .Columns.AutoFit
  12.     End With
  13. End Sub
复制代码
发表于 2014-6-16 04:57 | 显示全部楼层
  1. Sub Macro1()
  2. Dim rng As Range
  3.     On Error Resume Next
  4.     Set rng = Application.InputBox("请选择", "提示", , , , , Type:=8)
  5.     If rng Is Nothing Then Exit Sub
  6.     With Range(rng, [u65536].End(xlUp))
  7.         .Font.Size = 12
  8.         .HorizontalAlignment = xlCenter
  9.         .Columns.AutoFit
  10.     End With
  11. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2014-6-16 08:14 | 显示全部楼层
dsmch 发表于 2014-6-16 04:57

谢谢老师的帮助,这么早就帮助咱们了!
是我要的结果,就是有一点,就是,我最后一个非空单元格,不一定在U列,也就是说,最后一个单元格位置不确定在某列某行

请老师,再帮帮,谢谢了!
回复

使用道具 举报

发表于 2014-6-16 11:33 | 显示全部楼层    本楼为最佳答案   
  1. Sub Macro1()
  2. Dim rng As Range
  3.     On Error Resume Next
  4.     Set rng = Application.InputBox("请选择", "提示", , , , , Type:=8)
  5.     If rng Is Nothing Then Exit Sub
  6.     Set c1 = Cells.Find("*", , , , xlByRows, xlPrevious)
  7.     Set c2 = Cells.Find("*", , , , xlByColumns, xlPrevious)
  8.     With Range(rng, Cells(c1.Row, c2.Column))
  9.         .Font.Size = 12
  10.         .HorizontalAlignment = xlCenter
  11.         .Columns.AutoFit
  12.     End With
  13. End Sub
复制代码

评分

参与人数 1 +9 收起 理由
yjwdjfqb + 9 赞一个!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-6-16 11:40 | 显示全部楼层
dsmch 发表于 2014-6-16 11:33

就是这样的,但在有的文件中,代码能达到效果

在这个文件中,代码达不到效果。

请老师帮帮看看,再次谢谢了!
(如选择A2单元格)

测试.rar (8.31 KB, 下载次数: 5)

点评

没测试出问题  发表于 2014-6-16 21:04
回复

使用道具 举报

 楼主| 发表于 2014-6-16 22:01 | 显示全部楼层
yjwdjfqb 发表于 2014-6-16 11:40
就是这样的,但在有的文件中,代码能达到效果

在这个文件中,代码达不到效果。

那可能是我操作问题,我再看看!
回复

使用道具 举报

 楼主| 发表于 2014-6-17 18:17 | 显示全部楼层
dsmch 发表于 2014-6-16 11:33

请老师帮帮我看看,为什么,不能在自动列宽的基础上加10

Sub test()
Dim rng As Range
On Error Resume Next
Set rng = Application.InputBox("请选择", "提示", , , , , Type:=8)
If rng Is Nothing Then Exit Sub

Set rg = rng.CurrentRegion '当前区域

With Range(rng, rg(rg.Count)) '选择的单元格到当前有数据的区域最后一个单元格
.Font.Size = 11 '字号
.HorizontalAlignment = xlCenter '居中对齐


.Rows.AutoFit '自动行高
.RowHeight = .RowHeight + 10 '行高的基础上加10



.Columns.AutoFit '自动列宽


'##############问题,为什么,自动行高,加10后,可以实现
'###############但,自动列宽加10行。
.ColumnsHeight = .ColumnsHeight + 10 '列宽的基础上加10



End With

End Sub

用这个代码,操作后,列的宽度,还是,自动列宽的宽度,
没有达到想要的,列宽的宽度,为自动列宽的基础上加10
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 12:49 , Processed in 0.294878 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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