我绝对是vba的新手。我想使用以下代码将单元格中的某些值从两个选项卡(“ Equities”,“ Bonds”)复制到第三个选项卡(“ ZSM”)中。
Sub AllesAufEinmal()
Call Spalten
Call Wertpapiere
Call Daten
End Sub
Sub Spalten()
'
' Spalten Macro
'
Sheets("Equities").Select
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("ZSM").Select
Range("A4").Select
ActiveSheet.Paste
Range("A4").Select
Sheets("Bonds").Select
Range("B4").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("ZSM").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Range("A4").Select
End Sub
Sub Wertpapiere()
'
' Wertpapiere Macro
'
'
Sheets("Equities").Select
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("ZSM").Select
Range("A5").Select
ActiveSheet.Paste
Range("A5").Select
Sheets("Bonds").Select
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("ZSM").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Range("A5").Select
End Sub
Sub Daten()
'
' Daten Macro
'
'
Sheets("Equities").Select
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("ZSM").Select
Range("B5").Select
ActiveSheet.Paste
Sheets("Bonds").Select
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("ZSM").Select
Range("B5").Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(1, 1).Select
ActiveSheet.Paste
End Sub
在我想要以某种方式修改代码,以便我的vba代码将两个选项卡(“ Equities,Bonds”)中公式中的值复制到我的第三个选项卡(“ ZSM”)中之前,效果很好。我真的只希望该公式从“ = J5 * K24”之类的公式中返回的值被复制。即使我以以下方式修改了代码(以“ ### here”标记的更改),该代码也无法正常工作:
Sub AllesAufEinmal()
Call Spalten
Call Wertpapiere
Call Daten
End Sub
Sub Spalten()
'
' Spalten Macro
'
Sheets("Equities").Select
Range("A4").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("ZSM").Select
Range("A4").Select
ActiveSheet.Paste
Range("A4").Select
Sheets("Bonds").Select
Range("B4").Select
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("ZSM").Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Range("A4").Select
End Sub
Sub Wertpapiere()
'
' Wertpapiere Macro
'
'
Sheets("Equities").Select
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("ZSM").Select
Range("A5").Select
ActiveSheet.Paste
Range("A5").Select
Sheets("Bonds").Select
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("ZSM").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Range("A5").Select
End Sub
Sub Daten()
'
' Daten Macro
'
'
Sheets("Equities").Select
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("ZSM").Select
Range("B5").Select
ActiveSheet.PasteSpecial ###here
Sheets("Bonds").Select
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("ZSM").Select
Range("B5").Select
Selection.End(xlDown).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(1, 1).Select
ActiveSheet.PasteSpecial ###here
End Sub
有什么想法吗?我读了一些有关PasteSpecial Methode的信息,但目前无法将其应用于我的问题。
感谢您的帮助!非常感谢您的支持。
答案 0 :(得分:5)
使用您的last question中所述的直接价值转移方法,我想到了这一点。
传输的每个部分都带有标签,因此您可以根据需要将各个例程分开。
Option Explicit
Sub AllesAufEinmal()
Dim tws As Worksheet
Set tws = Worksheets("ZSM")
Call Spalten(tws)
'Call Wertpapiere(tws)
'Call Daten(tws)
End Sub
Sub Spalten(zsm As Worksheet)
' Spalten Macro
'headers, ISIN and data from from Equities
With Worksheets("Equities")
With .Range(.Cells(.Rows.Count, "A").End(xlUp), .Cells(4, .Columns.Count).End(xlToLeft))
zsm.Cells(4, "A").Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
'headers from Bonds
With Worksheets("Bonds")
With .Range(.Cells(4, "B"), .Cells(4, .Columns.Count).End(xlToLeft))
zsm.Cells(4, zsm.Columns.Count).End(xlToLeft).Offset(0, 1).Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
'ISIN from Bonds
With Worksheets("Bonds")
With .Range(.Cells(5, "A"), .Cells(.Rows.Count, "A").End(xlUp))
zsm.Cells(zsm.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
'data from Bonds
With Worksheets("Bonds")
With .Range(.Cells(.Rows.Count, "B").End(xlUp), .Cells(5, .Columns.Count).End(xlToLeft))
zsm.Cells(zsm.Cells(zsm.Rows.Count, "B").End(xlUp).Row, _
zsm.Cells(5, zsm.Columns.Count).End(xlToLeft).Column). _
Offset(1, 1).Resize(.Rows.Count, .Columns.Count) = .Value
End With
End With
End Sub
“最佳做法”要求您avoid Select并提供正确的父级工作表引用。为此,我已经将目标工作表引用作为参数传递给每个“ helper”子过程。
答案 1 :(得分:3)
You can try replacing those Activesheet.PasteSpecial
as:
Selection.PasteSpecial Paste:=xlPasteValues
This will paste your selected range as just values.
答案 2 :(得分:3)
You should use xlPasteValues
. Example:
Range("B5").PasteSpecial xlPasteValues
If you prefer formulas you could use xlPasteFormulas
.
I strongly advise to read this article on how to avoid using Select
: