新的VBA试图弄清楚。使用代码尝试从各种文件自动复制到具有公式的文件,计算并再返回。 当我尝试粘贴时,遇到了'运行时错误'1004',范围类的pastespecial方法失败'消息。该消息仅在使用变量声明第一个单元格时出现,以便复制一系列值。 当即时通讯使用直接单元格描述一切工作正常。 我还使用自定义函数来获取给定字段名称的列字母。无法粘贴 - Excel VBA
Function ActiveColumnName(fieldname As String, fieldnames_line As Integer) As String
Range("A" & fieldnames_line & ":AB" & fieldnames_line).NumberFormat = "@"
Cells.find(What:=fieldname, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveColumnNumber = ActiveCell.Column
Dim m As Integer
Dim ActiveColumnName As String
ActiveColumnName = ""
Do While (ActiveColumnNumber > 0)
m = (ActiveColumnNumber - 1) Mod 26
ActiveColumnName = Chr(65 + m) + ActiveColumnName
ActiveColumnNumber = Int((ActiveColumnNumber - m)/26)
Loop
End Function
sub main()
Dim firstrow_data_main As Integer
Dim firstrow_fieldnames_main As Integer
firstrow_data_main = 16
firstrow_fieldnames_main = 15
Range(ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_main) & firstrow_data_main, Range(ActiveColumnName("ÄÅÔÅ", firstrow_fieldnames_main) & Rows.Count).End(xlUp).Offset(-1)).Select
Application.CutCopyMode = False
Selection.Copy
Workbooks.Open help_file '"help_file" is any given .xls path with formulas
Dim firstrow_data_help As Integer
Dim firstrow_fieldnames_help As Integer
firstrow_data_help = 7
firstrow_fieldnames_help = 4
'NOW WHEN I USE THIS, DOESNT WORK:
-> Range(ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_help) & firstrow_data_help).Select
'WHEN I USE THIS, WORKS FINE:
-> Range("L7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
当它不工作时,它打开.xls和它的确选择了理想的细胞,但没有脑袋。我知道这与剪贴板有关,但无法弄清楚。有什么建议么?
1.除去所有的选择和激活通过直接参照的细胞,见[HERE](http://stackoverflow.com/questions/ 10714251/how-to-avoid-using-select-in-excel-vba-macros)2.查看“Cells()”而不是Range,避免将列号转换为字母的整个需求, )'使用数字。 3.当值是唯一需要的时候避免剪贴板,并简单地将值赋给新单元格(这将要求两个范围的大小相同,因此请使用Resize())4.始终表示范围的父表单它会减少错误。 –