简体   繁体   English

vba-循环调用函数

[英]vba - Call a function in a loop

I need to call an homemade function (GetCoordinates) in a loop and apply it at each loop step. 我需要在循环中调用一个自制函数(GetCoordinates),并在每个循环步骤中应用它。

I have a column with data (Starting at L2) and I need to apply my personnal function GetCoordinates to this data and write the result on the column next to it (so starting at M2) 我有一列包含数据(从L2开始),我需要将个人功能GetCoordinates应用于此数据,并将结果写在它旁边的列上(因此从M2开始)

NumRows = Range("L2", Range("L2").End(xlDown)).Rows.Count
      Range("L2").Select
      For x = 1 To NumRows
         Selection.Offset(, 1).Formula = "=GetCoordinates(L&x)"
         ActiveCell.Offset(1, 0).Select
      Next

My problem is that I don't know how to apply my function to each cell. 我的问题是我不知道如何将函数应用于每个单元格。 How should it be written so that the function is called for each cell in the loop? 应该如何编写,以便在循环中为每个单元格调用该函数? (The GetCoordinates function works fine) (GetCoordinates函数可以正常工作)

Try putting in all the formulas without a loop. 尝试不加循环地放入所有公式。 To write te formulas in column M and use column L as the formulas argument then use, 要在M列中编写公式并将L列用作公式参数,请使用,

 Range(cells(2, "M"), cells(rows.count, "L").End(xlup).offset(0, 1)).Formula = _
     "=GetCoordinates(L2)"

Your own might have been closer with, 您自己可能已经接近,

Selection.Offset(, 1).Formula = "=GetCoordinates(L" & x & ")"

But you were mismatching Do ... While and For ... Next loops. 但是您不匹配Do ... While和For ... Next循环。

Try this 尝试这个

 NumRows = Range("L" & Rows.Count).End(xlUp).Row

  For x = 2 To NumRows
     Cells(x, "M").Value = GetCoordinates(Cells(x, "L"))
  Next

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

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