why does my SUB X delete an entire row of data

时间:2019-01-07 13:52:34

标签: excel vba

I do in my Private sub get data and move it from one sheet to another. But when i run the "Sub X", it goes back to the sheet i am getting my data from, and deleting a row ("M") of data. i have tried to corret it, but with no luck. Then i just get "mismatch".

Hope you can help, Thank you.

Sub MyProcedure()
    a = Worksheets("ark1").Cells(Rows.Count, 1).End(xlUp).Row

    MsgBox (a)

End Sub

Private Sub CommandButton1_Click()
    Dim nøgletal As String, år As Integer
    Worksheets("Ark2").Select
    nøgletal = Range("B2")
    år = Range("C2")
    Worksheets("Ark1").Select
    Worksheets("Ark1").Range("A4").Select
    ThisWorkbook.Worksheets("Ark1").Range("A1:A100").Value = ThisWorkbook.Worksheets("Ark2").Range("A12:A100").Value
    ThisWorkbook.Worksheets("Ark1").Range("B1:B100").Value = ThisWorkbook.Worksheets("Ark2").Range("B12:B100").Value
    ThisWorkbook.Worksheets("Ark1").Range("C1:C100").Value = ThisWorkbook.Worksheets("Ark2").Range("C12:C100").Value
    ThisWorkbook.Worksheets("Ark1").Range("E1:E100").Value = ThisWorkbook.Worksheets("Ark2").Range("E12:E100").Value
    ThisWorkbook.Worksheets("Ark1").Range("G1:G100").Value = ThisWorkbook.Worksheets("Ark2").Range("M12:M100").Value
    ThisWorkbook.Worksheets("Ark1").Range("F1:F100").Value = ThisWorkbook.Worksheets("Ark2").Range("N12:N100").Value
    ThisWorkbook.Worksheets("Ark1").Range("H1:H100").Value = ThisWorkbook.Worksheets("Ark2").Range("O12:O100").Value
    If Worksheets("Ark1").Range("A4").Offset(1, 0) <> "" Then
        Worksheets("Ark1").Range("A4").End(xlDown).Select
    End If
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = nøgletal
    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = år
    Worksheets("Ark2").Select
    Worksheets("Ark2").Range("B2", "B16").Select
End Sub

Sub x()

    Dim lngDataColumns As Long
    Dim lngDataRows As Long

    lngDataColumns = 3
    lngDataRows = 50


    For t = 1 To lngDataRows

        Range("l2").Offset(((t - 1) * lngDataColumns) - 1, 0).Resize(lngDataColumns, 1).Value = _
                Application.Transpose(Range("f1:h1").Value)

        Range("M2").Offset(((t - 1) * lngDataColumns) - 1, 0).Resize(lngDataColumns, 1).Value = _
                Application.Transpose(Range("f1:h1").Offset(t).Value)

    Next t

End Sub

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

如果 X 子过程位于工作表的专用代码表而不是公共模块代码表上,则 Range Cells 总是隐式属于该工作表。

如果您在指定源工作表和目标工作表时遇到麻烦,那么也许将它们作为参数传递给 X 子过程将是一种有效的方法。

A,B,C,E,F

enter image description here