2017-03-16 255 views
0

新的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和它的確選擇了理想的細胞,但沒有腦袋。我知道這與剪貼板有關,但無法弄清楚。有什麼建議麼?

+0

1.除去所有的選擇和激活通過直接參照的細胞,見[HERE](http://stackoverflow.com/questions/ 10714251/how-to-avoid-using-select-in-excel-vba-macros)2.查看「Cells()」而不是Range,避免將列號轉換爲字母的整個需求, )'使用數字。 3.當值是唯一需要的時候避免剪貼板,並簡單地將值賦給新單元格(這將要求兩個範圍的大小相同,因此請使用Resize())4.始終表示範圍的父表單它會減少錯誤。 –

回答

0
  1. 通過直接引用單元格刪除所有的選擇和激活,見HERE獲取更多信息。
  2. 查看Cells()而不是Range,並避免將列號轉換爲字母的整個需求,因爲Cells()使用數字。
  3. 當值是唯一需要的值時避免剪貼板,並將值簡單地分配給新單元格(這將要求兩個範圍的大小相同,因此請使用Resize())
  4. 總是表示範圍,它會減少錯誤。

重構的代碼

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 
0

我認爲這個問題可能是在這裏:

ActiveColumnName("<FIELDNAME>", firstrow_fieldnames_main) 

這意味着ActiveColumnNamen x (1 to 2)維度的矩陣。如果你想連接一個名字給一個變量,你必須使用(例如):

"YourStringHere" & YourVariableHere & "AnotherString" 

而你的情況應該是:

ActiveColumnName("<FIELDNAME>" & firstrow_fieldnames_main) 

所以,如果我理解正確的(<FIELDNAME>有點晦澀),整個命令應該是:

Range(ActiveColumnName("<FIELDNAME>" & firstrow_fieldnames_help) & "," & firstrow_data_help).Select 
0

首先,運行之前,你應該編譯VBA。在VBA編譯器捕獲這種蝙蝠:
Dim ActiveColumnName As String
是不必要的,因爲你分配ActiveColumnName作爲字符串,當你在第1行

定義的函數可以使用很多活性細胞和細胞選擇參考。這被稱爲導致運行時錯誤。看到這篇文章:"How to avoid using Select in Excel Vba Macros"

我懷疑字段名不在你認爲它應該在你的help_file中的位置,即它不在第4行。這將意味着代碼不知道在哪裏粘貼數據。通常,調試的最佳方式是將代碼分成儘可能最小的動作,以查看導致錯誤的原因(請參閱SpreadSheet Guru Strategies)。你可以運行下面的代碼來看看輸出是什麼?

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 
0

謝謝大家的回覆!我嘗試了所有的建議,但一路上遇到了各種問題。不過,您的所有建議都有助於我對這個問題發展出不同的看法。我最終得到的解決方案源於你的建議,放棄「.select」作爲參考方式,並使用「rng」變量,當然也可以去掉雙引用「ActiveColumnName」。我知道我有一個很長的路要走,但目前這個東西的作品!!謝謝!

子主()

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 

結束子