我正在研究将多个excel工作簿编译为一个并绘制数据的程序。我碰到的一个问题是,实际数据之前的行会变化,我希望代码能够自己找到起点。最重要的是,我希望它使用从该行开始的范围,并一直沿着电子表格继续前进,直到数据停止。 Data File ExampleVBA Excel:查找超过5列正在使用的行
这是到目前为止我的代码:
Private Sub runHPO_Click()
Dim FolderPath As String
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As range
Dim DestRange As range
Dim DataSheet As Worksheet
Dim cht As Chart
Application.ScreenUpdating = False
'Test specific section - directory, chart title
FolderPath = "I:\SHARED\Marshall Test Compiler\Performance Tests\3.2.1.7 HPO\"
FileName = Dir(FolderPath & "*.*")
ThisWorkbook.Charts.Add.Name = "HPO"
Set cht = ActiveChart
With cht
.ChartType = xlXYScatterLinesNoMarkers
.HasTitle = True
.ChartTitle.Text = "3.2.1.7 Hot Pump Out"
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Time [min:sec]"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Text = "Fan Speed [rpm]"
End With
Do While FileName <> ""
ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = FileName
Set DataSheet = ActiveSheet
Set WorkBk = Workbooks.Open(FolderPath & FileName)
Set SourceRange = WorkBk.Worksheets(1).range("A1:Z2045")
Set DestRange = DataSheet.range("A1:Z2045")
DestRange.Value = SourceRange.Value
'Change legend name to serial number
Dim LName As String
LName = DataSheet.range("A14").Characters(8, 9).Text
'Add plotting
Dim profTime As range
Dim profInSpeed As range
Dim profSpDemand As range
Dim profLoLimit
Dim xrange As range
Dim fsrange As range
Dim pwmrange As range
Dim btrange As range
Dim sdrange As range
Set profTime = ThisWorkbook.Worksheets("Profiles").range("H4:H13")
Set profInSpeed = ThisWorkbook.Worksheets("Profiles").range("I4:I13")
Set profSpDemand = ThisWorkbook.Worksheets("Profiles").range("J4:J13")
Set profUpLimit = ThisWorkbook.Worksheets("Profiles").range("K4:K13")
Set xrange = DataSheet.range("A797:A2045")
Set fsrange = DataSheet.range("D797:D2045")
Set pwmrange = DataSheet.range("J797:J2045")
Set btrange = DataSheet.range("F797:F2045")
Set sdrange = DataSheet.range("K797:K2045")
xrange.NumberFormat = "mm:ss"
profTime.NumberFormat = "mm:ss"
'Profile
With cht.SeriesCollection.NewSeries
.Name = "Input Speed"
.AxisGroup = xlPrimary
.Values = profInSpeed
.XValues = profTime
End With
With cht.SeriesCollection.NewSeries
.Name = "Speed Demand"
.AxisGroup = xlPrimary
.Values = profSpDemand
.XValues = profTime
End With
With cht.SeriesCollection.NewSeries
.Name = "Fan Speed Upper Limit"
.AxisGroup = xlPrimary
.Values = profUpLimit
.XValues = profTime
End With
'Fan Speed
With cht.SeriesCollection.NewSeries
.Name = LName & " Fan Speed"
.AxisGroup = xlPrimary
.Values = fsrange
.XValues = xrange
End With
'PWM
With cht.SeriesCollection.NewSeries
.Name = LName & " PWM"
.AxisGroup = xlSecondary
.Values = pwmrange
.XValues = xrange
End With
'Box Temp
With cht.SeriesCollection.NewSeries
.Name = LName & " Box Temp"
.AxisGroup = xlSecondary
.Values = btrange
.XValues = xrange
End With
'Speed Demand
With cht.SeriesCollection.NewSeries
.Name = LName & " Speed Demand"
.AxisGroup = xlSecondary
.Values = sdrange
.XValues = xrange
End With
WorkBk.Close savechanges:=False
FileName = Dir()
Loop
With cht
.HasAxis(xlValue, xlSecondary) = True
.Axes(xlValue, xlSecondary).HasTitle = True
.Axes(xlValue, xlSecondary).AxisTitle.Select
.Axes(xlValue, xlSecondary).AxisTitle.Text = "PWM [%]/Box Temp [degC]"
.Axes(xlValue, xlPrimary).MaximumScale = 2400
.Axes(xlValue, xlSecondary).MaximumScale = 120
.Axes(xlValue, xlSecondary).MinimumScale = -800
.SeriesCollection(1).Delete
End With
ThisWorkbook.Worksheets("Compiler").Select
Application.ScreenUpdating = True
End Sub
欢迎来到SO!一般来说,将你的问题分解成更小,更离散的问题,关于“我该怎么做......”对于像我这样的人来说更容易帮助你。 “我希望代码能够自己找到起点。” ---让我们谈谈这个问题。你有没有听说过'''xlUp'''和'''xlDown'''?如何使用'''Cells(i,j)'''?谷歌这些,看看它可能会帮助! –
谢谢,我会牢记这一点!我做了相当多的搜索,并尝试了一些变化,主要是包括'.find',但我似乎无法使它工作。我相信我使用的确切代码是'rowStart = DataSheet.Columns(“K”)。Find(what:=“*”,After:= Cells(“K1”),LookIn:= xlValues,SearchDirection:= xlNext)'和'fRow = rowStart.row'。 –