|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 testwps 于 2022-10-26 06:27 编辑
共有两个表格,一个是上图中的数据表,是户内信息,可以按照户主或户号区分户内成员。
下图是要填写的表格,把数据表户成员信息填入到登记表中并以户主姓名进行保存,每户单独一个文件。
本人是小白,不会VBA,研究了一天也完全没明白,求大神帮忙。
参考了一位大神给别人的回贴,已解决,附解决代码
- Sub dengji()
- Dim arr, brr, frr(), S$, S1$, mt, mh
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set wb = Workbooks.Open(ThisWorkbook.Path & "\数据\数据.xls")
- brr = Range(("b2"), Cells(Rows.Count, 2).End(xlUp))
- arr = Range("a2").Resize(Cells(Rows.Count, 1).End(xlUp).Row - 1, 8)
- wb.Close False
- S = Join(Application.Transpose(brr), vbTab) & vbTab
- With CreateObject("VBScript.Regexp")
- .Global = True
- .Pattern = "^.*户主.*$"
- For i = 1 To UBound(arr)
- If .test(arr(i, 2)) = True Then
- n = n + 1
- ReDim Preserve frr(1 To n)
- frr(n) = arr(i, 1) & arr(i, 4) & arr(i, 3)
- End If
- Next
- .Pattern = "户主(?:(?!户主).*?\t)+"
- For Each mt In .Execute(S)
- k = k + 1: S1 = mt
- Sheet1.Copy
- .Pattern = ".*?\t"
- x = 0: y = 0
- For Each mh In .Execute(S1)
- j = j + 1: x = x + 1: y = y + 1
- If y = 1 Then
- Cells(x + 3, 2) = arr(j, 1)
- Cells(x + 3, 3) = arr(j, 2)
- Cells(x + 3, 4) = arr(j, 3)
- Cells(x + 3, 5) = arr(j, 4)
- Cells(x + 3, 6) = arr(j, 5)
- Cells(x + 3, 7) = arr(j, 6)
- Cells(x + 3, 8) = arr(j, 7)
- Cells(x + 3, 9) = arr(j, 8)
- Else
- Cells(x + 3, 2) = arr(j, 1)
- Cells(x + 3, 3) = arr(j, 2)
- Cells(x + 3, 4) = arr(j, 3)
- Cells(x + 3, 5) = arr(j, 4)
- Cells(x + 3, 6) = arr(j, 5)
- Cells(x + 3, 7) = arr(j, 6)
- Cells(x + 3, 8) = arr(j, 7)
- Cells(x + 3, 9) = arr(j, 8)
- End If
- Next
- ActiveWorkbook.SaveAs ThisWorkbook.Path & "\登记表" & frr(k) & ".xlsx"
- ActiveWorkbook.Close
- Next
- End With
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|