Foxtable(狐表)用户栏目专家坐堂 → VBA的语句如何改成狐表的?


  共有4136人关注过本帖树形打印复制链接

主题:VBA的语句如何改成狐表的?

帅哥哟,离线,有人找我吗?
wh420
  11楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1107 积分:10574 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2015/6/24 17:16:00 [只看该作者]

好的,多谢大红袍,辛苦。

 回到顶部
帅哥哟,离线,有人找我吗?
wh420
  12楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1107 积分:10574 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2015/6/25 11:21:00 [只看该作者]

改造的代码如下,功能可以正常执行,但新建的sheet1没有被删除,是不是tempWs.Delete语句放的位置不对,请老师帮忙看看

Dim App As New MSExcel.Application
try
    For Each file As String In FileSys.GetFiles(path)
        If file.EndsWith(".xls") OrElse file.EndsWith(".xlsx") Then
            Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(file)
            Dim tempWs = wb.WorkSheets.Add
            For k As Integer = 1 To Wb.WorkSheets.Count
               
                Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(k)
                Dim rg As MSExcel.Range
                For Each rg In ws.UsedRange
                    If rg.MergeCells Then
                        Dim tempCell As MSExcel.Range
                        Dim width As Double = 0
                        Dim tempCol
                        For Each tempcol In rg.MergeArea.Columns
                            width = width + tempcol.ColumnWidth
                        Next
                        tempWs.Columns(1).WrapText = True
                        tempWs.Columns(1).ColumnWidth = width
                        tempWs.Columns(1).Font.Size = rg.Font.Size
                        tempWs.Cells(1, 1).Value = rg.Value
                        tempWs.Cells(1, 1).RowHeight = 0
                        tempWs.Cells(1, 1).EntireRow.Activate
                        tempWs.Cells(1, 1).EntireRow.AutoFit
                        If (rg.RowHeight < tempWs.Cells(1, 1).RowHeight) Then
                            Dim tempHeight As Double
                            Dim tempCount As Integer
                            tempHeight = tempWs.Cells(1, 1).RowHeight
                            tempCount = rg.MergeArea.Rows.Count
                            For Each addHeightRow As object In rg.MergeArea.Rows
                                If (addHeightRow.RowHeight < tempHeight / tempCount) Then
                                    addHeightRow.RowHeight = tempHeight / tempCount
                                End If
                                tempHeight = tempHeight - addHeightRow.RowHeight
                                tempCount = tempCount - 1
                            Next
                            rg.WrapText = True
                        End If
                    End If
                Next
                tempWs.Delete
            Next
           
            Wb.Save
            App.Quit()
            Dim txt1 As WinForm.TextBox = Forms("翻译器").Controls("TextBox3")
            txt1.text = file & vbcrlf & txt1.text & vbcrlf
            Application.DoEvents()
            FileCount=FileCount+1
           
        End If
    Next


 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  13楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/6/25 11:39:00 [只看该作者]

Dim App As New MSExcel.Application
try
    For Each file As String In FileSys.GetFiles(path)
        If file.EndsWith(".xls") OrElse file.EndsWith(".xlsx") Then
            Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(file)
            Dim tempWs = wb.WorkSheets.Add
            For k As Integer = 1 To Wb.WorkSheets.Count - 1
               
                Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(k)
                Dim rg As MSExcel.Range
                For Each rg In ws.UsedRange
                    If rg.MergeCells Then
                        Dim tempCell As MSExcel.Range
                        Dim width As Double = 0
                        Dim tempCol
                        For Each tempcol In rg.MergeArea.Columns
                            width = width + tempcol.ColumnWidth
                        Next
                        tempWs.Columns(1).WrapText = True
                        tempWs.Columns(1).ColumnWidth = width
                        tempWs.Columns(1).Font.Size = rg.Font.Size
                        tempWs.Cells(1, 1).Value = rg.Value
                        tempWs.Cells(1, 1).RowHeight = 0
                        tempWs.Cells(1, 1).EntireRow.Activate
                        tempWs.Cells(1, 1).EntireRow.AutoFit
                        If (rg.RowHeight < tempWs.Cells(1, 1).RowHeight) Then
                            Dim tempHeight As Double
                            Dim tempCount As Integer
                            tempHeight = tempWs.Cells(1, 1).RowHeight
                            tempCount = rg.MergeArea.Rows.Count
                            For Each addHeightRow As object In rg.MergeArea.Rows
                                If (addHeightRow.RowHeight < tempHeight / tempCount) Then
                                    addHeightRow.RowHeight = tempHeight / tempCount
                                End If
                                tempHeight = tempHeight - addHeightRow.RowHeight
                                tempCount = tempCount - 1
                            Next
                            rg.WrapText = True
                        End If
                    End If
                Next
               
            Next
            app.DisplayAlerts = False
            tempWs.Delete
            Wb.Save
           
            Dim txt1 As WinForm.TextBox = Forms("翻译器").Controls("TextBox3")
            txt1.text = file & vbcrlf & txt1.text & vbcrlf
            Application.DoEvents()
            FileCount=FileCount+1
           
        End If
    Next
catch ex As exception
    msgbox(ex.message)
finally
    App.Quit()
End try

 回到顶部
帅哥哟,离线,有人找我吗?
wh420
  14楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1107 积分:10574 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2015/6/25 11:49:00 [只看该作者]

 For k As Integer = 1 To Wb.WorkSheets.Count - 1
-1之后最后一个标签不能被执行代码

 回到顶部
帅哥哟,离线,有人找我吗?
大红袍
  15楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:贵宾 帖子:39310 积分:196782 威望:0 精华:1 注册:2015/4/25 9:23:00
  发帖心情 Post By:2015/6/25 11:52:00 [只看该作者]

那不要 For k As Integer = 1 To Wb.WorkSheets.Count


 回到顶部
帅哥哟,离线,有人找我吗?
wh420
  16楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1107 积分:10574 威望:0 精华:0 注册:2014/2/19 16:32:00
  发帖心情 Post By:2015/6/25 12:05:00 [只看该作者]

多谢,问题解决。

 回到顶部
总数 16 上一页 1 2