将excel电子表格合并到一个电子表格中 [英] Merging excel spreadsheets into one spreadsheet

查看:244
本文介绍了将excel电子表格合并到一个电子表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我试图寻找类似的问题,但是我并没有明白什么是正在讨论的,因为这是我第一次看Excel的VBA编辑器。



简单来说,我有2个电子表格:Sheet1和Sheet2



表1:

  AB 
1 Header1 Header2
2 Text1 Info1
3 Text2 Info2

表2:

  AB 
1 Header1 Header2
2 Text3 Info3
3 Text4 Info4

我想要一个宏合并这两张表格变成一张新表(Sheet3),如下所示:

  AB 
1 Header1 Header2
2 Text1 Info1
3 Text2 Info2
4 Text3 Info3
5 Text4 Info4

我已尝试录制宏并保存以备以后使用。为此,我创建了一个新的工作表,将Sheet1中的所有内容复制/粘贴到Sheet3,然后将除Sheet2之外的所有信息复制到Sheet3。



嗯,宏为这个数据工作,但是我发现由excel生成的代码使得它在粘贴数据之前选择单元格A4(这里)。虽然这适用于这些数据,但是如果每张表中的记录数量一再改变,则不起作用。基本上,



1)我想知道是否有一个功能在粘贴下一组数据之前自动进入最后一个相关单元格(在本例中,单元格A4,如果我还有一个表,那么单元格A6)。



2)我看到了ActiveCell.SpecialCells(xlLastCell).Select使用Ctrl + End),但是将其带到表格的末尾。使用该功能后,我将需要类似Home和Down箭头键的功能。



这些选项中的任一个与我都很好。 ^ _ ^



这是我现在在Excel 2010中记录的宏记录器中的当前VBA代码:

  Sub Collat​​e_Sheets()

Sheets.Add After:= Sheets(Sheets.Count)
床单(Sheets.Count)。选择
床单(床单。选择
范围(A1)。选择
范围(选择,ActiveCell.SpecialCells(xlLastCell))。选择
Selection.Copy
表格(Sheet3)。选择
ActiveSheet.Paste
ActiveCell.SpecialCells(xlLastCell)。选择
'我需要在下面选择一个单元格,和列A中的单元格
表单(Sheet2)。选择
范围(A2)。选择
范围(选择,ActiveCell.SpecialCells(xlLastCell))。
Application.CutCopyMode = False
Selection.Copy
表格(Sheet3)。选择
ActiveSheet.Paste
End Sub

我希望我没有忘记任何有用的信息。让我知道,如果我没有!

解决方案

Jerry,尝试这段代码。我清理了你的代码,使它更有效率,以便能够做你想要的。我根据你的代码说我认为是正确的做了一些假设。如果没有,请评论这个答案,如果需要,我会进行调整。

  Option Explicit 

Sub Collat​​e_Sheets ()


Sheets.Add After:= Sheets(Sheets.Count)
Dim wks As Worksheet
设置wks = Sheets(Sheets.Count)

wks.Name =Sheet3

带表格(Sheet1)

Dim lastrow As Long
lastrow = .Range(B & .Rows.Count).End(xlUp).Row

.Range(A1:B& lastrow).Copy wks.Range(A& wks.Rows。计数).End(xlUp)

结束

带表格(Sheet2)

lastrow = .Range(B& .Rows.Count).End(xlUp).Row

.Range(A2:B& lastrow).Copy wks.Range(A& wks.Rows.Count)。结束(xlUp).Offset(1)

结束


结束Sub


Okay, I tried to look for similar questions but I didn't understand much of what was being discussed since it's the first time I'm looking at Excel's VBA editor.

In simple terms, I have 2 spreadsheets: "Sheet1" and "Sheet2"

Sheet 1:

    A         B
1 Header1   Header2
2 Text1     Info1
3 Text2     Info2

Sheet 2:

    A         B
1 Header1   Header2
2 Text3     Info3
3 Text4     Info4

And I would like to have a macro to merge the two sheets into a new sheet (Sheet3), like this:

    A         B
1 Header1   Header2
2 Text1     Info1
3 Text2     Info2
4 Text3     Info3
5 Text4     Info4

I have tried recording a macro and saving it for later use. To do this, I created a new sheet, copy/paste everything from Sheet1 to Sheet3, then copy all the information except the headings from Sheet2 to Sheet3.

Well, the macro works for this data, but I found that the code generated by excel makes it so it selects the cell A4 (here) before pasting the data. While this works for this data, it wouldn't work if the number of records in each sheet changes now and again. Basically,

1) I was wondering if there was a function that goes to the last relevant cell automatically before pasting the next set of data (in this example, cell A4, and if I have one more table, then cell A6).

2) I've seen the function "ActiveCell.SpecialCells(xlLastCell).Select" (activated when I use Ctrl+End) but that carries me to the end of the sheet. I would need something similar to "Home" and "Down" arrow key after using that function for it to work best.

Either one of those options would be good with me. ^_^

Here's my current VBA code recorded from the Macro Recorder in excel 2010:

Sub Collate_Sheets()

    Sheets.Add After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Select
    Sheets(Sheets.Count).Name = "Sheet3"
    Sheets("Sheet1").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Sheet3").Select
    ActiveSheet.Paste
    ActiveCell.SpecialCells(xlLastCell).Select
    ' I need to select one cell below, and the cell in column A at this point
    Sheets("Sheet2").Select
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Sheet3").Select
    ActiveSheet.Paste
End Sub

I hope I didn't forget any useful piece of information. Let me know if I did!

解决方案

Jerry, try this code. I cleaned up your code a bit and made it more efficient to be able to do what you wish. I've made some assumptions based on what your code said which I think are right. If not, comment on this answer and I will tweak if needed.

Option Explicit

Sub Collate_Sheets()


   Sheets.Add After:=Sheets(Sheets.Count)
   Dim wks As Worksheet
   Set wks = Sheets(Sheets.Count)

   wks.Name = "Sheet3"

   With Sheets("Sheet1")

    Dim lastrow As Long
    lastrow = .Range("B" & .Rows.Count).End(xlUp).Row

    .Range("A1:B" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp)

   End With

   With Sheets("Sheet2")

    lastrow = .Range("B" & .Rows.Count).End(xlUp).Row

    .Range("A2:B" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1)

   End With


End Sub

这篇关于将excel电子表格合并到一个电子表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆