简体   繁体   English

For循环更改与范围Excel VBA有关的公式

[英]For loop to change formula in relation to range excel vba

Wow, I feel like I'm really close on this one. 哇,我觉得我真的很接近这个。

I have this code in a module: 我在模块中有以下代码:

Range("R1400").Formula = "=(ROUND(AC1400, 0))+0.0625"
Range("R1401").Formula = "=(ROUND(AC1401, 0))+0.0625"
Range("R1402").Formula = "=(ROUND(AC1402, 0))+0.0625"
Range("R1403").Formula = "=(ROUND(AC1403, 0))+0.0625"
Range("R1404").Formula = "=(ROUND(AC1404, 0))+0.0625"
Range("R1405").Formula = "=(ROUND(AC1405, 0))+0.0625"
Range("R1406").Formula = "=(ROUND(AC1406, 0))+0.0625"
Range("R1407").Formula = "=(ROUND(AC1407, 0))+0.0625"
Range("R1408").Formula = "=(ROUND(AC1408, 0))+0.0625"
Range("R1409").Formula = "=(ROUND(AC1409, 0))+0.0625"
Range("R1410").Formula = "=(ROUND(AC1410, 0))+0.0625"
Range("R1411").Formula = "=(ROUND(AC1411, 0))+0.0625"
Range("R1412").Formula = "=(ROUND(AC1412, 0))+0.0625"
Range("R1413").Formula = "=(ROUND(AC1413, 0))+0.0625"
Range("R1414").Formula = "=(ROUND(AC1414, 0))+0.0625"
Range("R1415").Formula = "=(ROUND(AC1415, 0))+0.0625"
Range("R1416").Formula = "=(ROUND(AC1416, 0))+0.0625"
Range("R1417").Formula = "=(ROUND(AC1417, 0))+0.0625"
Range("R1418").Formula = "=(ROUND(AC1418, 0))+0.0625"
Range("R1419").Formula = "=(ROUND(AC1419, 0))+0.0625"
Range("R1420").Formula = "=(ROUND(AC1420, 0))+0.0625"
Range("R1421").Formula = "=(ROUND(AC1421, 0))+0.0625"
Range("R1422").Formula = "=(ROUND(AC1422, 0))+0.0625"
Range("R1423").Formula = "=(ROUND(AC1423, 0))+0.0625"
Range("R1424").Formula = "=(ROUND(AC1424, 0))+0.0625"
Range("R1425").Formula = "=(ROUND(AC1425, 0))+0.0625"
Range("R1426").Formula = "=(ROUND(AC1426, 0))+0.0625"
Range("R1427").Formula = "=(ROUND(AC1427, 0))+0.0625"
Range("R1428").Formula = "=(ROUND(AC1428, 0))+0.0625"
Range("R1429").Formula = "=(ROUND(AC1429, 0))+0.0625"

Now, let me be clear, this code works as I'd like it to. 现在,让我清楚一点,这段代码可以按照我的意愿工作。 It might be obvious that I'm taking a range of numbers in one column, rounding them all to the nearest whole integer, then adding a sixteenth, and representing those values in a different column. 显而易见,我在一列中采用了一系列数字,将它们全部四舍五入为最接近的整数,然后加一个十六进制,并在另一列中表示这些值。

however 然而

I feel like this could be 'better' executed in less code, and I'd like to become a more efficient programmer, so I try to write it like so: 我觉得这可以用更少的代码“更好地”执行,并且我想成为一个更有效率的程序员,所以我尝试这样写:

Dim X As Integer
For X = 1400 To 1429
    Range("R" & X).Formula = "=(ROUND(""AC"" "&X", 0))+0.0625"
Next X

But this (and the variations I've tried) all throw '1004' errors or syntax errors. 但这(以及我尝试过的变体)都抛出“ 1004”错误或语法错误。 Most of the changes I've made have had to do with the quotation marks and their placement, and then I've also tried to implement advice from these questions here , here, here, here, and here , but none of them seem to offer quite the solution that I'm looking for, or they don't make sense to me in my inexperience. 大部分我做了有带引号及其位置做了,变化然后我也试图执行这些问题提供咨询意见在这里这里, 这里, 这里,在这里 ,但他们都不来可以提供我一直在寻找的解决方案,否则就我的经验而言,它们对我来说没有意义。

It's possible (likely) that my searches haven't contained the proper verbiage I'm looking for, but that's probably because I'm just now taking my first stab at VBA with this :) 我的搜索可能没有(可能)没有包含我要查找的字词,但这可能是因为我现在正用此方法在VBA进行首次尝试:)

Thanks for any help or references. 感谢您的帮助或参考。 This is my 1st question on StackOverflow, but you folks have definitely taught me darn near everything I know about coding to date. 这是我关于StackOverflow的第一个问题,但是您肯定已经教给我了我迄今为止关于编码的所有知识。

As I mentioned in comments above, you don't need loop, you can simply use: 正如我在上面的评论中所提到的,您不需要循环,您可以简单地使用:

Range("R1400:R1429").Formula = "=(ROUND(AC1400, 0))+0.0625"

Excel would adjust formula properly for each row, so you would have: Excel将为每一行正确调整公式,因此您将:

  • in R1400 formula =(ROUND(AC1400, 0))+0.0625 R1400公式=(ROUND(AC1400, 0))+0.0625
  • in R1401 formula =(ROUND(AC1401, 0))+0.0625 R1401公式=(ROUND(AC1401, 0))+0.0625
  • ..... .....
  • in R1429 formula =(ROUND(AC1429, 0))+0.0625 R1429 =(ROUND(AC1429, 0))+0.0625

You need to cast the X value in the Range function as a string. 您需要将Range函数中的X值转换为字符串。 And you can pass the value of the referenced cell directly to ROUND using the same technique, rather than passing the cell reference. 您可以使用相同的技术将引用单元格的值直接传递给ROUND ,而不是传递单元格引用。 I don't know if that's an improvement, but it was my first instinct. 我不知道这是否有所改善,但这是我的本能。

Range("R" & CStr(X)).Formula = "=(ROUND(" & Range("AC" & CStr(X)).Value & ", 0))+0.0625"

As @simoco has pointed out, you don't have to use the loop at all. 正如@simoco所指出的,您根本不需要使用循环。 But if you still wanted to, or run into similar problems in the future, the above should work. 但是,如果您仍然想要或将来遇到类似的问题,上述方法应该可以解决。

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

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