使用VB.NET合并excel单元格问题

时间:2015-08-08 10:53:00

标签: vb.net excel

大家好,谢谢! 我有一些奇怪的例外,我无法解决。

虽然我无法理解,但这不是我的第一个节目!

我正在编写一个应用程序,我希望填写一个excel工作簿。 每当我试图合并单元格时,我都会收到错误。 我一遍又一遍地检查,我无法弄明白。 我的代码中的所有内容都应该有效。 所有声明的xl应用程序,工作簿,工作表和范围。 最奇怪的是,如果在代码运行期间,我手动输入excel工作表(在用msgbox或其他东西暂停程序之后),以及 选择一些单元格(无论哪个),代码运行得很好:((

我没有大声添加图片,所以这里是代码 - 复制粘贴。

btn点击代码:

Private Sub btnCreatPriceQuote_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreatPriceQuote.Click

    oXL.Visible = False

    Dim indexForCopy As Integer

    'few commands for faster run in the xl WB
    XlFasterRun()

    'Replace pricequote sheet with its format to avoid overwrite
    PriceQuoteRenew()

    'Decalre rows for add details on the BAMAY
    DecalreFirstRow()

    FillPriceQuoteTable()


    TillIndex = 39
    EmptyCellIndex = 13

来自模块的声明:

`Public Sub DecalreFirstRow()

    rng1thcol1thLinePQ = shPriceQuote.Range("A75:D75")
    rng2thcol1thLinePQ = shPriceQuote.Range("E75:F75")
    rng3thcol1thLinePQ = shPriceQuote.Range("G75:H75")
    rng4thcol1thLinePQ = shPriceQuote.Range("I75:J75")
    rng5thcol1thLinePQ = shPriceQuote.Range("K75:L75")
    rng6thcol1thLinePQ = shPriceQuote.Range("M75:N75")
    rng7thcol1thLinePQ = shPriceQuote.Range("O75:T75")
    rng8thcol1thLinePQ = shPriceQuote.Range("U75:X75")

    rng1thcol1thLineCF = shCusFile.Range("A12:G12")
    rng2thcol1thLineCF = shCusFile.Range("H12:K12")
    rng3thcol1thLineCF = shCusFile.Range("L12:O12")
    rng4thcol1thLineCF = shCusFile.Range("P12:S12")
    rng5thcol1thLineCF = shCusFile.Range("T12:W12")
    rng6thcol1thLineCF = shCusFile.Range("X12:AA12")
    rng7thcol1thLineCF = shCusFile.Range("AD12:AI12")
    rng8thcol1thLineCF = shCusFile.Range("AJ12:AN12")

End Sub

`   发生异常的公共子:

Public Sub FillPriceQuoteTable()

    With rng1thcol1thLinePQ
        .Select()
        .Value = rng1thcol1thLineCF.Value
        **.Merge()** 
    End With

    With rng2thcol1thLinePQ
        .Select()
        **.Merge()**
        .Value = rng2thcol1thLineCF.Value
    End With

    With rng3thcol1thLinePQ
        .Select()
        **.Merge()**
        .Value = rng3thcol1thLineCF.Value
    End With

    With rng4thcol1thLinePQ
        .Select()
        **.Merge()**
        .Value = rng4thcol1thLineCF.Value
    End With

1 个答案:

答案 0 :(得分:0)

替换:

rng.Merge()

使用:

rng.mergecells=true.