如何将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).Copywith.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
相关产品推荐
相关产品推荐

