我正在尝试使用LinEst函数从一系列数据行中获取值,并将其输入到某些标题下的新工作表中。我只想对特定数量的行执行此操作(最多将行号定义为“ c”。我的VBA技能非常基础,并且在生成以下代码方面有所帮助。
问题是我想通过将日志取到数据的基数10来转换一系列数据(xrng和yrng)。但是,当我尝试使用Log10或WorksheetFunction.Log10函数时,它指定数据必须为双精度,并且如果运行代码,则会出现类型不匹配错误。
Option Explicit
Sub Button7_Click()
Dim xrng As Range, yrng As Range, lxrng As Range, lyrng As Range
Dim Drop As Range
Dim Arr As Variant ' LinEst result array
Dim Rng As Range
Dim R As Long
Dim l As Long
Dim k As Long
Dim c As Long
Dim DownSweep As Chart, UpSweep As Chart, cht As Chart
Dim ws As Worksheet, Smallest As Variant
Dim dsws As Worksheet
Set ws = Worksheets("Template")
Sheets.Add.Name = "Down Sweep Power Law"
Set dsws = Worksheets("Down Sweep Power Law")
Set Rng = ws.Range(ws.Range("B11"), ws.Range("B11").End(xlDown))
Smallest = WorksheetFunction.Small(Rng, 1)
l = Rng.Find(what:=Smallest, LookIn:=xlValues, LookAt:=xlWhole).Row
k = Rng.Rows.Count
c = l - 10
R = 1
Set xrng = ws.Range("C11:CP11")
Set yrng = ws.Range("C201:CP201")
Set Drop = dsws.Range("C2:CP2").Offset(0, -2)
dsws.Range("A1").Value = "(n-1) Value"
dsws.Range("B1").Value = "log(k) Value"
dsws.Range("C1").Value = "n Value"
dsws.Range("D1").Value = "k Value"
dsws.Range("E1").Value = "R Value"
Do While R < c
Arr = Application.LogEst(Log10(yrng), Log10(xrng), True, False)
Drop.Value = Arr ' or perhaps: = Application.Transpose(Arr)
Set xrng = xrng.Offset(1, 0)
Set yrng = yrng.Offset(1, 0)
Set Drop = Drop.Offset(1, 0)
R = R + 1
Loop
End Sub
任何帮助将不胜感激。
未测试
替换:
Arr = Application.LogEst(Log10(yrng), Log10(xrng), True, False)
with:
With Application.WorksheetFunction
Arr = .LogEst(.Log10(yrng), .Log10(xrng), True, False)
End With
(可能需要其他更改。)
请尝试这段代码:
Dim ws As Worksheet, Arr As Variant, Drop As Range
Set ws = ActiveSheet 'use here your sheet
For i = 3 To 24 'columns number of C and CP
Arr = Application.LogEst(Log10(ws.Cells(11, i).value), Log10(ws.Cells(201, i).value), True, False)
'Here you must adapt the size of your Drop range.
'I do not understand how do you build it...
'I can also help on the issue if you clearly explain what do you want from this point of view (in workds)...
Next i