简体   繁体   English

如何使用VBA选择粘贴范围

[英]How to select the range for pasting using vba

I wrote some code for selecting the particular row and pasting it in column wise using paste-special property. 我编写了一些代码,用于选择特定的行并使用paste-special属性将其按列粘贴。 It is working correctly my code is : 它工作正常,我的代码是:

lngRow = Me.TextBox4.Value
strCol = Me.TextBox5.Value
Set rng = Range("A:A").Find(What:=lngRow, LookIn:=xlValues, LookAt:=xlWhole)
If rng Is Nothing Then
MsgBox "Value not found in row 1", vbExclamation
Else
 Range(rng, rng.End(xlToRight)).Copy
 Range("A1:E3").Columns(strCol).Offset(, 1).PasteSpecial Transpose:=True
 Range("A1:E3").Rows(1).Copy
 Range("A1:E3").Columns(strCol).PasteSpecial Transpose:=True
endif

the problem here is I am using Range(rng, rng.End(xlToRight)).Copy to copy the values and for pasting I am using Range("A1:E3").Columns(strCol).Offset(, 1).PasteSpecial Transpose:=True. 这里的问题是我正在使用Range(rng,rng.End(xlToRight))。Copy复制值并进行粘贴,我正在使用Range(“ A1:E3”)。Columns(strCol).Offset(,1)。粘贴特殊转置:= True。

How can I paste all the values which are copied? 如何粘贴所有复制的值? Because if the values are in column F then this macro will not paste those values. 因为如果值在F列中,则此宏将不会粘贴这些值。

This code will paste the whole row, if found, transposed to the given col, starting at row 1. 此代码将从第一行开始将整行(如果找到)粘贴到给定的col上。

If it has to start at a different row or col, you should be able to adapt to that. 如果必须从其他行或列开始,则应该能够适应该行。

lngRow = Me.TextBox4.Value
strCol = Me.TextBox5.Value

Set rng = Range("A:A").Find(What:=lngRow, LookIn:=xlValues, LookAt:=xlWhole)
If rng Is Nothing Then
MsgBox "Value not found in row 1", vbExclamation
Else
 Range(Rng, Rng.End(xlToRight)).Copy
 Cells(1, strCol).PasteSpecial Transpose:=True
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM