Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2949|回复: 2

[已解决]碰上一个高难度的问题,自动生成房号

[复制链接]
发表于 2013-1-15 16:19 | 显示全部楼层 |阅读模式
求助,我们公司让用VBA做一个程序,可以按照需要自动生成一些数据,比如说,若设置楼幢数为5,则自动生成5个工作表并分别命名1号楼、2号楼……5号楼,然后每幢楼设置几层、几个单元、每单元几户后自动生上面这个格式,房号中1-代表1号楼、1-1-代表1号楼一单元、1-1-101代表1号楼一单元01户,我的这个表是29层1个单元四户的,如果是两个单元,就需要在右边再加四户,这个用VBA该怎么做啊?
最佳答案
2013-1-15 20:06
  1. Option Explicit

  2. Sub GenNo()
  3. Dim Bs As Integer, Dep As Integer, Floors As Integer, H As Integer, i As Integer, j As Integer, k As Integer, arr, WB As Workbook, sh As Worksheet
  4. Bs = Range("B1")
  5. Dep = Range("B2")
  6. Floors = Range("B3")
  7. H = Range("B4")
  8. ReDim arr(1 To Floors + 2, 1 To Dep * H * 5)
  9. Set WB = Workbooks.Add
  10. For i = 1 To Bs
  11.     For j = 1 To Dep * H
  12.         arr(1, j * 5 - 4) = IIf(j Mod H, j Mod H, H)
  13.         arr(2, j * 5 - 4) = "房号"
  14.         arr(2, j * 5 - 3) = "附加价格位"
  15.         arr(2, j * 5 - 2) = "单位"
  16.         arr(2, j * 5 - 1) = "面积"
  17.         arr(2, j * 5) = "总价"
  18.         For k = Floors To 1 Step -1
  19.             arr(Floors - k + 3, j * 5 - 4) = "'" & i & "-" & (j - 1) \ H + 1 & "-" & k & Format(IIf(j Mod H, j Mod H, H), "00")
  20.         Next k
  21.     Next j
  22.     With WB.Sheets.Add(, Sheets(Sheets.Count))
  23.         .Name = i & "号楼"
  24.         With .Range("A1:E" & UBound(arr))
  25.             .HorizontalAlignment = xlCenter
  26.             .Borders(xlInsideVertical).LineStyle = xlContinuous
  27.             .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  28.             With .Borders(xlEdgeTop)
  29.                 .LineStyle = xlContinuous
  30.                 .Weight = xlMedium
  31.             End With
  32.             With .Borders(xlEdgeBottom)
  33.                 .LineStyle = xlContinuous
  34.                 .Weight = xlMedium
  35.             End With
  36.             With .Borders(xlEdgeRight)
  37.                 .LineStyle = xlContinuous
  38.                 .Weight = xlMedium
  39.             End With
  40.             .Range("A1:E1").HorizontalAlignment = xlCenterAcrossSelection
  41.             .Copy
  42.         End With
  43.         With .Range("A1").Resize(UBound(arr, 1), UBound(arr, 2))
  44.             .Value = arr
  45.             .PasteSpecial Paste:=xlPasteFormats
  46.         End With
  47.         Application.CutCopyMode = False
  48.     End With
  49. Next i
  50. Application.DisplayAlerts = False
  51. For Each sh In WB.Sheets
  52.     If sh.Cells(1, 1) = "" Then sh.Delete
  53. Next
  54. Application.DisplayAlerts = True
  55. End Sub
复制代码
求助,自动生成房号.zip (18.06 KB, 下载次数: 6, 售价: 1 个金币)

求助,自动生成房号.rar

9.77 KB, 下载次数: 39

发表于 2013-1-15 20:06 | 显示全部楼层    本楼为最佳答案   
  1. Option Explicit

  2. Sub GenNo()
  3. Dim Bs As Integer, Dep As Integer, Floors As Integer, H As Integer, i As Integer, j As Integer, k As Integer, arr, WB As Workbook, sh As Worksheet
  4. Bs = Range("B1")
  5. Dep = Range("B2")
  6. Floors = Range("B3")
  7. H = Range("B4")
  8. ReDim arr(1 To Floors + 2, 1 To Dep * H * 5)
  9. Set WB = Workbooks.Add
  10. For i = 1 To Bs
  11.     For j = 1 To Dep * H
  12.         arr(1, j * 5 - 4) = IIf(j Mod H, j Mod H, H)
  13.         arr(2, j * 5 - 4) = "房号"
  14.         arr(2, j * 5 - 3) = "附加价格位"
  15.         arr(2, j * 5 - 2) = "单位"
  16.         arr(2, j * 5 - 1) = "面积"
  17.         arr(2, j * 5) = "总价"
  18.         For k = Floors To 1 Step -1
  19.             arr(Floors - k + 3, j * 5 - 4) = "'" & i & "-" & (j - 1) \ H + 1 & "-" & k & Format(IIf(j Mod H, j Mod H, H), "00")
  20.         Next k
  21.     Next j
  22.     With WB.Sheets.Add(, Sheets(Sheets.Count))
  23.         .Name = i & "号楼"
  24.         With .Range("A1:E" & UBound(arr))
  25.             .HorizontalAlignment = xlCenter
  26.             .Borders(xlInsideVertical).LineStyle = xlContinuous
  27.             .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  28.             With .Borders(xlEdgeTop)
  29.                 .LineStyle = xlContinuous
  30.                 .Weight = xlMedium
  31.             End With
  32.             With .Borders(xlEdgeBottom)
  33.                 .LineStyle = xlContinuous
  34.                 .Weight = xlMedium
  35.             End With
  36.             With .Borders(xlEdgeRight)
  37.                 .LineStyle = xlContinuous
  38.                 .Weight = xlMedium
  39.             End With
  40.             .Range("A1:E1").HorizontalAlignment = xlCenterAcrossSelection
  41.             .Copy
  42.         End With
  43.         With .Range("A1").Resize(UBound(arr, 1), UBound(arr, 2))
  44.             .Value = arr
  45.             .PasteSpecial Paste:=xlPasteFormats
  46.         End With
  47.         Application.CutCopyMode = False
  48.     End With
  49. Next i
  50. Application.DisplayAlerts = False
  51. For Each sh In WB.Sheets
  52.     If sh.Cells(1, 1) = "" Then sh.Delete
  53. Next
  54. Application.DisplayAlerts = True
  55. End Sub
复制代码
求助,自动生成房号.zip (18.06 KB, 下载次数: 6, 售价: 1 个金币)
回复

使用道具 举报

 楼主| 发表于 2013-1-15 21:49 | 显示全部楼层
老师您真神了,十分感谢十分感谢,正是我要的样子,老师能帮我解释一下代码吗?我想好好学习一下,无论如何真是太谢谢你了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-17 00:04 , Processed in 0.889758 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表