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

如何将VBA程序的整行选中范围限制为A至M列?

Fix: Copy Only Columns A-M Instead of Entire Row in VBA

Got it, let's tweak your code to only copy columns A through M instead of the entire row—super easy fix! The core issue was using .Rows(Cell.Row) which targets every column in the row; we just need to narrow that range down to A:M.

Here's the updated, working code:

Sub Test()
    Dim Cell As Range
    With Sheets(1)
        ' Loop through column A until last cell with a value (no empty rows at the end)
        For Each Cell In .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            If Cell.Value = "0" Then
                ' Copy ONLY columns A to M of the matching row
                .Range("A" & Cell.Row & ":M" & Cell.Row).Copy _
                    Destination:=Sheets(4).Range("A" & Cell.Row)
            End If
        Next Cell
    End With
End Sub

What changed & why:

  • Replaced .Rows(Cell.Row).Copy with .Range("A" & Cell.Row & ":M" & Cell.Row).Copy: This explicitly defines the range as columns A to M for the row where your condition (Cell.Value = "0") is met.
  • Updated the destination: We use Sheets(4).Range("A" & Cell.Row) to ensure the pasted data starts at column A in the target sheet, keeping your A-M columns aligned correctly.

This will stop copying the entire row and only grab the columns you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:18:54