Excel精英培训网

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

[已解决]自动识别数据源最后一列,我用cloumns.count不行

[复制链接]
发表于 2013-10-8 23:19 | 显示全部楼层 |阅读模式
5学分
Dim rng As Range
Dim STR As String
STR = InputBox("请输入最后一列的英文标识如M,G等")

   Set rng = Range("A3:" & STR & "7")

   MsgBox (rng.Address)
   rng.Select
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlColumnClustered
    MsgBox (rng.Address)
    ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:" & rng.Address)
    ActiveChart.Legend.Select
    ActiveChart.Legend.LegendEntries(3).Select
    ActiveChart.SeriesCollection(3).ChartType = xlLineStacked100
    ActiveChart.Legend.Select
    ActiveChart.Legend.LegendEntries(4).Select
    ActiveChart.SeriesCollection(3).Select
    ActiveChart.SeriesCollection(3).AxisGroup = 2
    ActiveChart.Legend.Select
    ActiveChart.Legend.LegendEntries(3).Select
    ActiveChart.SeriesCollection(4).ChartType = xlLine
    ActiveChart.Legend.Select
    ActiveChart.Legend.LegendEntries(3).Select
    ActiveChart.SeriesCollection(4).Select
    ActiveChart.SeriesCollection(4).AxisGroup = 2
   ActiveChart.ChartArea.Select

    ActiveChart.SeriesCollection(4).Select
    ActiveChart.SeriesCollection(4).ApplyDataLabels
    ActiveChart.SeriesCollection(4).Points(8).Select
    ActiveChart.SeriesCollection(4).Points(7).Select
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Points(7).Select
    ActiveChart.SeriesCollection(1).Points(7).ApplyDataLabels
    ActiveChart.SeriesCollection(3).Select
    ActiveChart.SeriesCollection(3).ApplyDataLabels
    Range("N36").Select
End Sub

我把 Set rng = Range("A3:" & STR & "7") 写成range(cells(3,1),cells(7,cloumns.count).end(xltoleft)不行,没把法才用了INPUTBOX,是什么原因呢?


最佳答案
2013-10-9 08:46
本帖最后由 sgxb123431380 于 2013-10-9 08:48 编辑

    Dim rng As Range
    Dim STR As String
   
    Set rng = Range(Cells(3, 1), Cells(7, Columns.Count).End(xlToLeft))
    MsgBox (rng.Address)
    rng.Select
   
    ActiveSheet.Shapes.AddChart.Select
    With ActiveChart
        .ChartType = xlColumnClustered
        MsgBox (rng.Address)
        .SetSourceData Source:=Range("Sheet1!$A$3:" & rng.Address)
        .SeriesCollection(3).ChartType = xlLineStacked100
        .SeriesCollection(3).AxisGroup = 2
        .SeriesCollection(4).ChartType = xlLine
        .SeriesCollection(4).AxisGroup = 2
        .SeriesCollection(4).ApplyDataLabels
        .SeriesCollection(1).Points(7).ApplyDataLabels
        .SeriesCollection(3).ApplyDataLabels
    End With
   
    Range("N36").Select

发表于 2013-10-8 23:32 | 显示全部楼层
Cells(3, Columns.Count).End(xlToLeft).Column   '取第三行的最后一列的列号

评分

参与人数 1 +1 收起 理由
棋行天下黄 + 1 谢谢帮助

查看全部评分

回复

使用道具 举报

发表于 2013-10-9 08:25 | 显示全部楼层
我把 Set rng = Range("A3:" & STR & "7") 写成range(cells(3,1),cells(7,cloumns.count).end(xltoleft)不行,没把法才用了INPUTBOX,是什么原因呢?

这种写法是可以的,你的问题是,最右边少了一个")", 并且单词写错了一个,把set 也去掉。
rng = Range(cells(3,1),cells(7, columns.count).end(xltoleft))

评分

参与人数 1 +1 收起 理由
棋行天下黄 + 1 谢谢帮助,实际运行错误

查看全部评分

回复

使用道具 举报

发表于 2013-10-9 08:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sgxb123431380 于 2013-10-9 08:48 编辑

    Dim rng As Range
    Dim STR As String
   
    Set rng = Range(Cells(3, 1), Cells(7, Columns.Count).End(xlToLeft))
    MsgBox (rng.Address)
    rng.Select
   
    ActiveSheet.Shapes.AddChart.Select
    With ActiveChart
        .ChartType = xlColumnClustered
        MsgBox (rng.Address)
        .SetSourceData Source:=Range("Sheet1!$A$3:" & rng.Address)
        .SeriesCollection(3).ChartType = xlLineStacked100
        .SeriesCollection(3).AxisGroup = 2
        .SeriesCollection(4).ChartType = xlLine
        .SeriesCollection(4).AxisGroup = 2
        .SeriesCollection(4).ApplyDataLabels
        .SeriesCollection(1).Points(7).ApplyDataLabels
        .SeriesCollection(3).ApplyDataLabels
    End With
   
    Range("N36").Select

评分

参与人数 1 +1 收起 理由
棋行天下黄 + 1 解决了错误,还精简了代码!

查看全部评分

回复

使用道具 举报

发表于 2013-10-9 16:51 | 显示全部楼层
打红色的地方错误(cloumns应为Columns):
rangecells(3,1),cells(7cloumns.count.endxltoleft应改为:
Set rng = Range(Cells(3, 1), Cells(7, Columns.Count).End(xlToLeft))

评分

参与人数 1 +1 收起 理由
棋行天下黄 + 1 成功,赞一个!谢谢帮助!

查看全部评分

回复

使用道具 举报

发表于 2013-10-9 16:58 | 显示全部楼层
本帖最后由 美斯特邦威 于 2013-10-9 17:01 编辑
  1. range(cells(3,1),cells(7,cells(7,cloumns.count).end(1).column))
复制代码

评分

参与人数 1 +1 收起 理由
棋行天下黄 + 1 运行错误,谢谢帮助!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-10 10:46 , Processed in 0.289020 second(s), 18 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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