|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub to_barcode()
- Dim wb As Workbook
- Dim ws_split, ws_barcode As Worksheet
- Dim lastRow As Long, lastColumn, nonEmptyCount, numGroups As Long
- Dim cell, rng As Range
- Dim arr(), brr() As Variant
-
- Set wb = ThisWorkbook
- Set ws_split = wb.Sheets("拆分")
- Set ws_barcode = wb.Sheets("条形码")
-
- lastRow = ws_split.Cells(ws_split.Rows.Count, 1).End(xlUp).Row
- lastColumn = ws_split.Cells(1, ws_split.Columns.Count).End(xlToLeft).Column
-
- Set rng = ws_split.Range(Cells(2, 1), Cells(lastRow, lastColumn))
-
- '计算使用区域内非空单元格数量
- nonEmptyCount = 0
- For Each cell In rng
- If Not IsEmpty(cell) Then
- nonEmptyCount = nonEmptyCount + 1
- End If
- Next cell
- '用非空单元格数量减去外箱码数量,再除以3算出有多少个条码
- numGroups = (nonEmptyCount - lastRow + 1) / 3
- '条码数加上外箱码算出第2个表中数据的行数
- rowBarcode = lastRow - 1 + numGroups
- '重定义数组brr用于存放拆分表处理后的数据
- ReDim brr(1 To rowBarcode, 1 To 3)
-
- '将拆分表中的数据部分赋值给数组arr
- 'arr = ws_split.Range(Cells(2, 1), Cells(lastRow, lastColumn)).Value
- arr = rng.Value
-
- m = 0
- For i = LBound(arr, 1) To UBound(arr, 1)
- m = m + 1
- brr(m, 1) = Replace(arr(i, 1), "-", "--")
- For j = 2 To UBound(arr, 2) Step 3
- If arr(i, j) <> "" Then
- m = m + 1
- brr(m, 1) = arr(i, j)
- brr(m, 2) = arr(i, j + 1)
- brr(m, 3) = arr(i, j + 2)
- End If
- Next j
- Next i
-
- '将处理好的拆分表数据复制到条形码表
- ws_barcode.Cells.Clear
- ws_barcode.Range("A1").Resize(rowBarcode, 3) = brr
-
-
- '调整格式
- lastRow = ws_barcode.Cells(ws_barcode.Rows.Count, 1).End(xlUp).Row
- ws_barcode.Range(Cells(1, 1), Cells(lastRow, 3)).Font.Name = "微软雅黑"
- ws_barcode.Range(Cells(1, 1), Cells(lastRow, 3)).Font.Size = 12
- For i = 1 To lastRow
- If ws_barcode.Cells(i, 2).Value = "" Then
- ws_barcode.Range("A" & i).Font.Bold = True
- Else
- ws_barcode.Range("A" & i & ":C" & i).Borders.LineStyle = 1
- ws_barcode.Range("A" & i).NumberFormatLocal = "0_);[红色](0)"
- End If
- ws_barcode.Range("A" & i).HorizontalAlignment = xlHAlignLeft
- ws_barcode.Range("B" & i).HorizontalAlignment = xlHAlignLeft
- ws_barcode.Range("C" & i).HorizontalAlignment = xlHAlignCenter
- Next i
- End Sub
复制代码
时间有限写了个从拆分表到条形码的 |
评分
-
1
查看全部评分
-
|