使用变量与VBA的公式

时间:2017-11-15 23:15:42

标签: excel vba excel-formula

我正在努力解决一些代码的这一行的语法问题。带有“IfError”的公式似乎有错误的语法。我相信我在正确的地方有引号。

class TestEmailSvc
  def send_many_async(email, to_email_users, from_email_user: nil, send_at: nil)
    puts 'Real one called!'
  end
end

class TestEmail
end

class TestEmailUser
  attr_reader :email
  def initialize(email)
    @email = email
  end
end

class TestEmailResult
  attr_reader :user
  def initialize(user)
    @user = user
  end

  def status
    :pending
  end
end

describe 'Test Spec' do

  it 'tests my mock' do
    allow_any_instance_of(TestEmailSvc)
      .to receive(:send_many_async)
        .and_wrap_original { |m, _, args| args.map { |recipient| TestEmailResult.new(recipient) } }

    svc = TestEmailSvc.new

    users = [
      TestEmailUser.new('email 1'),
      TestEmailUser.new('email 2')
    ]

    results = svc.send_many_async(TestEmail.new, users)

    expect(results.count).to eql 2
  end

end

3 个答案:

答案 0 :(得分:1)

尝试this

 Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE &  CustomerLast & ", """")"

字符串文字中的引号必须被转义 - 这是通过将它们加倍来完成的。

答案 1 :(得分:1)

您还可以同时指定多个公式:

Range("AN1:AN300").Formula = "=IFERROR(AM1 / AM$" &  CustomerLast & ", """")"
Range("AO1:AO300").Formula = "=AO1 * S$" & CustomerLast

我还建议您查看Excel表格和Structured References

要避免循环引用问题,您可以计算公式并直接指定值:

Range("AN1:AN300") = Evaluate("IFERROR(AM1:AM300 / AM$" &  CustomerLast & ", """")")
Range("AO1:AO300") = Evaluate("INDEX(AO1:AO300 * S$" & CustomerLast & ",)")

答案 2 :(得分:0)

它在这里结束

& ", "")"

逗号和之间的两个引号结束字符串并开始一个新字符串。

'first string
","

'second string
")"

因为你不能像这样把两个字符串放在一起,我会用CHR等价物替换两个引号。应该看起来像这样:

Cells(j, Dist).Formula = "=IFERROR(" & MidPointE & j & " / " & MidPointE &  CustomerLast & ", "& CHR(034) & CHR(034) & ")"
'pretty sure 034 is the ascii code for "