我有一個宏,它填充一個圖形並將兩個工作表添加到工作簿中。我如何添加一張從特定文件夾中拉出Excel文件中的數據並將excel文件添加到其他兩張表之前的工作表?在excel中現有工作表旁邊的工作表中添加XLS文件
Private Sub Workbook_Open()
Dim files(1 To 20) As String
Dim numOfFiles As Integer
Dim chartName As String
Dim FilePath As String
Dim strPath As String
Dim strFile As String
Dim strFile1 As String
Dim strChart As String
Dim i As Integer
Dim j As Integer
strPath = "C:\PortableRvR\report\"
strFile = Dir(strPath & "*.csv")
i = 1
Do While strFile <> ""
With ActiveWorkbook.Worksheets.Add
With .QueryTables.Add(Connection:="TEXT;" & strPath & strFile, _
Destination:=.Range("A1"))
.Parent.Name = Replace(strFile, ".csv", "")
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
files(i) = .Parent.Name
i = i + 1
End With
End With
strFile = Dir
Loop
chartName = "Chart 8"
For j = 1 To numOfFiles
strFile = files(j)
Sheets(strFile).Select
Plot_y = Range("E1", Selection.End(xlDown)).Rows.Count
Plot_x = Range("D1", Selection.End(xlDown)).Rows.Count
Sheets("Uplink VS attenuation").Select
If j = 1 Then ActiveSheet.ChartObjects(chartName).Activate
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "TxPower"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Attenuation(dB)"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "TxPower(dBm)"
.Axes(xlCategory, xlPrimary).MinimumScale = 30 ' Constant value
End With
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(j).Name = strFile
ActiveChart.SeriesCollection(j).XValues = Sheets(strFile).Range("D1:D" & Plot_x)
ActiveChart.SeriesCollection(j).Values = Sheets(strFile).Range("E1:E" & Plot_y)
ActiveChart.SeriesCollection(j).MarkerStyle = -4142
ActiveChart.SeriesCollection(j).Smooth = False
Next j
ActiveSheet.ChartObjects(chartName).Activate
ActiveChart.Axes(xlValue).DisplayUnit = xlMillions
ActiveChart.Axes(xlValue).HasDisplayUnitLabel = False
End Sub
你嘗試過什麼?如果沒有,那麼你可能想從Excel VBA中按下魔術按鈕「F1」來檢查'Sheets.Add'的功能;) – 2012-03-28 08:41:41
好的。上述代碼面臨的問題是什麼? – 2012-03-28 18:11:25
現在我記得這個代碼:)它正在工作更早的權利?圖表在csv導入後生成。所以你唯一需要的是在表格(「上行VS衰減」)之前移動該表格? – 2012-03-28 18:21:03