Excel VBA-单元格是一个数字,然后复制它

Excel VBA-单元格是一个数字,然后复制它,excel,vba,copy,paste,contentoffset,Excel,Vba,Copy,Paste,Contentoffset,我在E、F和I列写了一些东西。在E和F中是名字,在I中是数字。在I列中,有些单元格带有数字,其余为空。当它是一个数字时,我应该将名称从E1、F1分别复制到C3和D3。我已经写了一个代码,但它不能正常工作。你能帮帮我吗 Option Explicit Sub copy() Dim cell As Long Dim nr As Integer Dim rng As range Dim i As Long range("G1:G15").

我在E、F和I列写了一些东西。在E和F中是名字,在I中是数字。在I列中,有些单元格带有数字,其余为空。当它是一个数字时,我应该将名称从E1、F1分别复制到C3和D3。我已经写了一个代码,但它不能正常工作。你能帮帮我吗

Option Explicit

Sub copy()
    Dim cell As Long
    Dim nr As Integer
    Dim rng As range
    Dim i As Long

    range("G1:G15").Select
    Selection.CurrentRegion.Select
    nr = Selection.Rows.Count
    For i = 2 To nr
        'test if cell is empty
        If ActiveCell(i, 7) = "" Then
            'write to adjacent cell
            ActiveCell = ActiveCell + 1
        Else
            ActiveCell.Offset(-3, -2).Value.copy ActiveCell.Offset(-3, 0)
        End If
    Next i
End Sub
你说:……但它不能正常工作。那么出了什么问题?如您所述,您需要将名称从一行(例如E1中的1)复制到另一行(例如C3中的3)是否正确?如果是,是否总是+2行?为什么要操作
ActiveCell
而不是
Cells(r,c)
其中
r
是行,
c
是列?
Option Explicit

Sub copy()
    Dim Row As Long
    Dim Col As Long
    
    Row = 2
    Col = 9
    Do Until Cells(Row, 5).Value = ""
        If IsNumeric(Cells(Row, Col)) = True Then
            Range("E" & Row & ":F" & Row).copy
            Range("C" & Row).PasteSpecial (xlPasteValues)
        End If
        Row = Row + 1
    Loop
    Application.CutCopyMode = False
End Sub