无法粘贴-Excel VBA
我正在编写一个代码,该代码将使流程自动化。我想让它从各种文件复制到其他文件与公式,计算,然后再回来 我在尝试粘贴时遇到一条消息“运行时错误”1004,“range类的pastespecial方法失败”。仅当我使用变量声明第一个单元格以复制一系列值时,才会显示此消息。 当我使用直接单元格描述时,一切正常。 我还使用一个自定义函数来获取给定字段名的列字母无法粘贴-Excel VBA,excel,vba,Excel,Vba,我正在编写一个代码,该代码将使流程自动化。我想让它从各种文件复制到其他文件与公式,计算,然后再回来 我在尝试粘贴时遇到一条消息“运行时错误”1004,“range类的pastespecial方法失败”。仅当我使用变量声明第一个单元格以复制一系列值时,才会显示此消息。 当我使用直接单元格描述时,一切正常。 我还使用一个自定义函数来获取给定字段名的列字母 Function ActiveColumnName(fieldname As String, fieldnames_line As Integ
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, DOESN'T 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
函数ActiveColumnName(字段名为字符串,字段名为整数)为字符串
范围(“A”&字段名\行&“:AB”&字段名\行)。NumberFormat=“@”
查找(What:=fieldname,After:=ActiveCell,LookIn:=xlFormulas,LookAt_
:=xlPart,SearchOrder:=xlByRows,SearchDirection:=xlNext,MatchCase:=_
False,SearchFormat:=False)。激活
ActiveColumnNumber=ActiveCell.Column
将m作为整数
将ActiveColumnName设置为字符串
ActiveColumnName=“”
执行时(ActiveColumnNumber>0)
m=(ActiveColumnNumber-1)Mod 26
ActiveColumnName=Chr(65+m)+ActiveColumnName
ActiveColumnNumber=Int((ActiveColumnNumber-m)/26)
环
端函数
次干管()
Dim第一行\u数据\u main为整数
Dim firstrow_fieldnames_main为整数
第一行数据主数据=16
第一行\u字段名\u main=15
范围(ActiveColumnName(“,firstrow_fieldnames_main)和firstrow_data_main),范围(ActiveColumnName(“ÄÔÔÔ”,firstrow_fieldnames_main)和行数。结束(xlUp)。偏移量(-1))。选择
Application.CutCopyMode=False
选择,复制
Workbook.Open help_文件的“help_文件”是任何给定的带公式的.xls路径
将第一行\u数据\u帮助设置为整数
将第一行\u字段名\u帮助设置为整数
firstrow\u data\u help=7
第一行\字段名\帮助=4
“现在当我使用这个时,它不起作用:
->范围(ActiveColumnName(“,第一行\字段名\帮助)&第一行\数据\帮助)。选择
“当我使用这个时,效果很好:
->范围(“L7”)。选择
Selection.Paste特殊粘贴:=xlPasteValues,操作:=xlNone,skipblank:=False,转置:=False
Application.CutCopyMode=False
端接头
当它不工作时,它会打开
.xls
,并且确实选择了所需的单元格,但没有显示。我知道这与剪贴板有关,但我无法理解。有什么建议吗?我想问题可能在这里:
ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_main)
在你的情况下,这将是:
ActiveColumnName("<FIELDNAME>" & firstrow_fieldnames_main)
ActiveColumnName(“&firstrow\u fieldnames\u main”)
因此,如果我理解正确(
有点模糊),整个命令应该是:
Range(ActiveColumnName("<FIELDNAME>" & firstrow_fieldnames_help) & "," & firstrow_data_help).Select
范围(ActiveColumnName(“&firstrow\u fieldnames\u help)&“,”&firstrow\u data\u help)。选择
Sub main()
Dim firstrow_data_main As Integer
Dim firstrow_fieldnames_main As Integer
Dim rng As Range
Dim tWb As Workbook
Dim ws As Worksheet
Dim tWs As Worksheet
Dim firstrow_data_help As Integer
Dim firstrow_fieldnames_help As Integer
Set ws = ThisWorkbook.ActiveSheet
Set tWb = Workbooks.Open(help_file)
Set tWs = tWb.ActiveSheet
firstrow_data_main = 16
firstrow_fieldnames_main = 15
firstrow_data_help = 7
firstrow_fieldnames_help = 4
With ws
Set rng = .Range(.Cells(firstrow_data_main, firstrow_fieldnames_main), .Cells(.Rows.Count, firstrow_fieldnames_main).End(xlUp).Offset(-1))
tWs.Cells(firstrow_data_help, firstrow_fieldnames_help).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
End With
End Sub
首先,您应该在运行之前编译VBA。VBA编译器捕捉到了这一点:
将ActiveColumnName设置为字符串
没有必要,因为在第1行中定义函数时已将ActiveColumnName指定为字符串 对活动单元格和选择单元格使用了大量引用。这会导致运行时错误。请参阅此帖子: 我怀疑字段名不在您认为应该在帮助文件中的位置,即它不在第4行。这意味着代码不知道将数据粘贴到哪里。通常,最好的调试方法是将代码分割成尽可能小的动作,以查看导致错误的原因(请参阅)。您可以运行以下代码来查看输出是什么吗
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 _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveColumnNumber = ActiveCell.Column
Dim m As Integer
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()
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
MessageBox = ActiveColumnName("FIELDNAME", firstrow_fieldnames_help) & firstrow_data_help
End Sub
谢谢大家的回复!我一个接一个地尝试了你的所有建议,但在过程中遇到了各种各样的问题。然而,你所有的建议帮助我对这个问题有了不同的看法。我最终得到的解决方案来自于您的建议,即放弃“.select”作为一种引用方式,使用“rng”变量,当然还要消除双重引用“ActiveColumnName”。我知道我还有很长的路要走,但就目前而言,这件事是可行的!!谢谢 副标题()
Dim firstrow\u data\u main作为整数
Dim firstrow_fieldnames_main为整数
将第一行\u数据\u帮助设置为整数
将第一行\u字段名\u帮助设置为整数
第一行数据主数据=16
第一行\u字段名\u main=15
第一行数据帮助=7
第一行\字段名\帮助=4
变暗rng1 As范围
变暗rng2 As范围
设置rng1=Range(ActiveColumnName(“,firstrow\u fieldnames\u main)和firstrow\u data\u main,Range(ActiveColumnName(“,firstrow\u fieldnames\u main)和Rows.Count)。结束(xlUp)。偏移量(-1))
所选单元格=rng1.Rows.Count
作业本,打开
设置rng2=Range(ActiveColumnName(“,firstrow\u fieldnames\u help)&firstrow\u data\u help,Range(ActiveColumnName(“,firstrow\u fieldnames\u help)&选定单元格+firstrow\u data\u help-1))
rng1。复制rng2
末端接头1。通过直接参考单元格删除所有选择和激活选项,请参见2。查看
Cells()
而不是Range
,避免将列数字转换为字母,因为Cells()
使用数字。3.当值是您唯一需要的内容时,请避免使用剪贴板,只需将值分配给新单元格(这将要求两个区域的大小相同,因此请使用Resize())4。始终表示范围的父表,这样可以减少错误。
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 _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveColumnNumber = ActiveCell.Column
Dim m As Integer
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()
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
MessageBox = ActiveColumnName("FIELDNAME", firstrow_fieldnames_help) & firstrow_data_help
End Sub
Dim firstrow_data_main As Integer
Dim firstrow_fieldnames_main As Integer
Dim firstrow_data_help As Integer
Dim firstrow_fieldnames_help As Integer
firstrow_data_main = 16
firstrow_fieldnames_main = 15
firstrow_data_help = 7
firstrow_fieldnames_help = 4
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Range(ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_main) & firstrow_data_main, Range(ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_main) & Rows.Count).End(xlUp).Offset(-1))
cells_selected = rng1.Rows.Count
Workbooks.Open <help_file>
Set rng2 = Range(ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_help) & firstrow_data_help, Range(ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_help) & cells_selected + firstrow_data_help - 1))
rng1.Copy rng2