瀏覽量:103次
如何將多個(gè)Excel文件合并成一個(gè)且保留原有數(shù)據(jù)?事實(shí)上,我們可以借用Excel宏命令來處理該操作,并且保留原有數(shù)據(jù),建立多個(gè)Sheet頁,具體操作請(qǐng)看下文多個(gè)Excel文件合并成一個(gè)的方法。
如何將多個(gè)Excel文件合并成一個(gè)且保留原有數(shù)據(jù)?
1、將需合并的Excel文件放在同一個(gè)文件夾中,并這個(gè)文件中新建一個(gè)Excel文件。
2、打開新建的Excel文件,按 Alt + F11 鍵,打開宏,選擇視圖→代碼窗口。
3、將下面的代碼拷貝進(jìn)去:
01Sub sheets2one()02'定義對(duì)話框變量03Dim cc As FileDialog04Set cc = Application.FileDialog(msoFileDialogFilePicker)05Dim newwork As Workbook06Set newwork = Workbooks.Add07With cc08If .Show = -1 Then09Dim vrtSelectedItem As Variant10Dim i As Integer11i = 112For Each vrtSelectedItem In .SelectedItems13Dim tempwb As Workbook14Set tempwb = Workbooks.Open(vrtSelectedItem)15tempwb.Worksheets(1).Copy Before:=newwork.Worksheets(i)16newwork.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xls", "")17tempwb.Close SaveChanges:=False18i = i + 119Next vrtSelectedItem20End If21End With22Set cc = Nothing23End Sub復(fù)制代碼Sub sheets2one()'定義對(duì)話框變量Dim cc As FileDialogSet cc = Application.FileDialog(msoFileDialogFilePicker)Dim newwork As WorkbookSet newwork = Workbooks.AddWith ccIf .Show = -1 ThenDim vrtSelectedItem As VariantDim i As Integeri = 1For Each vrtSelectedItem In .SelectedItemsDim tempwb As WorkbookSet tempwb = Workbooks.Open(vrtSelectedItem)tempwb.Worksheets(1).Copy Before:=newwork.Worksheets(i)newwork.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".xls", "")tempwb.Close SaveChanges:=Falsei = i + 1Next vrtSelectedItemEnd IfEnd WithSet cc = NothingEnd Sub[聲明]本網(wǎng)轉(zhuǎn)載網(wǎng)絡(luò)媒體稿件是為了傳播更多的信息,此類稿件不代表本網(wǎng)觀點(diǎn),本網(wǎng)不承擔(dān)此類稿件侵權(quán)行為的連帶責(zé)任。故此,如果您發(fā)現(xiàn)本網(wǎng)站的內(nèi)容侵犯了您的版權(quán),請(qǐng)您的相關(guān)內(nèi)容發(fā)至此郵箱【779898168@qq.com】,我們?cè)诖_認(rèn)后,會(huì)立即刪除,保證您的版權(quán)。
官網(wǎng)優(yōu)化
整站優(yōu)化
渠道代理
400-655-5776