EXCEL 批量将相同的行合并(宏脚本记录)

Sub Macro1()

    '

    ' Macro1 Macro

    ' 判定A列上每一同类的起点与终点

    '

    ' 快捷键: Ctrl+w

    '

    Application.DisplayAlerts = False '关闭警告信息显示

    Dim i As Integer '表格递增变量


    Dim first As Integer '同类内容的起点行号

    Dim last As Integer '同类内容的终点行号


    first = 1 '赋初值


    For i = 1 To 50 Step 1  '这个地方是多少行

        If Worksheets("Sheet1").Range("A" & i) = Worksheets("Sheet1").Range("A" & i + 1) Then '如果相同表示是同一类  A是指A列

            '同一类情况下所要进行的操作

        Else '比较不相等,所以一个新的类别开始

            last = i '赋值给last,一个类别的终点行号

            Worksheets("Sheet1").Range("A
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Apache POI库在Java中自动合并Excel单元格的示例代码: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelMergeRows { public static void main(String[] args) { try { // 读取Excel文件 FileInputStream fis = new FileInputStream(new File("input.xlsx")); Workbook workbook = WorkbookFactory.create(fis); Sheet sheet = workbook.getSheetAt(0); // 定义要合并的列 int mergeColumn = 0; // 第一列 // 遍历所有 int rowCount = sheet.getLastRowNum(); for (int i = 0; i <= rowCount; i++) { Row row = sheet.getRow(i); if (row == null) { continue; } // 获取当前单元格和上一个单元格的值 Cell currentCell = row.getCell(mergeColumn); Cell prevCell = null; if (i > 0) { prevCell = sheet.getRow(i - 1).getCell(mergeColumn); } // 如果当前单元格和上一个单元格的值相同,则合并单元格 if (currentCell != null && prevCell != null && currentCell.toString().equals(prevCell.toString())) { CellStyle style = currentCell.getCellStyle(); sheet.getRow(i - 1).getCell(mergeColumn).setCellStyle(style); sheet.addMergedRegion(new org.apache.poi.ss.util.CellRangeAddress(i - 1, i, mergeColumn, mergeColumn)); } } // 保存Excel文件 FileOutputStream fos = new FileOutputStream(new File("output.xlsx")); workbook.write(fos); fis.close(); fos.close(); workbook.close(); System.out.println("Excel merged successfully!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个示例代码假设要合并Excel文件的第一列。您可以通过更改变量`mergeColumn`来指定要合并的列。运此代码后,将在当前目录下创建一个名为`output.xlsx`的新文件,其中包含已合并的单元格。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值