Próbuję skopiować całą kolumnę w arkuszu A do arkusza B. Kolumna Arkusz A posiada wartości utworzone za pomocą formularzy. Kopiuję wartości z kolumny ArkuszA tylko przy pomocy xlPasteValues. Ale wartości nie są wklejane do innego arkusza B. Kolumna w arkuszuB jest pusta. Mój kod VBA
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
With Sheets("SheetA")
lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row)
.Range(.Cells(1, arr1(i)), .Cells(lastrow, arr1(i))).Copy
Sheets("SheetB").Range(arr2(i) & firstrowDB).PasteSpecial xlPasteValues
End With
Next
Application.CutCopyMode = False
End Sub
Jeśli chcesz po prostu skopiować całą kolumnę, możesz znacznie uprościć kod, robiąc coś takiego:
Sub CopyCol()
Sheets("Sheet1").Columns(1).Copy
Sheets("Sheet2").Columns(2).PasteSpecial xlPasteValues
End Sub
Lub
Sub CopyCol()
Sheets("Sheet1").Columns("A").Copy
Sheets("Sheet2").Columns("B").PasteSpecial xlPasteValues
End Sub
Lub jeśli chcesz zachować pętlę
Public Sub CopyrangeA()
Dim firstrowDB As Long, lastrow As Long
Dim arr1, arr2, i As Integer
firstrowDB = 1
arr1 = Array("BJ", "BK")
arr2 = Array("A", "B")
For i = LBound(arr1) To UBound(arr1)
Sheets("Sheet1").Columns(arr1(i)).Copy
Sheets("Sheet2").Columns(arr2(i)).PasteSpecial xlPasteValues
Next
Application.CutCopyMode = False
End Sub
Osobiście skróciłbym go też trochę, jeśli potrzebujesz tylko kolumn:
For i = LBound(arr1) To UBound(arr1)
Sheets("SheetA").Columns(arr1(i)).Copy
Sheets("SheetB").Columns(arr2(i)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
Next
z tego fragmentu kodu wynika, że nie ma zbyt wiele sensu w lastrow
lub firstrowDB
.