VBA脚本将列中的单元格值复制到其他列中给出的单元格地址

时间:2015-08-07 14:49:40

标签: excel vba excel-vba

我希望做到以下几点:

我有一列有值,一列有单元格地址。我想将A列中的值复制到B列中的单元格地址.VBA脚本应该运行直到有一个空单元格。

   A       B       C     D     E

 1 *       C1
 2 $       E3
 3 à       C2

运行宏后我应该:

   A       B       C     D     E

 1 *       C1      *
 2 $       E3      à
 3 à       C2                  $

2 个答案:

答案 0 :(得分:1)

Option Explicit

Public Sub copyValues()
    Dim ws As Worksheet, cel As Range, ur As Range

    Set ws = Sheet1

    Set ur = ws.Range("A1:A" & ws.Cells(ws.UsedRange.Rows.Count + 1, 1).End(xlUp).Row)

    For Each cel In ur
        If Len(cel.Offset(0, 1)) > 0 Then ws.Range(cel.Offset(0, 1).Value2) = cel
    Next
End Sub

答案 1 :(得分:0)

试试这个:

Sub CopyPasteCellValues()

    Dim lastrow As Long, x As Long
    Dim RangeValues As Range, cell As Range
    Dim address As String

    lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

    'RangeValues is a collection of all cell values that are supposed to be copied and pasted
    Set RangeValues = Range(Cells(1, 1), Cells(lastrow, 1))

    x = 1

    For Each cell In RangeValues
        address = Cells(x, 2).Value
        Range(address) = Cells(x, 1)
        x = x + 1
    Next cell

End Sub