Excel vba报告

我正在build立一个VB6的Excel报告。 我正在做的是穿过一个logging集,并插入文本到一个单元格。 我试图看看是否有办法让我dynamic地合并中心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的值合并,并将它居中放置在两个单元格中,所以它下面的2列看起来像属于标签 – 因为我插入了很多标签,我希望它看起来有些专业。

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

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

所以我包括这下面的LABEL,它给我一个错误“ 方法范围的对象工作表失败

 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 

Range()方法接受一个string参数或其他有效的Range对象。 它看起来像xlRowxlCol是无法提供给Range()方法的数值。

尝试更改为Cells() ,它们分别为rowcolumn数字参数。

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

此外,不需要以这种方式select对象,因为您可以直接访问其属性和方法。 考虑到这一点,你的代码可以被重写为:

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