gpt4 book ai didi

excel - 将多个单元格从excel列复制到剪贴板

转载 作者:行者123 更新时间:2023-12-04 20:10:00 25 4
gpt4 key购买 nike

Example

有没有办法从excel中复制多个选定的单元格,如下所示?它总是复制从第一个选定单元格到最后一个单元格的整个范围,而不是复制选定单元格中的值。

VBA 代码将很有用。

最佳答案

以下将有所帮助,这会将指定的范围复制到剪贴板,以便您可以将它们粘贴到记事本中:

Sub CopyToClipboard()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
ws.Range("B11:B12,B14,B18,B20,B22").Copy
'copy range to clipboard
End Sub

更新:

一个可能的解决方法是使用临时工作表并将选定的值添加到其中,然后将该范围复制到剪贴板中,有点冗长,但它会起作用:
Sub CopyToClipboard()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set the worksheet you are working with, amend as required
Dim work As Worksheet
Dim arr() As Variant
i = 0

For Each work In ThisWorkbook.Worksheets
If work.Name = "Temp" Then
Application.DisplayAlerts = False
work.Delete
Application.DisplayAlerts = True
End If
Next
'if Temp worksheet exists then delete it

For Each c In Selection
i = i + 1
ReDim Preserve arr(1 To i)
arr(i) = c.Value
Next
'above add the values from selection to an array

Set ws2 = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
ws2.Name = "Temp"
'add a temporary worksheet

For x = LBound(arr) To UBound(arr)
ws2.Cells(x, 1).Value = arr(x)
Next x
'copy values from array into temp worksheet

LastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
ws2.Range("A1:A" & LastRow).Copy
'copy continuous range from Temp worksheet
End Sub

关于excel - 将多个单元格从excel列复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53203434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com