将VBA unmerge和fil转换为VBscript unmerge并填充

时间:2017-11-08 18:45:35

标签: excel vba excel-vba vbscript

您好我正在尝试将VBA转换为VBscript但遇到麻烦,因为我不知道从哪里开始。任何帮助表示赞赏。

VBA:取消合并单元格,然后将数据从原始合并数据单元格填充到这些单元格。

Sub UnMergeFill()

Dim cell As Range, joinedCells As Range

For Each cell In ThisWorkbook.ActiveSheet.UsedRange
    If cell.MergeCells Then
        Set joinedCells = cell.MergeArea
        cell.MergeCells = False
        joinedCells.Value = cell.Value
    End If
Next

End Sub

VBScript:我有代码的开头打开正确的excel文件并保存并关闭,但不是VBA脚本正在进行的修改。

'create the excel object
    Set objExcel = CreateObject("Excel.Application") 

'view the excel program and file, set to false to hide the whole process
    objExcel.Visible = True 

'open an excel file (make sure to change the location) .xls for 2003 or earlier
    Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\vbsTest.xlsx")

'save the existing excel file
    objWorkbook.Save

'close the workbook
    objWorkbook.Close 

'exit the excel program
    objExcel.Quit

'release objects
    Set objExcel = Nothing
    Set objWorkbook = Nothing

1 个答案:

答案 0 :(得分:0)

VBscript代码:

'Microsoft Excel Automation
':: Open, unmerge cells and fill then save
'---------------------------------
'create the excel object
    Set objExcel = CreateObject("Excel.Application") 

'view the excel program and file, set to false to hide the whole process
    objExcel.Visible = True 

'open an excel file (make sure to change the location) .xls for 2003 or earlier
    Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\Book3.xlsx")

Dim cell
Dim joinedCells

For Each cell In objWorkbook.ActiveSheet.UsedRange
    If cell.MergeCells Then
        Set joinedCells = cell.MergeArea
        cell.MergeCells = False
        joinedCells.Value = cell.Value
    End If
Next

'save the existing excel file
    objWorkbook.Save

'close the workbook
    objWorkbook.Close 

'exit the excel program
    objExcel.Quit

'release objects
    Set objExcel = Nothing
    Set objWorkbook = Nothing