简体   繁体   English

VBA Excel 填充公式与外部 function 向下直到最后一行

[英]VBA Excel filling formula with external function down till the last row

I am using the function located here:我正在使用位于此处的 function:

https://www.extendoffice.com/documents/excel/1497-excel-convert-decimal-degrees-to-degrees-minutes-seconds.html https://www.extendoffice.com/documents/excel/1497-excel-convert-decimal-degrees-to-degrees-minutes-seconds.html

in order to convert the degree min sec values to the decimal ones.为了将度分秒值转换为十进制值。

Unfortunately, I've encountered the problem.不幸的是,我遇到了这个问题。

I found that is not so simple as shown in this example:我发现这并不像这个例子中显示的那么简单:

Fill formula down till last row in column 向下填充公式直到列中的最后一行

in the other hand, the example more dedicated for my situation另一方面,这个例子更适合我的情况

Finding the Last Row and using it in a formula 查找最后一行并在公式中使用它

also wans't successful.也不想成功。

I would like to know why I am getting:我想知道为什么我得到:

  • the result to the second row only结果只到第二行
  • the "@" character before by formula (as I can see in the bar).公式之前的“@”字符(正如我在栏中看到的那样)。

What is wrong with my code then?那我的代码有什么问题?

 Sub Sun()
 Dim rng As Range, cell As Range
 Dim wors As Worksheet

 Set wors = ThisWorkbook.ActiveSheet

 Dim lastRow As Long, LastRow2 As Long

 lastRow = wors.Range("P" & wors.Rows.Count).End(xlUp).Row
 LastRow2 = wors.Range("Q" & wors.Rows.Count).End(xlUp).Row
 Set rng = wors.Range("P1:P" & lastRow)



 wors.Columns("E").Copy
 wors.Columns("P").PasteSpecial xlPasteValues



 For Each cell In rng
 cell = WorksheetFunction.Substitute(cell, " ", "° ", 1)
 Next

 Call Degree

 Range("Q2:Q" & lastRow).Formula = "=ConvertDecimal (P" & LastRow2 & " )"

 End Sub

Moreover, it comes to an error:此外,它会出现一个错误:

Invalid procedure call or argument无效的过程调用或参数

, with debugging: ,带调试:

     xDeg = Val(Left(pInput, InStr(1, pInput, "°") - 1))  

the code from the function: function 中的代码:

 Function ConvertDecimal(pInput As String) As Double
'Updateby20140227
 Dim xDeg As Double
 Dim xMin As Double
 Dim xSec As Double
 xDeg = Val(Left(pInput, InStr(1, pInput, "°") - 1))
 xMin = Val(Mid(pInput, InStr(1, pInput, "°") + 2, _
         InStr(1, pInput, "'") - InStr(1, pInput, _
         "°") - 2)) / 60
 xSec = Val(Mid(pInput, InStr(1, pInput, "'") + _
        2, Len(pInput) - InStr(1, pInput, "'") - 2)) _
        / 3600
 ConvertDecimal = xDeg + xMin + xSec
 End Function

How can I drag this formula down to the last row?如何将此公式拖到最后一行?

在此处输入图像描述

在此处输入图像描述

Your order is a little off, fill P before trying to find the last row.您的订单有点偏离,请在尝试找到最后一行之前填写 P。

Also finding the last row in Q is not needed.也不需要在 Q 中找到最后一行。

 Sub Sun()
     Dim rng As Range, cell As Range
     Dim wors As Worksheet
    
     Set wors = ThisWorkbook.ActiveSheet
    
     Dim lastRow As Long
     
     wors.Columns("E").Copy
     wors.Columns("P").PasteSpecial xlPasteValues
    
     lastRow = wors.Range("P" & wors.Rows.Count).End(xlUp).Row
     Set rng = wors.Range("P1:P" & lastRow)
     For Each cell In rng
         cell = WorksheetFunction.Substitute(cell, " ", "° ", 1)
         cell = WorksheetFunction.Substitute(cell, " ", "' ", 2)
     Next
    
   
     Range("Q2:Q" & lastRow).Formula2 = "=ConvertDecimal(P2)"

 End Sub

在此处输入图像描述

But I would use variant arrays to speed it up a little:但我会使用变体 arrays 来加快速度:

 Sub Sun()
     Dim rng As Variant, cell As Range
     Dim wors As Worksheet
    
     Set wors = ThisWorkbook.ActiveSheet
    
     Dim lastRow As Long
     
   
     lastRow = wors.Range("E" & wors.Rows.Count).End(xlUp).Row
     rng = wors.Range("E1:E" & lastRow).Value
     
     Dim outArray() As Variant
     ReDim outArray(1 To UBound(rng, 1), 1 To 2)
     
     outArray(1, 1) = rng(1, 1)
     outArray(1, 2) = "Output"
     
     Dim i As Long
     For i = 2 To UBound(rng, 1)
        rng(i, 1) = WorksheetFunction.Substitute(WorksheetFunction.Substitute(rng(i, 1), " ", "' ", 2), " ", "° ", 1)
        outArray(i, 1) = rng(i, 1)
        outArray(i, 2) = ConvertDecimal(CStr(rng(i, 1)))

     Next
    
     wors.Range("P1").Resize(UBound(outArray, 1), 2).Value = outArray

 End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM