简体   繁体   English

尝试将Range从工作表复制到新工作簿中。 Excel VBA

[英]Trying to copy Range from worksheet into a new workbook. Excel VBA

Here is what I have: 这是我所拥有的:

Sub exportDataAM()
Dim wbS As Workbook, wbT As Workbook
Dim wsS As Worksheet, wsT As Worksheet

Set wbS = ThisWorkbook 'workbook that holds this code
Set wsS = wbS.Worksheets("Recap").Range("A1:R5")

wsS.Copy
Set wbT = ActiveWorkbook 'assign reference asap

Set wsT = wbT.Worksheets("Recap").Range("A1:R5")
wsT.Name = "Recap" 'rename sheet

wbT.SaveAs wbS.Path & "\" & "Recap AM" & Format(Now, " dd-mm-yyyy ") 'save new workbook
End Sub

I get a Type mismatch which just doesn't make sense to me regarding the .Range on Line 6. I know there are other ways to do this, but this is working fine without the .Range and I wanted to figure out specifically why that may be. 我遇到了类型不匹配的问题,对于第6行的.Range来说,这对我来说没有任何意义。我知道还有其他方法可以做到这一点,但是如果没有.Range,这还是可以的,我想具体找出原因也许。

Any help on this will be greatly appreciated. 任何帮助,将不胜感激。

You have declared the variables wsS and wsT as Worksheets. 您已将变量wsSwsT为工作表。 Yet, you try adding a range into the variable. 但是,您尝试将范围添加到变量中。 This will cause a mismatch. 这将导致不匹配。

Either declare the variables as Range , or add only the worksheet into the variable. 将变量声明为Range ,或仅将工作表添加到变量中。

The following should work 以下应该工作

Private Sub Copy()
Set newBook = Workbooks.Add
With newBook
    .Title = "Recap Am"
    .Subject = "RFQ"
    .SaveAs Filename:="Recap.xls"
  Dim rng As Excel.Range

Set rng = ThisWorkbook.Worksheets("SheetName").Cells.SpecialCells (xlCellTypeVisible)

rng.Copy newBook.Worksheets("Sheet1").Range("A1")

End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM