• 
    

    
    

      99热精品在线国产_美女午夜性视频免费_国产精品国产高清国产av_av欧美777_自拍偷自拍亚洲精品老妇_亚洲熟女精品中文字幕_www日本黄色视频网_国产精品野战在线观看 ?

      VBA實現(xiàn)按合并單元格拆分Excel工作表

      2018-06-19 02:14:18姚曄石翠
      無線互聯(lián)科技 2018年3期

      姚曄 石翠

      摘要:在Excel應用中,部門信息常見的格式設置,一種是占據(jù)一列,另一種格式為合并單元格,占據(jù)一行,文章討論的是后一種,通過合并單元格的判定,確定要包含的數(shù)據(jù)范圍,然后復制到以合并單元格提取的內容作為新建工作簿命名的文件中去。

      關鍵詞:VBA;合并單元格;Excel

      1 問題提出

      在Excel應用中,常會遇到以合并單元格拆分數(shù)據(jù)[1],如圖1所示。

      2 解決方法

      經(jīng)過多次嘗試,在Excel表格中,簡單的函數(shù)功能無法實現(xiàn),因此選擇采用宏(Macro)來實現(xiàn)。 宏是一種批處理,是一些命令組織在一起,作為一個單獨命令完成一個特定任務。Microsoft O伍ce辦公軟件自動集成“VBA”高級程序語言,使用宏語言Visual Basic將宏作為一系列指令來編寫[2]。

      定義宏seprateExcel

      Sub seprateExcel()

      End sub

      2.1合并單元格的判定

      合并單元格為班級信息,通過MergeCells來判定[3]。

      Duni As Long

      Dim titleCounts As Long

      titleCounts=3

      Dun RowCounts As Long

      RowCounts= Cells(Rows.Count, l).End(xlUp).Row

      Dim cel As Range

      For i=titleCounts+1 To RowCounts

      Set cel= Range("A"&i;)

      If cel.MergeCells Then

      End If

      Nexti

      End Sub

      2.2計算列

      計算總列數(shù)的前提條件是最后一列必須保證為數(shù)據(jù),因為列標題行所有單元格都有數(shù)據(jù),因此,選擇標題行計算工作表的列數(shù),再以列數(shù)得到最后一列的字母列名,這樣無論是Office哪一個版本,都可以通過計算獲得[4]。

      columnTitleRow=3

      Dim ColumnCounts As Long

      ColumnCounts=Cells(columnTitleRow, Columns.Count).End(xlToLeft).Column

      卜計算最后一列的列名

      Dim columnLetter As String

      num= ColumnCounts

      Do While num>0

      columnLetter=Chr《num -1)Mod 26+65)&columnLetter;

      num= Int《num -1)/26)

      Loop

      2.3提取合并單元格內容

      titleCounts=3

      RowCounts= Cells(Rows.Count, l).End(xlUp).Row

      Dim sourceFile As String

      sourceFile=”成績表.xls"

      Dim targetFile As String

      Dim targetFilewithPath As String

      Dim cel As Range

      For i=titleCounts+1 To RowCounts

      Set cel= Range("A"&i;)

      Ifcel.MergeCells Then

      targetFile= Trim(cel)&".xls"

      targetFilewithPath=ThisWorkbook.Path&”\”&Trim;(cel)&”.xls”

      Workbooks.Add

      ActiveWorkbook.SaveAs targetFilewithPath

      Windows(sourceFile).Activate

      End If

      Nexti

      2.4復制表頭

      Windows(sourceFile).Activate

      ThisWorkbook.Sheets("sheetl").Activate

      Range("Al:"&columnLetter;&titleCounts;).Copy

      Windows(targetFile).Activate

      Range("Al").Select

      ActiveSheet.Paste

      2.5復制內容

      拆分工作表,首先確定拆分范圍。起止行的判定也是通過合并單元格的判定來完成。

      Dim BeginRow As Long, EndRow As Long

      For i=titleCounts+1 To RowCounts

      Set cel= Range("A"&i;)

      Ifcel.MergeCells Then

      Ifi>titleCounts+1 Then

      EndRow=i-1

      Windows(sourceFile).Activate

      ThisWorkbook.Sheets (" sheetl").Activate

      Range( "A" & BeginRow & ":" & columnLetter &EndRow;).Copy

      Windows(targetFile).Activate

      Range("Al").Select

      ActiveSheet.Paste

      End If

      targetFile= Trim(cel)&”.xls” targetFilewithPath=ThisWorkbook.Path&”\”&Trim;(cel)&”.xls”

      Workbooks.Add

      ActiveWorkbook.SaveAs targetFilewithPath

      Windows(sourceFile).Activate

      BeginRow= 1+1

      End If

      Nexti

      EndRow= RowCounts

      Windows(sourceFile).Activate

      ThisWorkbook.Sheets(" sheetl").Activate

      Range("A"&BeginRow;&”:”&columnLetter;&EndRow;).Copy

      Windows(targetFile).Activate

      Range("Al").Select

      ActiveSheet.Paste

      3 結語

      由于微軟辦公自動化軟件Office的普及,包含在其中的各個組件都可以利用VBA,VBA的應用有助于使工作自動化,可以使這些軟件的應用獲得更高的工作效率。另外,由于VBA可以直接應用Office套裝軟件的各項強大功能,所以,對Office進行二次程序設計和程序開發(fā)更加方便快捷。

      [參考文獻]

      [1]廖麗嶸基于Excel VBA的成績管理系統(tǒng)[J].現(xiàn)代計算機(專業(yè)版),2012( 23):64-66

      [2]高世萍,翟濱,廉鎖原.基于Excel函數(shù)及Excel VBA的成績分析[J].電腦學習,2011(1):18-19.

      [3]李利民.基于Exce12007 VBA的學生成績管理系統(tǒng)的設計與實現(xiàn)[J]電腦知識與技術,2010(5):1128-1129,1137

      [4]格林.Excel 2007 VBA參考大全[M].Excel Home,譯北京:人民郵電出版社,2009

      额尔古纳市| 承德县| 柘城县| 正定县| 绥宁县| 无棣县| 二连浩特市| 晋江市| 城步| 布尔津县| 德保县| 竹山县| 临沭县| 花垣县| 罗江县| 北票市| 和硕县| 新竹市| 高唐县| 分宜县| 临邑县| 夏津县| 汶川县| 晴隆县| 龙南县| 综艺| 青州市| 酒泉市| 台东市| 邮箱| 营口市| 娄底市| 保靖县| 邵阳市| 武功县| 玉屏| 宜川县| 大埔县| 沅陵县| 濮阳县| 襄垣县|