代码之家  ›  专栏  ›  技术社区  ›  FatBoySlim7

Excel vba报表

  •  1
  • FatBoySlim7  · 技术社区  · 9 年前

    我正在用vb6构建一个excel报表。我正在做的是浏览记录集并将文本插入单元格。我想看看是否有一种方法可以动态合并中心2单元格,并在其周围放置边框。这就是我的代码的样子。。。。

    Do While Not g_RS.EOF
        xlSheetInsurance.Cells(xlRow, xlCol).Value = g_RS("Label")
        xlSheetInsurance.Cells(xlRow + 1, xlCol).Value = " Count Sales "
        xlSheetInsurance.Cells(xlRow + 1, xlCol + 1).Value = "Count Buys "
         xlCol = xlCol + 2
        g_RS.MoveNext
     Loop
    

    因此,“标签”会插入到其他列中。在标签下面,我插入了COUNT SALES和COUNT BUYS,所以基本上我试图取label的值,合并并将其居中于两个单元格上,使其下面的两列看起来像是属于标签的-因为我插入了很多标签,所以我希望它看起来有点专业。

    编辑: 我创建了宏,但我似乎做错了什么

    xlSheetInsurance.Cells(xlRow, xlCol).Value = g_RS("Label")
    xlSheetInsurance.Range(xlCol, xlCol + 1).Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
    End with
    

    所以我把这个放在标签下面,它给了我一个错误” 对象工作表的方法范围失败 "

    2 回复  |  直到 9 年前
        1
  •  1
  •   Tim Williams    9 年前
    Do While Not g_RS.EOF
        With xlSheetInsurance.Cells(xlRow, xlCol)
    
            .Value = g_RS("Label")
            .Offset(1, 0).Value = " Count Sales "
            .Offset(1, 1).Value = "Count Buys "
    
            With .Resize(1, 2)
                .Merge
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .Borders.Weight = xlThin
            End With
    
        End With
    
        xlCol = xlCol + 2
        g_RS.MoveNext
    Loop
    
        2
  •  0
  •   SierraOscar    9 年前

    这个 Range() 方法接受字符串参数或其他有效参数 Range 物体。看起来像是 xlRow xlCol 是无法提供给 范围() 方法

    尝试更改为 Cells() 相反,它接受的是 row column 分别地

    xlSheetInsurance.Cells(xlCol, xlCol + 1).Select
    

    此外,不需要以这种方式选择对象,因为您可以直接访问其属性和方法。记住这一点,您的代码可以重新编写为:

    With xlSheetInsurance.Cells(xlRow, xlCol)
        .Value = g_RS("Label")
        With .Offset(1, 0)
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
        End With
    End with