复制 - 从乘法行粘贴单元格值

时间:2018-01-15 10:50:01

标签: excel vba copy row cut

我有一个问题。 我有多个行。列中每行的值很少。

我需要宏来让我选择任何行来从列到另一列获取值。

enter image description here

例如。 我有从5到23的行。并且我在每行中有H,K,N,Q,T列的值。 有一次我需要从第6行和过去的所有值切割到W,Z ......列, 其他时间我需要使用第15行。

如果有一些宏可以让我写下行号并只为该行运行宏?或类似的东西?

我不擅长这个...... 请帮忙。

谢谢大家

1 个答案:

答案 0 :(得分:1)

如下所示,它将提示用户输入一个数字,然后它会在第1行查找该数字,如果找到,则会将该列复制到W&列中。 Z:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
start:
x = InputBox("Enter a Column Number to copy to column W & Z", "Copy Column") 'get number from user
If IsNumeric(x) Then 'make sure input was number
Set foundcolumn = ws.Range("1:1").Find(What:=x) 'find number on row 1
    If Not foundcolumn Is Nothing Then 'if found
        ws.Range(ws.Cells(5, foundcolumn.Column), ws.Cells(LastRow, foundcolumn.Column)).Copy 'copy from row 5 to last on given column
        ws.Range("W5").PasteSpecial xlPasteValues 'paste in W5
        ws.Range("Z5").PasteSpecial xlPasteValues 'paste in Z5
    End If
Else
MsgBox "Please enter a number as in the first row"
GoTo start:
End If
End Sub