Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel “如何替换”;S";在一个公式中,能有一个变量而不是这个字母S吗?_Excel_Vba_Sum - Fatal编程技术网

Excel “如何替换”;S";在一个公式中,能有一个变量而不是这个字母S吗?

Excel “如何替换”;S";在一个公式中,能有一个变量而不是这个字母S吗?,excel,vba,sum,Excel,Vba,Sum,我认为问题很简单,答案可能不是 Sheet1.Cells().Value = "=SUM(E" & i & ":S" & i & ")/" & n 简而言之,我想用一个变量(从E到AB,我不停到Z!)来替换字母S,以便能够将这个简单的公式放在一系列单元格中。 在写这个问题时,我正在考虑使用数组。我想我会朝这个方向看。但是,如果你有一个更简单的答案,不要犹豫。 谢谢。对我来说,显而易见的答案是:

我认为问题很简单,答案可能不是

Sheet1.Cells().Value = "=SUM(E" & i & ":S" & i & ")/" & n
简而言之,我想用一个变量(从E到AB,我不停到Z!)来替换字母S,以便能够将这个简单的公式放在一系列单元格中。 在写这个问题时,我正在考虑使用数组。我想我会朝这个方向看。但是,如果你有一个更简单的答案,不要犹豫。
谢谢。

对我来说,显而易见的答案是:

Dim ColEnd As String

ColEnd = "X"    ' X is anything between E and AB
Sheet1.Cells().Value = "=SUM(E" & i & ":" & ColEnd & i & ")/" & n
我错过什么了吗

如果
ColEnd
需要是一个数字,这将执行转换:

Public Function ColNumToCode(ByVal ColNum As Long) As String

  Dim ColCode As String
  Dim PartNum As Long

  '   2004?  Originally written for two character codes for Excel 2003.
  '          Date not recorded.
  '  3Feb12  Adapted to handle three character codes.
  '  5Jul20  I coded the routine below before I properly understood the Range
  '          property Address. Once I understood it, I assumed the following
  '          would be faster:
  '             Dim Addr As String
  '             Addr = Cells(1, ColNum).Address
  '             ColNumToCode = Mid(Addr, 2, Len(Addr) - 3)
  '          However, when I timed the two approaches, I found:
  '            * the Address property technique was 6.0 times slower
  '              than the code below for columns 1 to 26 (A to Z)
  '            * the Address property technique was 3.8 times slower
  '              than the code below for columns 27 to 702 (AA to ZZ)
  '            * the Address property technique was 2.9 times slower
  '              than the code below for columns 703 to 16834 (AAA to XFD)

  If ColNum = 0 Then
    Debug.Assert False
    ColNumToCode = "0"
  Else
    ColCode = ""
    Do While ColNum > 0
      PartNum = (ColNum - 1) Mod 26
      ColCode = Chr(65 + PartNum) & ColCode
      ColNum = (ColNum - PartNum - 1) \ 26
    Loop
  End If

  ColNumToCode = ColCode

End Function

对我来说,显而易见的答案是:

Dim ColEnd As String

ColEnd = "X"    ' X is anything between E and AB
Sheet1.Cells().Value = "=SUM(E" & i & ":" & ColEnd & i & ")/" & n
我错过什么了吗

如果
ColEnd
需要是一个数字,这将执行转换:

Public Function ColNumToCode(ByVal ColNum As Long) As String

  Dim ColCode As String
  Dim PartNum As Long

  '   2004?  Originally written for two character codes for Excel 2003.
  '          Date not recorded.
  '  3Feb12  Adapted to handle three character codes.
  '  5Jul20  I coded the routine below before I properly understood the Range
  '          property Address. Once I understood it, I assumed the following
  '          would be faster:
  '             Dim Addr As String
  '             Addr = Cells(1, ColNum).Address
  '             ColNumToCode = Mid(Addr, 2, Len(Addr) - 3)
  '          However, when I timed the two approaches, I found:
  '            * the Address property technique was 6.0 times slower
  '              than the code below for columns 1 to 26 (A to Z)
  '            * the Address property technique was 3.8 times slower
  '              than the code below for columns 27 to 702 (AA to ZZ)
  '            * the Address property technique was 2.9 times slower
  '              than the code below for columns 703 to 16834 (AAA to XFD)

  If ColNum = 0 Then
    Debug.Assert False
    ColNumToCode = "0"
  Else
    ColCode = ""
    Do While ColNum > 0
      PartNum = (ColNum - 1) Mod 26
      ColCode = Chr(65 + PartNum) & ColCode
      ColNum = (ColNum - PartNum - 1) \ 26
    Loop
  End If

  ColNumToCode = ColCode

End Function


使用
formula1c1
属性通过数字确定公式,您将能够创建一个数组。例如,您将引用单元格
R1C5
,而不是公式中的单元格
E1
。此外,您需要将公式中的
i
更改为
CStr(i)
,以便能够连接字符串。

使用
公式1c1
属性通过数字确定公式,您将能够创建数组。例如,您将引用单元格
R1C5
,而不是公式中的单元格
E1
。此外,您需要将公式中的
i
更改为
CStr(i)
,以便能够连接字符串。

是的,我最终使用数组col=array(“E”,“F”)执行了类似的操作,…,col是一个变体而不是您的ColEnd。我喜欢函数的想法,我想您指的是ColNumToCode而不是NewColNumToCode。带有公式1c1的想法听起来不错,但我无法让它工作。@JLuc01抱歉,是的
NewColNumToCode
应该是
ColNumToCode
。实际上,这并不像我想象的那样是一个好的例行程序应该。我多年来一直使用不同的例程来执行此转换,但我一直认为此方法会更快。我在发布之前将其编码为
NewColNumToCode
,以测试它是否工作,并且在删除
新的
方面做得很差。此后,我对其进行了计时,并且使用属性
地址
比在VBA中编写转换代码。如果您愿意,我将发布一个更快的例程。我可以看到,如果列数较少,将“E”数组转换为“AB”可能会更快。但是如果您希望将列16384转换为“XFD”,该怎么办?我对我得到的没问题,但是如果你有一个更快更好的例程,我会非常高兴看到它,总是很高兴了解更多。@JLuc01我在回答中包括了我的更快例程,以及显示它是更快技术的计时。我在我的PERSONAL.XLXB中有这个例程,所以我的所有工作簿都可以使用它。a在您的有限情况下,n array可能更快,但对我来说,它将是定制代码。我在数百个宏中使用过此例程。我避免使用定制代码,因为我重视创建新代码的时间,而不是节省几毫秒的运行时。是的,我终于用数组col=array(“E”,“F”)做了类似的事情,…,col是一个变体而不是您的ColEnd。我喜欢函数的想法,我想您指的是ColNumToCode而不是NewColNumToCode。带有公式1c1的想法听起来不错,但我无法让它工作。@JLuc01抱歉,是的
NewColNumToCode
应该是
ColNumToCode
。实际上,这并不像我想象的那样是一个好的例行程序应该。我多年来一直使用不同的例程来执行此转换,但我一直认为此方法会更快。我在发布之前将其编码为
NewColNumToCode
,以测试它是否工作,并且在删除
新的
方面做得很差。此后,我对其进行了计时,并且使用属性
地址
比在VBA中编写转换代码。如果您愿意,我将发布一个更快的例程。我可以看到,如果列数较少,将“E”数组转换为“AB”可能会更快。但是如果您希望将列16384转换为“XFD”,该怎么办?我对我得到的没问题,但是如果你有一个更快更好的例程,我会非常高兴看到它,总是很高兴了解更多。@JLuc01我在回答中包括了我的更快例程,以及显示它是更快技术的计时。我在我的PERSONAL.XLXB中有这个例程,所以我的所有工作簿都可以使用它。a在您有限的情况下,n array可能更快,但对我来说,它将是定制代码。我在数百个宏中使用过此例程。我避免使用定制代码,因为我重视创建新代码的时间,而不是节省几毫秒的运行时。这个想法听起来不错,但我无法让它工作。显然,Sheet3.Cells(I,iNR+5).FormulaR1C1=“=SUM(R[”&i&“]C[5]:R[”&i&“]C[”&iNR-1&“])/“&n不起作用,但是Sheet3.Cells(i,iNR+5)。Value=“=SUM(E“&i&:”&col(iNR-1)&i&“/”&n起作用。我在编码的第一行上看不到语法错误。尽管将
i
更改为
CStr(i)可能更清楚)
,这是不必要的。如果可能,VBA将执行隐式转换。如果表达式需要,整数将转换为字符串、单个或布尔值。您还可以编写
I=I+“5”
和“5”将转换为整数。隐式转换对程序员非常有帮助,但您必须小心。如果将数值变量定义为字符串,则不会收到警告,解释器将根据需要来回转换。这会起作用,但速度会很慢。请注意,括号用于相对公式,请在我会给你绝对公式。例如,
Cells(3,7)。FormulaR1C1=“=sqrt(R[1]C[-1])”
相当于
Cells(3,7)。FormulaR1C1=“=sqrt(R4C6)”
同样,尽管在某些情况下不会将
i
更改为
CStr(i)
工作,但在某些情况下不会工作