请问大虾,在VB中怎样用代码合并EXCEL的单元格?

yt2110362 2004-07-26 10:35:46
请问大虾们,再VB中可以用代码合并EXCEL的单元格吗???
...全文
577 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinweihong 2004-07-26
  • 打赏
  • 举报
回复
eg:在excel中随便录制一宏,将其中代码换为以下 看效果~ 融会贯通!
For i = 4 To 20
ActiveSheet.Range(ActiveSheet.Cells(i, 7), ActiveSheet.Cells(i, 11)).Select

With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Selection.Merge
Next i
End Sub
northwolves 2004-07-26
  • 打赏
  • 举报
回复
Option Explicit
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlsheet As Excel.Worksheet '定义工作表类
Private Sub Command1_Click()
Set xlApp = CreateObject("excel.application.10")
xlApp.Visible = True
xlApp.Workbooks.Add
Set xlsheet = xlApp.Worksheets(1)
xlsheet.Range(Cells(2, 2), Cells(3, 5)).Merge
xlsheet.Range(Cells(2, 2), Cells(3, 5)).Borders.LineStyle = xlContinuous
Set xlsheet = Nothing
Set xlApp = Nothing
End Sub
online 2004-07-26
  • 打赏
  • 举报
回复
参考一个收集的资料

我做一个报表,打印到EXCEL中,我想问的问题不知道各位能否帮我解答一下:
1,怎样在VB中通过编程把EXCEL几行或几列合并,并且使其中的内容居中。
2,怎样在VB中通过编程在EXCEL中划线,
3,怎样在VB中编程实现EXCEL单元格有边框,
也就是打印到EXCEL中不要再重新排版(客户要求这样)。
因为打印到EXCEL中行列不规则,有的行列需要边框,有的不需要,有的还需要在一行下面划一条线---------------------------------------------------------------

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

With Rs_Dzgl_Receipt
If .RecordCount < 1 Then
MsgBox ("没有记录!")
Exit Sub
End If
xlSheet.Cells(1, 4).Value = .Fields("bt")
xlSheet.Cells(2, 1).Value = .Fields("invoice")
xlSheet.Cells(2, 9).Value = .Fields("packdate")
xlSheet.Cells(3, 1).Value = .Fields("mark")

'合并单元格
Dim nIcol As Integer

xlSheet.Range(xlSheet.Cells(3, 1), xlSheet.Cells(5, 9)).Select
With xlApp.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With

xlSheet.Range(xlSheet.Cells(1, 1), xlSheet.Cells(1, 9)).Select
With xlApp.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
'网格线
With xlSheet
.Range(.Cells(1, 1), .Cells(1, 9)).Font.Name = "黑体"
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, 9)).Font.Bold = True
'标题字体加粗
.Range(.Cells(1, 1), .Cells(1, 9)).Borders.LineStyle = xlContinuous
'设表格边框样式
End With

'显示表格
Dim ExclFileName As String
ExclFileName = App.Path & "\箱单" & Text1(1).Text & ".xls"
If Dir(ExclFileName) <> "" Then
Kill ExclFileName
End If
xlSheet.SaveAs (ExclFileName)
xlApp.Application.Visible = True
'交还控制给Excel
xlSheet.PrintPreview
' xlApp.Application.Quit
' xlApp.Quit
End With

---------------------------------------------------------------

你先学学怎么用EXCEL里面的宏,那宏都是VBA写的,在试试做报表
---------------------------------------------------------------

用excel的录制宏,然后操作成你想要的报表格式,再停止宏,再查看vb编辑器里的代码,稍微修改一下就可以用
---------------------------------------------------------------

注意,宏是问题的关键!

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧