繁体   English   中英

Excel-复制选定的单元格并粘贴到另一个工作表中列的末尾

[英]Excel - Copy selected cell and paste to the end of a column in another worksheet

我是VBA的新手,希望复制所选单元格的内容并将其粘贴到工作表“草稿”的B列中的下一个可用行。

我能够找到我需要的其他2个简单宏,但是可以弄清楚如何将它们组合在一起以解决这个问题。

Sub CopyName()
'Copy's selected cell to cell K2 within same worksheet... which then triggers vlookups with additional info

Selection.Copy
Range("K2").Select
ActiveSheet.Paste
End Sub

Sub SelectPlayer()
'Copies name pasted in K2 , to next available row in column B of other worksheet

Worksheets("Draft").Range("B" & Rows.Count).End(xlUp).Offset(1) =       Worksheets("Dashboard").Range("K2").Value




 End Sub

好的, 试试这个:

Sub CopyName()
Worksheets("Draft").Range("B" & Rows.Count).End(xlUp).Offset(1, 0) = Worksheets("Dashboard").Range("K2").Value
End Sub

您实际上需要找出工作表“草稿”中的下一个可用行。 用这个:

Sub CopyName()
'Copy's selected cell to cell K2 within same worksheet... which then triggers vlookups with additional info

Selection.Copy
Range("K2").Select
ActiveSheet.Paste
End Sub

Sub SelectPlayer()
'Copies name pasted in K2 , to next available row in column B of other worksheet
Dim intNextEmptyCell As Integer
'assuming data on sheet "Draft" starts from row 2 column 1
intNextEmptyCell = Get_Count(2, 1, Worksheets("Draft"), true)
Worksheets("Draft").Range("B" & Strings.Trim(Str(intNextEmptyCell+1))).End(xlUp).Offset(1) =  Worksheets("Dashboard").Range("K2").Value
End Sub

Get_Count是我编写的函数,用于获取列或行的数据数量。 使用该功能,您可以获取草稿表中“ B”列中的数据行数,并将“ K2”中的内容复制到下一个可用单元格。 您可以在我的博客中获取有关该功能的更多信息, 获取列和行数据计数

'intRow: The row your data starts
'intColumn: The column your data starts
'wrkSheet: The worksheet object
'flagCountRows: True if trying to get the number of rows
Public Function Get_Count(ByVal intRow As Integer, ByVal intColumn As Integer, ByRef wrkSheet As Worksheet, _
ByVal flagCountRows As Boolean) As Integer
Dim i As Integer
Dim flag As Boolean

i = 1
flag = True
While flag = True
    If flagCountRows = True Then
        If wrkSheet.Cells(i + intRow - 1, intColumn) <> "" Then

           i = i + 1
        Else
           flag = False
        End If
    Else
        If wrkSheet.Cells(intRow, intColumn + i–1) <> "" Then
           i = i + 1
        Else
           flag = False
        End If
    End If
Wend

Get_Count = i–1
End Function

String.Trim(Str())将整数数据类型转换为字符串。

暂无
暂无

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

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