简体   繁体   English

Excel VBA - 从一系列单元格中复制并粘贴到一个单元格中

[英]Excel VBA - Copy from a range of cells and paste in one cell

I need to copy content from a range of cells in sheet1 , say A1:A10 , and paste all the data in a single cell in sheet2 in cell A1 This seems to be a simple use case but I couldn't find any solution so far.我需要从sheet1的一系列单元格中复制内容,比如A1:A10 ,并将所有数据粘贴到单元格A1sheet2的单个单元格中这似乎是一个简单的用例,但到目前为止我找不到任何解决方案. Below is what I tried以下是我尝试过的

' Attempt 1
Sheets("shee1").Select    
Range("A1:A10").Select
Selection.Copy
MsgBox Selection.Text ' Error: Type mismatch. If I add Selection.Text to watch I see an array kind of structure with each index having each cell data
Dim val = Selection.Text
Sheets("sheet2").Select
Range("A1") = val

' Attempt 2
Sheets("sheet1").Range("A1:A10").Copy(Sheets("sheet2").Range("A1") 'Doesn't copy in single cell A1, instead copies to multiple rows like in source

Perhaps this?也许这个?

Option Explicit

Sub Sample()
    Dim MyAr As Variant
    MyAr = Sheets("Sheet1").Range("A1:A10").Value2
    Sheets("sheet2").Range("A1").Value = Join(Application.Transpose(MyAr), vbLf)
End Sub

This reads the range into an array and then joins them before writing the output to the relevant range.这会将范围读入一个数组,然后在将 output 写入相关范围之前将它们连接起来。

Copy Column Data to Cell将列数据复制到单元格

  • All three solutions do the same thing.所有三种解决方案都做同样的事情。
  • Adjust the values in the constants section.调整常量部分中的值。
  • s - Source, d - Destination s - 来源, d - 目的地
  • In a nutshell: writes the values of a column range to an array, then writes the values from the array to a string, and then writes the string to the destination cell.简而言之:将列范围的值写入数组,然后将数组中的值写入字符串,然后将字符串写入目标单元格。
Option Explicit

Sub copyRangeData()
    
    Const sName As String = "Sheet1"
    Const sAddress As String = "A1:A10"
    
    Const dName As String = "Sheet2"
    Const dAddress As String = "A1"
    Const dDelimiter As String = vbLf
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
    Dim srg As Range: Set srg = sws.Range(sAddress)
    Dim sData As Variant: sData = srg.Value
    
    Dim dstring As String
    Dim r As Long
    For r = 1 To UBound(sData, 1)
        dstring = dstring & sData(r, 1) & dDelimiter
    Next r
    dstring = Left(dstring, Len(dstring) - Len(dDelimiter))
    
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    Dim dCell As Range: Set dCell = dws.Range(dAddress)
    dCell.Value = dstring
    
End Sub

Sub copyRangeDataShorter()
    
    Const sName As String = "Sheet1"
    Const sAddress As String = "A1:A10"
    
    Const dName As String = "Sheet2"
    Const dAddress As String = "A1"
    Const dDelimiter As String = vbLf
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sData As Variant: sData = wb.Worksheets(sName).Range(sAddress).Value
    
    Dim dstring As String
    Dim r As Long
    For r = 1 To UBound(sData, 1)
        dstring = dstring & sData(r, 1) & dDelimiter
    Next r
    dstring = Left(dstring, Len(dstring) - Len(dDelimiter))
    
    wb.Worksheets(dName).Range(dAddress).Value = dstring
    
End Sub

Sub copyRangeDataShortest()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim sData As Variant: sData = wb.Worksheets("Sheet1").Range("A1:A10").Value
    
    Dim dstring As String
    Dim r As Long
    For r = 1 To UBound(sData, 1)
        dstring = dstring & sData(r, 1) & vbLf
    Next r
    dstring = Left(dstring, Len(dstring) - 1)
    
    wb.Worksheets("Sheet2").Range("A1").Value = dstring
    
End Sub

You can use Chr(10) which is equivalent to excel Alt + Enter command.您可以使用相当于 excel Alt + Enter命令的Chr(10) It would set cells WrapText to True and enters data in next line.它会将单元格WrapText设置为True并在下一行输入数据。

Sub MultipleCells_To_OneCell()
    Dim cell As Range, SelectedRange As Range

    ThisWorkbook.Worksheets("Sheet2").Activate

    Set SelectedRange = ThisWorkbook.Worksheets("Sheet2").Range("A1:A10")

    For Each cell In SelectedRange
        ThisWorkbook.Worksheets("Sheet3").Range("A1").Value = ThisWorkbook.Worksheets("Sheet3").Range("A1") _
            & Chr(10) & cell
    Next cell
End Sub

Hope this would help you.希望这会对你有所帮助。

暂无
暂无

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

相关问题 Excel VBA宏-将单元格和粘贴值复制到活动单元格范围 - Excel VBA Macro - Copy Cells and Paste Values to Active Cell Range Excel VBA-当某些名称与另一本工作簿中的某些名称匹配时,复制并粘贴一系列单元格 - Excel VBA - Copy and paste a range of cells when certain names match from one work book another Excel VBA 从一个工作表中复制范围并将其一次一个单元格地粘贴到另一张工作表中 - Excel VBA copy range from one sheet and paste it one cell at a time in another sheet vba代码从一个单元格复制数据并将其粘贴到excel中的动态范围内 - vba code to copy data from one cell and paste it in dynamic range in excel Excel VBA如何将WorkSheet名称与单元格值匹配,然后复制并粘贴一系列单元格 - Excel VBA How to match WorkSheet name with Cell value, then Copy and Paste a Range of Cells Excel:需要复制和粘贴单元格,取决于范围中一行中单元格值的非空白状态 - Excel: Need to copy and paste cells depending on non-blank status of cell values in one row in range Excel VBA宏,它将复制一系列单元格并粘贴到另一个工作簿中 - Excel VBA macro that will copy a range of cells and paste into another workbook Excel VBA在列中重复复制和粘贴一系列单元格 - Excel VBA Copy & Paste a Range of Cells Repeatedly in a Column 通过范围单元格进行复制粘贴:Excel VBA - Pass Range Cells for Copy-Paste: Excel VBA 如何复制单元格并将其粘贴到 Excel VBA 宏中的多个单元格 - How to copy a cell & paste it to multiple cells in Excel VBA Macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM