excel - 附加 A 列的最后 4 个字符并添加到 B 列
问题描述
我正在尝试从组合表中的 A 列中提取最后 4 个字符并将其添加到新的 B 列中,但我的代码的最后一部分不起作用。
合并文件
列添加了“发票号”
从发票号码中提取最后 4 个字符
Sub test() Dim fn, ws As Worksheet, e, flg As Boolean, LastR As Range, wsName As String fn = Application.GetOpenFilename("Excel(*.csv*),*.csv*", MultiSelect:=True) If Not IsArray(fn) Then Exit Sub Set ws = ActiveWorkbook.Sheets.Add ws.Name = "Combined" Application.ScreenUpdating = False ws.Cells.Clear For Each e In fn With Workbooks.Open(e) With .Sheets(1) wsName = .Name If Not flg Then .Rows(1).Copy ws.Cells(1) ws.Columns(1).Insert ws.Cells(1).Value = "Invoice number" flg = True End If Set LastR = ws.Cells(Rows.Count, 2).End(xlUp)(2) With .Range("a1").CurrentRegion With .Resize(.Rows.Count - 1).Offset(1) .Copy LastR LastR(, 0).Resize(.Rows.Count).Value = _ CreateObject("Scripting.FileSystemObject").GetBasename(e) End With End With End With .Close False End With Next ws.Range("a1").CurrentRegion.Columns.AutoFit Application.ScreenUpdating = True Set ws = Nothing Worksheets("Combined").Activate With ActiveSheet Dim LastRow As Long Dim i As Long LastRow = Range("A" & Rows.Count).End(xlUp).Row Columns("B:B").Insert Shift:=xlToRight, _ CopyOrigin:=xlFormatFromLeftOrAbove For i = 2 To LastRow Range("B" & i).Value = Right(Range("A" & i).Value, 4) Next i End With End Sub
解决方案
推荐阅读
- python - 使用由随机像素组成的图像生成与 CIFAR10 相同大小的训练集
- gradle - 通过 Bitbucket Pipeline 使用 Gradle 构建
- python - 关于 Epoch 问题的梯度下降成本图
- c++ - 如何使用 OpenGL 在 Gtk+ 中进行 3D 渲染?
- python - 如何在 Python 中将一个文件中的值匹配到另一个文件
- c++ - 我们可以在不使用友元函数的情况下重载 '<<' 运算符吗?
- python-3.x - 无法在 Windows 中使用 Python 3.9 创建虚拟环境
- matlab - 如何使用单个语句(例如 for 循环或条件语句)按整数因子 M 对序列 x 进行下采样
- java - 按月休眠多个组
- sql - 每个月按时间标记的用户