开发者

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. Here's my generated Macro code which imports data from a text file and graphs it.

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 = Tr开发者_高级运维ue
    .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; it just goes from D3:H3. I'd like the chart to use the data from D3 to HX, where X is the end of the data row. 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!


For these thigs, I like to use a named range using =OFFSET().
See http://www.ozgrid.com/Excel/DynamicRanges.htm or google for "excel dynamic range offset"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜