excel 取列中每个单元格的反对数

wztqucjr  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(175)

我有一个以日志格式显示的数据表,我需要能够更改它才能上载它。我似乎一直遇到与单元格内容被读取为字符串而不是双精度值相关的错误。
我尝试了下面代码的几种变体(使用^和power)。我能得到的最接近的变体是用公式本身而不是答案填充每个单元格。变量c正确加载,其他公式似乎也能工作。

Dim c As Range

Dim Lastrow As Long

Lastrow = ActiveWorkbook.Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In Range("A2 :A" & Lastrow)
   c = c.Power(10, c)
    Next
End Sub

Function Antilog(rwDest As Range)
Dim c As Range
Dim Lastrow As Long
Lastrow = ActiveWorkbook.Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
    For Each c In Range("A2" & Lastrow)
         c.Value = "=10^ c.Value"
    Next
End Function
kcugc4gi

kcugc4gi1#

使用Worksheet.Evaluate避免循环

Sub AntiLog()
        
    ' Reference the workbook.
    Dim wb As Workbook: Set wb = ActiveWorkbook
    ' If it's the workbook containing this code, use 'ThisWorkbook' instead.
    
    ' Reference the single-column range.
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim rg As Range: Set rg = ws.Range("A2:A" & LastRow)
    
    ' Use 'Worksheet.Evaluate'...
    rg.Value = ws.Evaluate("10^" & rg.Address)
    'rg.Value = ws.Evaluate("IFERROR(10^" & rg.Address & ","""")")
 
End Sub

相关问题