简体   繁体   English

从一个Excel到另一个Excel逐个单元复制值

[英]Copying value cell by cell from one excel to another excel

I want to copy values from one excel file to another excel file, but on later stages of my project, I may need to cross check the value and then paste it in the new excel file. 我想将值从一个Excel文件复制到另一个Excel文件,但是在项目的后期阶段,我可能需要交叉检查该值,然后将其粘贴到新的Excel文件中。 So I dont want to copy whole sheet at a time. 所以我不想一次复制整张纸。 I want to copy a value by value. 我想按值复制一个值。

I was able to open and switch between to excel file but I m not able to copy and paste the values. 我可以打开并在excel文件之间切换,但无法复制和粘贴值。 Following is the code I tried- 以下是我尝试的代码-

Private Sub CommandButton1_Click()
Dim x As Long
Dim NumRows As Long
Set fromwb = Workbooks.Open("G:\Excel\From.xlsx")
Set towb = Workbooks.Open("G:\Excel\To.xlsx")
fromwb.Activate
NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
ReDim a(1 To NumRows)

For x = 1 To NumRows
    fromwb.Activate
    a(x) = Cells(x, 1).Value

    towb.Activate
    Cells(x, 1).Value = a(x)
Next x

End Sub

Thanking you in anticipation. 感谢你在期待。

You just need to specify the to cells as equal to the from cells, no need to loop anything. 您只需要将to单元格指定为from单元格,而无需循环任何内容。

Workbooks("To.xlsx").Sheets("ToSheetNameHere").Range("rangehere").Value = Workbooks("From.xlsx").Sheets("FromSheetNameHere").Range("rangehere").Value

Keep in mind if you have macro code in the workbook(s) they will be .xlsm and not .xlsx 请记住,如果工作簿中有宏代码,它们将是.xlsm而不是.xlsx

Try out the code below. 试试下面的代码。 I hope the comments are very clear! 希望评论很清楚!

Option Explicit
Private Sub CommandButton1_Click()

Dim SourceBook As Workbook, DestBook As Workbook
Dim SourceSheet As Worksheet, DestSheet As Worksheet
Dim Index As Long, NumRows As Long
Dim ValidCheck As Boolean

'set references up-front
Set SourceBook = Workbooks.Open("G:\Excel\From.xlsx")
Set SourceSheet = SourceBook.ActiveSheet
With SourceSheet
    NumRows = .Range("A", .Rows.Count).End(xlUp).Row '<~ last row in col A
End With
Set DestBook = Workbooks.Open("G:\Excel\To.xlsx")
Set DestSheet = DestBook.ActiveSheet

'loop through col A and write out values to destination
For Index = 1 To NumRows

    '...
    'do your value validation
    'in this space using ValidCheck
    '...

    If ValidCheck Then '<~ if true write to Dest
        DestSheet.Cells(Index, 1) = SourceSheet.Cells(Index, 1)
    End If '<~ if not, do not write to Dest

Next Index

End Sub

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

相关问题 根据单元格值将行从一个 Excel 工作表复制到另一个 - Copying rows from one Excel sheet to another based on cell value 使用VBA将单元格值保留在Excel中保留格式从一个单元格到另一个单元格 - Copying the cell value preserving the formatting from one cell to another in excel using VBA 使用vba将单元格从一个工作簿复制到excel中的另一个工作簿 - Copying the cell from One workbook to another workbook in excel using vba Excel VBA - 将单元格内容从一张纸复制到另一张纸 - Excel VBA - copying cell contents from one sheet to another 使用VBA将单元格值复制到Excel中的另一个工作表 - Copying Cell Value to another sheet in Excel with VBA Excel - 将内容从一个单元格复制到另一个单元格(有条件) - Excel - Copying content from a cell to another ( with condition ) Excel根据第三个单元格的值将一个单元格的值复制到另一个单元格 - Excel copying value of one cell into another based off value of third cell 从 Excel 中的另一个单元格控制单元格值 - Control cell value from another cell in Excel Excel / Google表格:在“ ISTEXT”条件下将数据从一个单元格复制到另一选项卡和单元格 - Excel/Google Sheets: Copying data from one cell upon “ISTEXT” condition to another tab and cell excel将从单元格1找到的字符串复制到新单元格 - excel copying string found from cell one to a new cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM