【问题标题】:Excel VBA for each cell in range A, copy value in cell B, to new sheet范围A中每个单元格的Excel VBA,将单元格B中的值复制到新工作表
【发布时间】:2015-09-10 17:58:40
【问题描述】:

我在 Sheet1 中有如下值: A1 = 23,B1 = M,C1 = 256 对于范围 A 中的每个值,我需要将单元格 B1 和 C1 中的值复制到 Sheet2 到单元格 A6 和 D7 中。

非常感谢您的帮助

你好,是的。我试着让它像:

Sub Get_Data()  
    Dim lastrowDB As Long, lastrow As Long  
    Dim arr1, arr2, i As Integer  

    With Sheets("Hárok2")
         lastrowDB = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    Sheets("Hárok1").Select
    arr1 = Array("B", "C", "D")
    Sheets("Hárok2").Select
    arr2 = Array("B3:C3", "B7:C7", "E7:F7")

    For i = LBound(arr1) To UBound(arr1)
        With Sheets("Hárok1")
             lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
             .Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Copy
             Sheets("Hárok2").Range(arr2(i) & lastrowDB).PasteSpecial xlPasteValues
        End With
        'Sheets("Hárok2").Select
        'Range("A1:L5").PrintOut  
    Next  
     Application.CutCopyMode = False
End Sub 

但它给了我一个错误:Sheets("Hárok2").Range(arr2(i) & lastrowDB).PasteSpecial xlPasteValues

【问题讨论】:

标签: excel vba


【解决方案1】:

我认为数组在这里是多余的。我猜你希望它从 A6 和 D7 继续向下。显然根据需要更改工作表。这是从 ws1(源)拉到 ws2(目标)。

Sub denn()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow As Long

Set ws1 = WorkSheets("Sheet4")
Set ws2 = WorkSheets("Sheet5")
lastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row

For i = 1 To lastRow
    ws2.Cells(i + 5, 1) = ws1.Cells(i, 2).Value
    ws2.Cells(i + 6, 4).Value = ws1.Cells(i, 3).Value
Next

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2017-02-06
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多