Excel vba 方法,“源.复制目标”导致一些神秘现象

Excel vba 方法,“源.复制目标”导致一些神秘现象

这是宏:

Sub dddExport()    
Dim sFile As String    
Dim rSource As Range    
Dim rDest As Range    

Set rSource = ThisWorkbook.ActiveSheet.Range("a1")    

sFile = "C:\dann\dan.xlsx" ''''Adjust as needed    
Workbooks.Open sFile    
Set rDest = ActiveWorkbook.Sheets(1).Range("a1")    
rSource.Copy rDest    
ActiveWorkbook.Close True    

End Sub    

无论范围 A1 中的内容是什么,在源中,粘贴到目标后,我都会得到:
在此处输入图片描述

<TABLE class="table table-bordered" cellSpacing=0 cellPadding=1 width="100%" border=1 data-cols="3"><CAPTION style="FONT-SIZE: 1.2em; FONT-WEIGHT: bold; PADDING-BOTTOM: 3px; TEXT-ALIGN: left">Monatsmittelkurs (gültig für Oktober 2019) </CAPTION>

<TBODY>

<TR role=columnheader>

<TH>Land</TH>

<TH>Währung </TH>

<TH>Devisenkurs (Verkauf) </TH></TR></TBODY>

<TBODY>

<TR>

<TD style="FONT-WEIGHT: bold" vAlign=top>Europäische Währungsunion</TD>

<TD style="FONT-WEIGHT: bold; TEXT-ALIGN: right" vAlign=top>1 EUR</TD>

<TD style="FONT-WEIGHT: bold; TEXT-ALIGN: right" vAlign=top>1.1021</TD></TR>

<TR>

<TD style="FONT-WEIGHT: bold" vAlign=top>USA</TD>

<TD style="FONT-WEIGHT: bold; TEXT-ALIGN: right" vAlign=top>1 USD</TD>

<TD style="FONT-WEIGHT: bold; TEXT-ALIGN: right" vAlign=top>0.9983</TD></TR>

<TR>

<TD vAlign=top>Ägypten</TD>

<TD style="TEXT-ALIGN: right" vAlign=top>100 EGP</TD>

<TD style="TEXT-ALIGN: right" vAlign=top>6.0811</TD></TR>

<TR>

<TD vAlign=top>Albanien</TD>

<TD style="TEXT-ALIGN: right" vAlign=top>100 ALL</TD>

<TD style="TEXT-ALIGN: right" vAlign=top>0.9161</TD></TR>

<TR>

<TD vAlign=top>Argentinien</TD>

<TD style="TEXT-ALIGN: right" vAlign=top>1 ARS</TD>

<TD style="TEXT-ALIGN: right" vAlign=top>0.0177</TD></TR>

<TR>....etc

如果源是 A1:B1,我还会得到以下信息:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

   "http://www.w3.org/TR/html4/strict.dtd">

<html>



<head>

  <title>THIS DOMAIN NAME IS FOR SALE </title>

  <META name="description" content="Contact WebsiteNames.com to inquire about this domain."><META name="keywords" content="WebsiteNames.com, buy domains, for sale, domains for sale, premium domains, webside address, internet address, names for sale">

</head>

<frameset rows="100%,*" border="0">

  <frame src="http://www.contactwebsitenames.com" frameborder="0" />

  <frame frameborder="0" noresize />

</frameset>



<!-- pageok -->

<!-- 12 -->

<!-- -->

</html>     

两个文件,源文件和目标文件都是新的、干净的文件。
如果源范围不是 A1 或 B1,则什么也不会发生。
有什么解释吗?(为什么是“Monatsmittelkurs(gültig für Oktober 2019)”,为什么是“联系 WebsiteNames.com 进行咨询”?)
谢谢。

答案1

无法重现 - 提供的代码运行正常(Office 2010 Prof)。尝试分享有问题的工作簿...

当然,不会执行任何可能改变焦点的并行进程。

不过我还是建议您编辑代码:

Sub dddExport()    
Dim sFile As String    
Dim rSource As Range    
Dim rDest As Range    

' Do not use Active???? - use definite object. '
' Set rSource = ThisWorkbook.ActiveSheet.Range("a1") '
Set rSource = ThisWorkbook.Sheets("Sheet1").Range("a1")    

sFile = "C:\dann\dan.xlsx" ' Adjust as needed '

' Store the reference to opened workbook - this allows to avoid Active???? usage '
' Workbooks.Open sFile ' 
Dim wDest As Workbook
Set wDest = Workbooks.Open(sFile)

' Do not use Active???? - use definite object. '
' Set rDest = ActiveWorkbook.Sheets(1).Range("a1") '
Set rDest = wDest.Sheets("Sheet1").Range("a1")

rSource.Copy rDest    

' Do not use Active???? - use definite object. '
' Use explicit focusing. '
' ActiveWorkbook.Close True '
wDest.Close True
Set wDest = Nothing
ThisWorkbook.Activate
ThisWorkbook.Sheets("Sheet1").Activate

End Sub  

相关内容