通过VBA不正确的公式结果

时间:2015-07-07 05:01:03

标签: excel vba excel-vba

我制作了一个简单的程序,将特定的公式放在Excel中。问题是结果中出现了单个反引号。

结果应为='[Fiber Loss Report - 7210 DCN.xlsx]1310'!G9, 我得到的结果是='[Fiber Loss Report - 7210 DCN.xlsx]1310'!'G9'

Sub CopyValues()
Dim n As Integer
Dim y As Integer
Dim rng As Range
y = 6
For n = 9 To 175
    rngText = "D" & y
    Range(rngText).Select
    'rng.Select
    'Range("D6").Select
    formulaText = "='[Fiber Loss Report - 7210 DCN.xlsx]1310'!G" & n
    ActiveCell.FormulaR1C1 = formulaText
    rngText = "E" & y
    Range(rngText).Select
    formulaText = "='[Fiber Loss Report - 7210 DCN.xlsx]1310'!G" & n + 1
    ActiveCell.FormulaR1C1 = formulaText
    n = n + 2
    y = y + 1
Next
End Sub

请告诉我如何避免这些单引号的出现。

4 个答案:

答案 0 :(得分:3)

继我的评论下面你的问题......

您需要在代码中找到趋势。这样你可以避免循环。

例如,从D6D61的公式从G9增加到G12再增加到G15,依此类推。对于Col E也是如此。

所以有一种趋势。您可以问自己的下一个问题是“我可以在哪里找到可以得到9,12,15,18等结果的公式?”。公式是

=(ROW()-3)*3

如果您将该公式放在G6中并将其拉下来,您将获得该序列。

现在也可以代替循环我们可以将公式写入整个范围。因此,我们只需将上述内容与Indirect结合起来。见这个

最短的代码?

Sub CopyValues()
    Dim ws As Worksheet
    Dim fName As String

    fName = "Fiber Loss Report - 7210 DCN.xlsx"
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '"='[Fiber Loss Report - 7210 DCN.xlsx]1310'!G9"
        .Range("D6:D61").Formula = "=INDIRECT(" & Chr(34) & "'[" & _
                                   fName & "]1310'!G" & Chr(34) & _
                                   " & (ROW()-3)*3)"

        .Range("E6:E61").Formula = "=INDIRECT(" & Chr(34) & "'[" & _
                                   fName & "]1310'!G" & Chr(34) & _
                                   " & (ROW()-3)*3+1)"
    End With
End Sub

只是为了好玩

我可以只用两行转换上面的代码。只需将Sheet1替换为相关的工作表名称。

Sub CopyValues()
    ThisWorkbook.Sheets("Sheet1").Range("D6:D61").Formula = "=INDIRECT(" & Chr(34) & "'[Fiber Loss Report - 7210 DCN.xlsx]1310'!G" & Chr(34) & " & (ROW()-3)*3)"
    ThisWorkbook.Sheets("Sheet1").Range("E6:E61").Formula = "=INDIRECT(" & Chr(34) & "'[Fiber Loss Report - 7210 DCN.xlsx]1310'!G" & Chr(34) & " & (ROW()-3)*3+1)"
End Sub

答案 1 :(得分:2)

Option Explicit

Sub CopyValues()

    Dim i As Long, j As Long

    j = 6
    For i = 9 To 175 Step 3
        Range("D" & j).Formula = "='[Fiber Loss Report - 7210 DCN.xlsx]1310'!G" & i
        Range("E" & j).Formula = "='[Fiber Loss Report - 7210 DCN.xlsx]1310'!G" & i + 1
        j = j + 1
    Next

End Sub

结果:=' [光纤损耗报告 - 7210 DCN.xlsx] 1310'!G9

答案 2 :(得分:1)

您可以删除单引号并更改为RC格式:

Sub CopyValues()
 Dim n As Integer
 Dim y As Integer
 Dim rng As Range
 y = 6
 For n = 9 To 175
    rngText = "D" & y
    Range(rngText).Select
    'rng.Select
    'Range("D6").Select
    formulaText = "=[Fiber Loss Report - 7210 DCN.xlsx]1310!R" & n & "C7"        
    ActiveCell.FormulaR1C1 = formulaText
    rngText = "E" & y
    Range(rngText).Select
    formulaText = "=[Fiber Loss Report - 7210 DCN.xlsx]1310!R" & n + 1 & "C7"        
    ActiveCell.FormulaR1C1 = formulaText

    n = n + 2
    y = y + 1
Next
End Sub

答案 3 :(得分:0)

使用.value代替.FormulaR1C1可以解决问题。 :)