2017-01-06 40 views
0

我試圖從一本書(Book1)複製變量範圍到另一本書(book2)的變量範圍的末尾。我只對第1冊中的變量範圍的值感興趣,這是問題所在。所以我需要找到最後一行值(而不是公式)。在這個論壇上,我發現了幾個選項,但沒有一個適用於我的情況。下面是我得到了什麼(請查看代碼「複製詳細USHB」的第二部分 - 「選擇單元格複製):複製可變範圍的值而不是公式

   ''''''Copy Detail by Vendor'''''' 
'Last cell in column 

    Dim WS As Worksheet 
    Dim LastCell As Range 
    Dim LastCellRowNumber As Long 
    Set WS = Worksheets("Detail by Vendor") 
    With WS 
    Set LastCell = .Cells(.Rows.Count, "B").End(xlUp) 
    LastCellRowNumber = LastCell.Row + 1 
    End With 
    Dim wb As Workbook, wb2 As Workbook 
    Dim vFile As Variant 

'Set source workbook 
Set wb = ActiveWorkbook 

'Open the target workbook 
Workbooks.Open Filename:= _ 
     "Book2.xlsm" 
'Set selectedworkbook 
Set wb2 = ActiveWorkbook 

'Select cells to copy 
Sheets("By Vendor").Select 
Range("A2").Select 
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select 
Selection.Copy 

'Go back to original workbook you want to paste into 
wb.Activate 
Sheets("Detail by Vendor").Select 

'Paste starting at the last empty row 
wb.Worksheets("Detail by Vendor").Range("B" & LastCellRowNumber).Select 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
    :=False, Transpose:=False 
Application.CutCopyMode = False 
Application.ScreenUpdating = True 

        '''''Copy Detail USHB''''' 
'Last cell in column 
    Set WS = Worksheets("Detail USHB") 
    With WS 
    Set LastCell = .Cells(.Rows.Count, "B").End(xlUp) 
    LastCellRowNumber = LastCell.Row + 1 
    End With 
'Activate the target workbook 
wb2.Activate 

'Select cells to copy 
Sheets("Detail USHB").Select 
Dim jLastRow As Long 
jLastRow = Columns("B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows,  SearchDirection:=xlPrevious).Row 

Range(Selection, ActiveCell.SpecialCells(xlLastRow).Select 
Selection.Copy 

'Go back to original workbook you want to paste into 
wb.Activate 
Sheets("Detail USHB").Select 

'Paste starting at the last empty row 
wb.Worksheets("Detail USHB").Range("B" & LastCellRowNumber).Select 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
    :=False, Transpose:=False 
Application.CutCopyMode = False 
Application.ScreenUpdating = True 
End Sub 
+5

你爲什麼要標記這個'java'? 「excel-vba」會更合適嗎? – Andreas

+0

如果你關心的只是價值 - 不要複製/粘貼。只需'R.Value = S.Value',其中'R'和'S'是適當設置的範圍變量。 –

+0

@JohnColeman這個範圍會有所不同,我需要合併的範圍會有所不同。所以它不會在我的情況下工作 – Katie

回答

0

按照你的意見,我相信你正在努力做到以下幾點:

'... 

    '''''Copy Detail USHB''''' 
    Dim D As Range 
    Dim S As Range 
    With wb2.Worksheets("Detail USHB") 
     'Locate the last non-blank value in source range 
     LastRow = .Range("B:B").Find(What:="*", _ 
            LookIn:=xlValues, _ 
            SearchDirection:=xlPrevious).Row 
     'Set range 
     Set S = .Range("B2:B" & LastRow) 
    End With 
    With wb.Worksheets("Detail USHB") 
     'Find last used cell in destination range 
     Set D = .Range("B" & .Rows.Count).End(xlUp) 
     'Offset to next row, and resize appropriately 
     Set D = D.Offset(1, 0).Resize(LastRow - 1, 1) 
    End With 
    'Copy values 
    D.Value = S.Value 
End Sub 
+0

@johncoleman - 我將'R.Value = S.Value'改爲'D.Value = S.Value'(這樣我就不會被指責抄襲)) – YowE3K