范围上的 Log10 函数 - 类型 13 不匹配错误
Log10 function on a range - type 13 mismatch error
我正在尝试使用 LinEst 函数从一系列数据行中获取值,并将它们输入到某些标题下的新 sheet 中。我只想对特定数量的行执行此操作(最多定义为 "c" 的行号)。我的 VBA 技能非常基础,我在生成下面的代码时得到了帮助。
问题是我想通过以 log 为基数 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 Application.WorksheetFunction
Arr = .LogEst(.Log10(yrng), .Log10(xrng), True, False)
End With
(可能需要进行其他更改。)
请试试这段代码。出于明显的原因未进行测试。我必须承认,我无法理解 Droprange must exist. Its row must be updated for each cell iteration. Looking to the
LogEst` 函数定义在哪里,它接收了一个类似参数的范围。不是 Log10 不能应用于范围...如果您需要每个范围单元格的对数值,则必须进行初步范围处理。
Sub testLog10()
Dim ws As Worksheet, dsws As Worksheet, Arr As Variant, Drop As Range
Set ws = ActiveSheet 'use here your sheet
dsws = Worksheets("Your sheet") ' use here your sheet name
Arr = Application.LogEst(ws.Range(ws.Cells(11, 3), ws.Cells(11, 94)), _
ws.Range(ws.Cells(201, 3), ws.Cells(201, 94)), True, False)
Set Drop = dsws.Range("A2")
Drop.Resize(1, UBound(Arr)).value = Arr
End Sub
我正在尝试使用 LinEst 函数从一系列数据行中获取值,并将它们输入到某些标题下的新 sheet 中。我只想对特定数量的行执行此操作(最多定义为 "c" 的行号)。我的 VBA 技能非常基础,我在生成下面的代码时得到了帮助。
问题是我想通过以 log 为基数 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 Application.WorksheetFunction
Arr = .LogEst(.Log10(yrng), .Log10(xrng), True, False)
End With
(可能需要进行其他更改。)
请试试这段代码。出于明显的原因未进行测试。我必须承认,我无法理解 Droprange must exist. Its row must be updated for each cell iteration. Looking to the
LogEst` 函数定义在哪里,它接收了一个类似参数的范围。不是 Log10 不能应用于范围...如果您需要每个范围单元格的对数值,则必须进行初步范围处理。
Sub testLog10()
Dim ws As Worksheet, dsws As Worksheet, Arr As Variant, Drop As Range
Set ws = ActiveSheet 'use here your sheet
dsws = Worksheets("Your sheet") ' use here your sheet name
Arr = Application.LogEst(ws.Range(ws.Cells(11, 3), ws.Cells(11, 94)), _
ws.Range(ws.Cells(201, 3), ws.Cells(201, 94)), True, False)
Set Drop = dsws.Range("A2")
Drop.Resize(1, UBound(Arr)).value = Arr
End Sub