简体   繁体   English

根据使用vba填充的新数据更新excel图形

[英]update excel graph according to new data populated using vba

Sub Macro1()
    Dim wb1 As Excel.Workbook
    Dim lastcolumn As Integer
    Dim EndColumnname As String
    Dim StartColumnname As String
    Set wb1 = ActiveWorkbook
    wb1.Sheets("Sheet1").Activate
    lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    EndColumnname = ConvertToLetter(lastcolumn)
    StartColumnname = ConvertToLetter(lastcolumn - 4)
    ActiveSheet.ChartObjects("Chart 6").Activate
   'ActiveChart.SetSourceData Source:=Range("A1,C1:H1,A3:A4,C3:H4")
    ActiveChart.SetSourceData Source:=Range("A1", StartColumnname & 1 & ":" & EndColumnname & 1, "A3:A4", StartColumnname & 3 & ":" & EndColumnname & 4)
End Sub

Function ConvertToLetter(iCol As Integer) As String
   Dim iAlpha As Integer
   Dim iRemainder As Integer
   iAlpha = Int(iCol / 27)
   iRemainder = iCol - (iAlpha * 26)
   If iAlpha > 0 Then
      ConvertToLetter = Chr(iAlpha + 64)
   End If
   If iRemainder > 0 Then
      ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
   End If
End Function

i am getting the error in this line 我在这行中得到错误

ActiveChart.SetSourceData Source:=Range("A1", StartColumnname & 1 & ":" & EndColumnname & 1, "A3:A4", StartColumnname & 3 & ":" & EndColumnname & 4)

Error is - Compile error--- wrong no. 错误是-编译错误--错误号。 of arguments or invalid property assigned 分配的参数或无效属性

3rd parameter - is series labels 2nd parameter - date range for x axis label 4th parameter - data range 第三个参数-是系列标签第二个参数-x轴标签的日期范围第四个参数-数据范围

It works fine when i hard code it like 当我像这样硬编码时,它工作正常

ActiveChart.SetSourceData Source:=Range("A1,C1:H1,A3:A4,C3:H4")

I want when data is populated in "I" column the above line become as shown below with the help of code 我希望在“ I”列中填充数据时,借助代码,上一行变为如下所示

ActiveChart.SetSourceData Source:=Range("A1,D1:I1,A3:A4,D3:I4") 

You may want to borrow R3uK's ConvertToLetter routine, but you can set your source data in a single line if you'd like, you just have to fix the formatting & concatenation. 您可能想借用R3uK的ConvertToLetter例程,但是如果愿意,可以在一行中设置源数据,只需修复格式和串联。

Change 更改

ActiveChart.SetSourceData Source:=Range("A1", StartColumnname & 1 & ":" & _
  EndColumnname & 1, "A3:A4", StartColumnname & 3 & ":" & EndColumnname & 4)

to

ActiveChart.SetSourceData Source:=Range("A1," & StartColumnname & "1:" & _
  EndColumnname & "1, A3:A4," & StartColumnname & "3:" & EndColumnname & "4")

You just managed to confuse yourself with location of commas outside of quoted strings, when they needed to be inside. 当逗号需要放在引号中时,您只是设法使自己与逗号的位置混淆。 For example, in your original line, you have "A1", , where the comma outside the quotes is a parameter separator, not a separator between the ranges. 例如,在原始行中,您有"A1",其中引号外的逗号是参数分隔符,而不是范围之间的分隔符。

You're overcomplicating your life. 您使生活变得过于复杂。 I personally tend to load my data into a table and deploy the chart to given table. 我个人倾向于将数据加载到表中,然后将图表部署到给定的表中。 When the dataset expands, the table expands as well and your chart will refresh by itself. 当数据集扩展时,表也将扩展,并且图表将自动刷新。 May not work in your case (shared wb or such) but for me it usually does the trick. 可能不适用于您的情况(共享wb等),但对我而言通常可以解决问题。

It's better to create an intermediate string and I changed your ConvertToLetter with something more efficient (I used the same algo before too ;) ) 最好创建一个中间字符串,然后用更有效的方式更改您的ConvertToLetter(我之前也使用相同的算法;))

Try this : 尝试这个 :

Sub Macro1()
    Dim Wb1 As Excel.Workbook
    Dim Ws1 As Worksheet
    Dim LastColumn As Integer
    Dim EndColumnname As String
    Dim StartColumnname As String
    Dim RgSrc1 As String
    Dim RgSrc2 As String


    'Set wb1 = ActiveWorkbook
    Set Ws1 = ActiveWorkbook.Sheets("Sheet1")
    LastColumn = Ws1.Cells(1, Columns.Count).End(xlToLeft).Column

    EndColumnname = ConvertToLetter(LastColumn)
    StartColumnname = ConvertToLetter(LastColumn - 4)


   'ActiveChart.SetSourceData Source:=Range("A1,C1:H1,A3:A4,C3:H4")
    RgSrc1 = StartColumnname & 1 & ":" & EndColumnname & 1
    RgSrc2 = StartColumnname & 3 & ":" & EndColumnname & 4

    ActiveSheet.ChartObjects("Chart 6").SetSourceData Source:=Range("A1", RgSrc1, "A3:A4", RgSrc2)

    Set Wb1 = Nothing
    Set Ws1 = Nothing
End Sub

Public Function ConvertToLetter(iCol As Integer) As String
With ActiveSheet.Columns(iCol)
    ConvertToLetter = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM