选择列中的UsedRange和Sheet中的行

时间:2013-12-21 10:08:08

标签: excel vba excel-vba

  • 我的Excel文件中有两张,DTMGISDTMEdit
  • DTMEdit表格为空
  • 我试图对DTMGIS中的所有内容进行复制,并仅将值粘贴到工作表DTMEdit

我不知道如何仅选择UsedRange并将其粘贴到DMTEdfit

Dim ws As Worksheet
Dim LastRow As Long, LastCoulmn As Long, Header As Long
Header = 2
Set ws = ThisWorkbook.Sheets("DTMGIS")
LastRow = ws.UsedRange.Rows.Count
LastCoulmn = ws.UsedRange.Column.Count

With ws.UsedRange
    .Select
    .Copy
End With

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Sub CopyPasteValues()
    Dim ws1 As Worksheet, ws2 As Worksheet

    Set ws1 = ThisWorkbook.Sheets("DTMGIS")
    Set ws2 = ThisWorkbook.Sheets("DTMEdit")

    ws1.Range(ws1.UsedRange.Address).Copy
    ws2.Range("a1").PasteSpecial xlPasteValues
End Sub

答案 1 :(得分:0)

以下的变体

  • 避免假设UsedRange从A1
  • 开始
  • 在不移动格式的情况下运行副本(如果您最后使用.Copy使用Application.CutCopyMode = False

Sub FastCopy()
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Set ws1 = ThisWorkbook.Sheets("DTMGIS")
    Set ws2 = ThisWorkbook.Sheets("DTMEdit")
    ws2.Range(ws1.UsedRange.Address).Value = ws1.UsedRange.Value
End Sub