简体   繁体   English

如何使用vbscript复制粘贴并连接Excel列?

[英]How to copy paste and concatenate Excel column with vbscript?

I am playing with vbscript and how to make my life easier. 我正在玩vbscript以及如何使我的生活更轻松。 Now I want to run a script which opens an excel file. 现在,我想运行一个打开Excel文件的脚本。 Copy column C (unknown rows - length) to column B and concatenate it to something. 将C列(未知行-长度)复制到B列并将其连接为某种东西。

Example: 例:

-------------------
||| A  |  B  |  C |
-------------------
|1|ONE | ANY | 11 | 
|2|TWO | ANY | 42 |
|3|FOUR| ANY | 96 |
|4|SIX | ANY | 42 |
-------------------

To This: 为此:


||| A  |  B  |  C |
-------------------
|1|ONE |*11* | 11 | 
|2|TWO |*42* | 42 |
|3|FOUR|*96* | 96 |
|4|SIX |*42* | 42 |
-------------------

So far I have tried this: 到目前为止,我已经尝试过了:

Option Explicit
Dim objExcel

Set objExcel = CreateObject("Excel.Application")
With objExcel
  .Workbooks.Open ("Z:\1\one.xlsx")
  .Visible = False
  .Range("C:C").Copy
  .Range("B1").Select
  .ActiveSheet.Paste
  .ActiveWorkbook.Close(True)
  .Quit
End With

Firstly it gives me an error that clipboard is full and it is better to clean up in order to free memory. 首先,它给我一个错误,就是剪贴板已满,最好清理一下以释放内存。 And secondly I have not found a concatenate function in internet for vbscript. 其次,我还没有在Internet中为vbscript找到连接功能。

With some research and the Help of Bruce that worked for me in case someone is interested: 通过一些研究和布鲁斯的帮助对我有用,以防万一有人感兴趣:

Option Explicit
Dim objExcel,LastRow
Const xlUp = -4162


Set objExcel = CreateObject("Excel.Application")
With objExcel
    .Workbooks.Open ("Z:\1\onest.xlsx")
    .Visible = False
    LastRow = .Range("A1048576").End(xlUp).Row
    .Range("C1:" & "C" & LastRow).Copy
    .Range("B1").Select
    .ActiveSheet.Paste
    Do While LastRow <>0
        .Cells(LastRow,2)="*"&.Cells(LastRow,3)&"*"
        LastRow = LastRow - 1
     Loop
     .ActiveWorkbook.Close(True)
     .Quit
End With

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

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