[VB 2010 + Excel 2k3]从源复制特定列并将其粘贴到目标中的其他列名称中 [英] [VB 2010 + Excel 2k3] Copy specific Columns from Source and Paste them in another Columns names in destination
本文介绍了[VB 2010 + Excel 2k3]从源复制特定列并将其粘贴到目标中的其他列名称中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我使用此代码将工作簿复制到另一个,然后粘贴一个范围:
I use this code to copy workbook to another and after that paste just one range:
Option Strict On
Option Explicit On
Option Infer Off
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim app As New Excel.Application
app.Visible = True
'Open Source 1
Dim wbBase As Excel.Workbook = app.Workbooks.Open("D:\My Documents\temp\Differences\Base.xlsx")
Dim base As Excel.Worksheet = CType(wbBase.Sheets("Base"), Excel.Worksheet)
'Open Source 2
Dim wbModified As Excel.Workbook = app.Workbooks.Open("D:\My Documents\temp\Differences\ModifiedBase.xlsx")
Dim mods As Excel.Worksheet = CType(wbModified.Sheets("Modifications"), Excel.Worksheet)
'Add Destination
Dim wbDestination As Excel.Workbook = app.Workbooks.Add
'Copy Source 1 to destination
base.Copy(wbDestination.Sheets(1))
'Cleanup Source 1 references
base = Nothing
wbBase.Close()
wbBase = Nothing
'Get reference to copy
Dim DestinationBase As Excel.Worksheet = CType(wbDestination.Sheets("Base"), Excel.Worksheet)
'Set Range to compare
Dim ColumnD As Excel.Range = DestinationBase.Range("D2", "D5")
'Compare and set differences
Dim CellG As Excel.Range
Dim cellD As Excel.Range
For Each cellD In ColumnD.Cells
CellG = mods.Range("G" & cellD.Row.ToString)
If Not Object.Equals(cellD.Value, CellG.Value) Then
cellD.Value = CellG.Value
cellD.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
End If
Next
' Save Differences
wbDestination.SaveAs("D:\My Documents\temp\Differences\Differences.xlsx")
wbDestination.Close()
wbModified.Close()
'cleanup
cellD = Nothing
CellG = Nothing
ColumnD = Nothing
wbDestination = Nothing
wbModified = Nothing
app.Quit()
app = Nothing
但是,我需要做的是,在验证之后,将一些特定列从源复制到目标
But, what I need to do, is after validation, to copy some specific columns from source to another column ranges in destination
中的另一个列范围(例如,我希望源中的A列在目标中为G,而源中的C列在目标中为D列...)
(for example, I want Column A in source to be G in destination and Column C in Source to be Column D in destination...)
如何我可以这样做吗
最好的问候
推荐答案
公式是什么??A在G中通常B在H中,为什么C在D?
What is the formula -?A is in G so normally B in H,Why C is in D ?
这篇关于[VB 2010 + Excel 2k3]从源复制特定列并将其粘贴到目标中的其他列名称中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文