在同一工作表中是否有任何有效的方法或正确的方法进行复制和粘贴?我的代码:
With ActiveWorkbook.Sheets("Sheet1")
For Each row In .Rows
If Not row.Columns("A:A") Is Empty Then 'error here
.Columns("A:A").Copy .Range("B1")
End If
Next rw
.Columns("A:A").Delete
End With
因此,在上面的代码中,我想仅在单元格的列A不为空时才用列A替换列B. 例如:
1 Nil
Nil
24
4 Nil
4 Nil
12
3
7 Nil
2
Nil
8 Nil
最终结果将如B列所示:
1
Nil
24
4
4
12
3
7
2
Nil
8
编辑:没关系,解决了。
With ActiveWorkbook.Sheets("Sheet1")
For rw = 1 To .Rows.Count
If (.Rows(rw).Columns("A:A").Value <> "") Then
.Rows(rw).Columns("A:A").Copy .Range("B" & rw)
End If
Next rw
.Columns("A:A").Delete
End With
答案 0 :(得分:0)
With ActiveWorkbook.Sheets("Sheet1").UsedRange
For Each Row In .Rows
If Row.Cells(1, 1) <> "" Then
Row.Cells(1, 2) = Row.Cells(1, 1)
End If
Next
.Columns("A:A").Delete
End With
答案 1 :(得分:0)
如果要在列更改中的任何单元格中使用该方法时触发该方法 Worksheet_Change,这里我们只在列J中捕获对单元格的任何更改
在此示例中,我们将值E列中的值复制到G,而不包括空单元格。如果使用此命令工作表(&#34; Sheet1&#34;),我们首先清除G列,如果它有任何旧值。范围(&#34; G:G&#34;)。ClearContents < / p>
Private Sub Worksheet_Change(ByVal Target As Range)
idx = ActiveCell.Row
idxStr = CStr(idx)
labelIdx = "J" + idxStr
Dim ii As Long
Dim columnNumber As Long
ii = 1
columnNumber = 10
If ActiveCell.Column = columnNumber And ActiveCell.Value <> "" Then
Worksheets("Sheet1").Range("F1") = Range(labelIdx).Value
Worksheets("Sheet1").Range("G:G").ClearContents
For Each cell In Worksheets("Sheet1").Range("E:E")
If cell.Value <> "" And cell.Value <> "COLUMN LABEL" Then
Worksheets("Sheet1").Range("G" + CStr(ii)).Value = cell.Value
ii = ii + 1
End If
Next cell
End If
End Sub