[英]Excel Visual Basic Macro- Selecting data dynamically for a chart?
So, this question is probably fairly stupid, but I'm not too familiar with Excel VBA. 因此,这个问题可能很愚蠢,但是我对Excel VBA不太熟悉。 Here's my generated Macro code which imports data from a text file and graphs it. 这是我生成的Macro代码,该代码从文本文件导入数据并对其进行图形处理。
Sub getData()
'
' getData Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\data.txt", Destination:=Range("$D$3"))
.Name = "data_2"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Range("D3:H4").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
ActiveChart.SetSourceData Source:=Range("Sheet1!$D$3:$H$4")
End Sub
Essentially, because the data inputted from data.txt can be any length, this program does not work; 本质上,因为从data.txt输入的数据可以是任意长度,所以该程序不起作用; it just goes from D3:H3. 它仅来自D3:H3。 I'd like the chart to use the data from D3 to HX, where X is the end of the data row. 我希望图表使用从D3到HX的数据,其中X是数据行的末尾。 How would I do this? 我该怎么做? Thanks for helping an idiot ! 感谢您帮助白痴 !
Probably the following will work: 可能以下方法会起作用:
Sub getData()
With ...
....
End With
LastRowColH = Range("H65536").End(xlUp).Row
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
ActiveChart.SetSourceData Source:=Range("Sheet1!$D$3:$H$" & CStr(LastRowColH))
End Sub
HTH! HTH!
For these thigs, I like to use a named range using =OFFSET(). 对于这些thig,我喜欢使用= OFFSET()命名的范围。
See http://www.ozgrid.com/Excel/DynamicRanges.htm or google for "excel dynamic range offset" 请参阅http://www.ozgrid.com/Excel/DynamicRanges.htm或Google,以获取“ excel动态范围偏移量”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.