You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改VBA代码:点击提交按钮后将汇总表数据按列写入新工作表

修改VBA代码实现按列写入数据

原代码是将指定单元格数据按行追加到Sheet3中,要改成按新空白列纵向写入,只需做以下调整:

  1. 替换行定位逻辑为列定位:从找下一行改为找Sheet3中最右侧的空白列
  2. 调整数据写入的单元格坐标:把「行固定、列递增」改为「列固定、行递增」

修改后的完整代码:

Sub data_input2()
    Dim ws_output As String
    Dim next_col As Integer
    
    ws_output = "Sheet3"
    
    ' 获取Sheet3中第一个空白列的列号
    next_col = Sheets(ws_output).Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column
    
    ' 将源数据纵向写入新空白列
    Sheets(ws_output).Cells(1, next_col).Value = Range("B1").Value
    Sheets(ws_output).Cells(2, next_col).Value = Range("B2").Value
    Sheets(ws_output).Cells(3, next_col).Value = Range("B3").Value
    Sheets(ws_output).Cells(4, next_col).Value = Range("A8").Value
    Sheets(ws_output).Cells(5, next_col).Value = Range("B8").Value
    Sheets(ws_output).Cells(6, next_col).Value = Range("C5").Value
    Sheets(ws_output).Cells(7, next_col).Value = Range("C6").Value
    Sheets(ws_output).Cells(8, next_col).Value = Range("C7").Value
    Sheets(ws_output).Cells(9, next_col).Value = Range("C8").Value
End Sub

关键修改说明:

  • 新增Dim声明变量类型,避免隐式类型转换问题(可选但推荐)
  • 用Cells(1, Columns.Count).End(xlToLeft).Offset(0,1).Column定位最右侧空白列
  • 每个数据写入的单元格从Cells(next_row, 列号)改为Cells(行号, next_col),实现纵向排列

内容的提问来源于stack exchange,提问作者Rams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:15:21