这是我的第一个问题,所以请耐心等待:)
我不是一个经验丰富的 VBA 程序员,我在我的软件中遇到了一些问题。
我有一个程序可以粘贴一些数据,然后添加一些新列。之后,它拆分一些文本并将其放在新列中的单元格内。
该程序第一次运行完美,但第二次似乎将数据粘贴错误。它有不同的外观,当它从某些显然不存在的单元格中提取数据时,程序会失败。
它给了我一个错误:无法获取工作表函数类的平均属性
希望你有一些好的想法。我试图清除所有格式、内容等。
谢谢你。
这是我的代码,非常抱歉糟糕的编程风格。我需要将我的一些循环收集到更流畅的东西中,但首先我需要它工作:)
感谢您的时间!
Option Explicit
Private Sub btnExit_Click()
Application.Quit
End Sub
Private Sub btni2_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Worksheets("System").Activate
Worksheets("System").Cells(1, 1).Select
ActiveCell.PasteSpecial
On Error GoTo myError:
Worksheets("System").Cells(2, 2) = "=COUNTA(A3:A10000)"
Dim laps As Integer
laps = Worksheets("System").Cells(2, 2)
'MsgBox ("Resultat er: " & laps)
' Opret nye kolloner til at seperare tekst fra I2.
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("H:H").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'Flyt text til nye kolloner for at splitte data op
'Split A
Range("A3:A10000").Select
Selection.TextToColumns Destination:=Range("A3"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
'Split C
Range("C3:C10000").Select
Selection.TextToColumns Destination:=Range("C3"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
'Split E
Range("E3:E10000").Select
Selection.TextToColumns Destination:=Range("E3"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
'Split G
Range("G3:G10000").Select
Selection.TextToColumns Destination:=Range("G3"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
'check om der er data i Main arket
Dim Check As String
Check = Worksheets("Main").Range("B1").Value
If Check = "" Then
Worksheets("System").Range("A3").Copy
Worksheets("Main").Select
Range("B1").Select
Selection.PasteSpecial
Worksheets("System").Select
Worksheets("System").Range("B3").Copy
Worksheets("Main").Select
Range("B2").Select
Selection.PasteSpecial
Worksheets("System").Select
Worksheets("System").Range("C3").Copy
Worksheets("Main").Select
Range("B6").Select
Selection.PasteSpecial
Worksheets("System").Select
Worksheets("System").Range("D3").Copy
Worksheets("Main").Select
Range("B4").Select
Selection.PasteSpecial
Worksheets("System").Select
Worksheets("System").Range("E3").Copy
Worksheets("Main").Select
Range("B3").Select
Selection.PasteSpecial
Range("B7").Value = "Mads S. Christiansen"
Worksheets("System").Select
End If
'definer alle de variabler der skal pastes ind i de respektive sessions
Dim EditLaps As Integer
Dim FastLap As Variant 'J
Dim NoLaps As Integer 'inkl in/out brug variabel laps fra tidligere
Dim TotalTime As Variant 'Sum af alle felter i J =sum(J3:J+laps)
Dim TotalKm As Variant ' AM3 og AN & laps +3 trukket fra hinanden
Dim MaxRpm As Long 'Max V3 til V & laps + 3
Dim MaxWaterT As Double ' max O3 til O & laps + 3
Dim AvgWaterT As Double ' avg O3 til O & laps + 3
Dim MaxOilT As Double ' MAX Q3 til Q & laps + 3
Dim AvgOilT As Double ' AVG
Dim IntakeT As Double
Dim MaxOilP As Double
Dim MinOilP As Double
Dim AvgOilP As Double
Dim MaxCoolP As Double
Dim MinCoolP As Double
Dim AvgCoolP As Double
Dim TotalKm1, TotalKm2 As Variant
NoLaps = laps
'Bruges som reference for at det passser med offset pga af første celle ref
EditLaps = NoLaps + 2
'Find hurtigste omgang og tildel den til FastLap
FastLap = Application.WorksheetFunction.Min(Range(Cells(3, 10), Cells(EditLaps, 10)))
' Denne format virker !! Range("Z1").NumberFormat = "mm:ss.000"
' Total tid for session
TotalTime = Format(Application.WorksheetFunction.Sum(Range(Cells(3, 10), Cells(EditLaps, 10))), "HH:MM:SS")
'Total antal km for session, er dist slut minus dist start
TotalKm1 = Range("AM3").Value
TotalKm2 = Range("AN" & EditLaps).Value
TotalKm = TotalKm2 - TotalKm1
'------------------------------------------ Dette er for at convertere felte om til nummerisk formatering----------
Dim a As Variant
Dim b As Variant
Dim c As Variant
Dim d As Variant
Dim e As Variant
Dim f As Variant
Dim g As Variant
Dim h As Variant
Dim i As Variant
Dim j As Variant
For Each a In Range("V1:V" & EditLaps)
If a = "" Then GoTo nexta
If IsNumeric(a) Then
a.Value = a.Value * 1
a.NumberFormat = "general"
End If
nexta:
Next a
For Each b In Range("N1:N" & EditLaps)
If b = "" Then GoTo nextb
If IsNumeric(b) Then
b.Value = b.Value * 1
b.NumberFormat = "general"
End If
nextb:
Next b
For Each c In Range("O1:O" & EditLaps)
If c = "" Then GoTo nextc
If IsNumeric(c) Then
c.Value = c.Value * 1
c.NumberFormat = "general"
End If
nextc:
Next c
For Each d In Range("K1:K" & EditLaps)
If d = "" Then GoTo nextd
If IsNumeric(d) Then
d.Value = d.Value * 1
d.NumberFormat = "general"
End If
nextd:
Next d
For Each e In Range("L1:L" & EditLaps)
If e = "" Then GoTo nexte
If IsNumeric(e) Then
e.Value = e.Value * 1
e.NumberFormat = "general"
End If
nexte:
Next e
For Each f In Range("Q1:Q" & EditLaps)
If f = "" Then GoTo nextf
If IsNumeric(f) Then
f.Value = (f.Value * 1) / 1000
f.NumberFormat = "general"
End If
nextf:
Next f
For Each g In Range("P1:P" & EditLaps)
If g = "" Then GoTo nextg
If IsNumeric(g) Then
g.Value = (g.Value * 1) / 1000
g.NumberFormat = "general"
End If
nextg:
Next g
For Each h In Range("R1:R" & EditLaps)
If h = "" Then GoTo nexth
If IsNumeric(h) Then
h.Value = (h.Value * 1) / 1000
h.NumberFormat = "general"
End If
nexth:
Next h
For Each i In Range("T1:T" & EditLaps)
If i = "" Then GoTo nexti
If IsNumeric(i) Then
i.Value = i.Value * 1
If i.Value >= 1 Then
i.Value = i.Value / 1000
End If
i.NumberFormat = "general"
End If
nexti:
Next i
For Each j In Range("S1:S" & EditLaps)
If j = "" Then GoTo nextj
If IsNumeric(j) Then
j.Value = j.Value * 1
If j.Value >= 1 Then
j.Value = j.Value / 1000
End If
j.NumberFormat = "general"
End If
nextj:
Next j
'Max rpm
MaxRpm = Application.WorksheetFunction.Max(Range(Cells(3, "V"), Cells(EditLaps, "V")))
'Max vand temp
MaxWaterT = Application.WorksheetFunction.Max(Range(Cells(3, "N"), Cells(EditLaps, "N")))
AvgWaterT = Application.WorksheetFunction.Average(Range(Cells(3, "O"), Cells(EditLaps, "O")))
MaxOilT = Application.WorksheetFunction.Max(Range(Cells(3, "K"), Cells(EditLaps, "K")))
AvgOilT = Application.WorksheetFunction.Average(Range(Cells(3, "L"), Cells(EditLaps, "L")))
'IntakeT =
MaxOilP = Application.WorksheetFunction.Max(Range(Cells(4, "Q"), Cells(EditLaps - 1, "Q")))
MinOilP = Application.WorksheetFunction.Min(Range(Cells(4, "P"), Cells(EditLaps - 1, "P")))
AvgOilP = Application.WorksheetFunction.Average(Range(Cells(4, "R"), Cells(EditLaps - 1, "R")))
MaxCoolP = Application.WorksheetFunction.Max(Range(Cells(4, "T"), Cells(EditLaps - 1, "T")))
MinCoolP = Application.WorksheetFunction.Min(Range(Cells(4, "S"), Cells(EditLaps - 1, "S")))
AvgCoolP = Application.WorksheetFunction.Average(Range(Cells(4, "T"), Cells(EditLaps - 1, "T")))
' lav et object der indeholder det sheet som der skal bruges
Dim Sheet As Object
Set Sheet = Worksheets("Main")
'Definer hvilken session der er kopieret ind
Dim Session As String
Session = UCase(Range("F3"))
Select Case Session
Case Is = " TEST"
Set Sheet = Worksheets("Test")
Case Is = " Q1"
Set Sheet = Worksheets("Q1")
Case Is = " Q2"
Set Sheet = Worksheets("Q2")
Case Is = " WU"
Set Sheet = Worksheets("WU")
Case Is = " RACE1"
Set Sheet = Worksheets("Race1")
Case Is = " RACE2"
Set Sheet = Worksheets("Race2")
End Select
Sheet.Activate
Range("B3").Value = FastLap
Range("B4").Value = NoLaps
Range("B5").Value = TotalTime
Range("B7").Value = TotalKm
Range("B13").Value = MaxRpm
Range("B16").Value = MaxWaterT
Range("B17").Value = AvgWaterT
Range("B20").Value = MaxOilT
Range("B21").Value = AvgOilT
Range("B24").Value = 25
Range("B27").Value = MaxOilP
Range("B28").Value = MinOilP
Range("B29").Value = AvgOilP
Range("B32").Value = MaxCoolP
Range("B33").Value = MinCoolP
Range("B34").Value = AvgCoolP
Sheet9.Activate
Sheet9.Cells.Select
With Cells
.Clear
.ClearComments
.ClearContents
.ClearFormats
.ClearHyperlinks
.ClearNotes
.ClearOutline
End With
' aktiver main siden efter endt handling af System seperation
Worksheets("Main").Activate
Cells(1, 1).Select
'Fjern hovedform fra billede og derefter vises Main arket.
MainForm.Hide
myError:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
If Err Then MsgBox Err.Description, vbCritical, "Error"
End Sub
Private Sub btnView_Click()
' aktiver kun main sheet hvis der oenskes view.
Worksheets("Main").Activate
'marker celle
Cells(1, 1).Select
'gem main form sŒ der kun er normalt excel view
MainForm.Hide
End Sub