定义一个变量以用作图表标题和文件名

时间:2015-04-16 21:24:51

标签: excel vba

我设法解决了我的问题,所以我正在更新我的代码以防万一有人偶然发现类似的东西。 (问题是:我有多张表,我没有在我的代码中指定)


我在Excel中打开一个csv文件。我希望单元格C1中的字符串存储为FILE,并将其用于:

  1. 将我的Excel文件保存为" FilePath ... \ FILE.xlsm"
  2. 绘制一些图表(时间序列数据)并为每个图表的标题提供"文件的平均值"
  3. 这里是代码

    Sub OpenCvs()
    'Open Cvs with right formats
    
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\medeossie\Documents\CariParma\SAS\Corporate\corANA01.csv", _
        Destination:=Range("$A$1"))
        .Name = "corANA01"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(4, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    End Sub
    
    Private Sub CommandButton1_Click()
    'Plot Charts
    
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = "=Sheet1!$B$1"
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$B$2:$B$61"
    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$61"
    ActiveChart.ChartType = xlLine
    ActiveChart.ChartType = xlLine
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
    
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = "=Sheet1!$C$1"
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$C$2:$C$61"
    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$61"
    ActiveChart.ChartType = xlLine
    ActiveChart.ChartType = xlLine
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
    
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = "=Sheet1!$D$1"
    ActiveChart.SeriesCollection(1).Values = "=Sheet1!$D$2:$D$61"
    ActiveChart.SeriesCollection(1).XValues = "=Sheet1!$A$2:$A$61"
    ActiveChart.ChartType = xlLine
    ActiveChart.ChartType = xlLine
    ActiveChart.Legend.Select
    Selection.Delete
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
    
    
    'Lining up Charts
    
    Dim MyWidth As Single, MyHeight As Single
    Dim NumWHigh As Long
    Dim iChtIx As Long, iChtCt As Long
    
    MyWidth = 400
    MyHeight = 300
    NumHigh = 3
    
    iChtCt = ActiveSheet.ChartObjects.Count
    For iChtIx = 1 To iChtCt
    With ActiveSheet.ChartObjects(iChtIx)
         .Width = MyWidth
         .Height = MyHeight
         .Left = ((iChtIx - 1) Mod NumHigh) * MyWidth
         .Top = Int((iChtIx - 1) / NumHigh) * MyHeight
    End With
    Next
    
    'Saving file
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename _
        :="C:\Users\medeossie\Documents\CariParma\SAS\Corporate\" & Range("Sheet1!C1") & ".xlsm", FileFormat _
        :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
    End Sub
    

1 个答案:

答案 0 :(得分:1)

您需要一个字符串类型变量,可以为其分配一些文本。您必须在图表对象属性中工作才能获得标题。

这是一个相当基本的例子,但您应该能够看到图表标题的存在和内容的确定位置。我会留下其他几个,这样你就可以看到定位和源数据之类的内容。

Dim cht As Chart, sTitle as String
sTitle = "My New Chart Title"
Set cht = Charts.Add
Set cht = cht.Location(Where:=xlLocationAsObject, Name:="Sheet1")
With cht
    .ChartType = xl3DPie
    ' lots of other chart definition stuff here
    .SetSourceData Source:=Range("Sheet1'!$A$2:$A$9")
    .HasLegend = False
    With .Parent
        .Name = "My_New_Chart"
        .Top = Range("M5").Top
        .Left = Range("M5").Left
        .Width = Range("M5:Q20").Width
        .Height = Range("M5:Q20").Height
    End With
    ' the next two have to do with the chart title
    .HasTitle = True
    .ChartTitle.Text = sTitle 
End With

关于保存工作表,您真的需要知道从C1中绘制文件名的工作表。 ActiveSheet当时可能是 doh-key> most ,但是通过工作表名称或代号定义工作表的文件名会更好。我假设一个名为 SheetOne 的工作表。

ActiveWorkbook.SaveAs _
  Filename:="C:\Users\medeossie\Documents\CariParma\SAS\Corporate\" & Sheets("SheetOne").Range("C1").Value & ".xlsm", _
  FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Workbook.SaveAs Method看起来是正确的,虽然我可能不会为 CreateBackup:= False 而烦恼,因为false是默认值。表格中的值非法文件名字符(" SheetOne")。范​​围(" C1")是另一个可能的麻烦区域。