vba开发实例教程

上面为vba要处理的excel页面展示;

2、

如下是处理的脚本

'主方法
Sub main_function()

Dim MyUrl As String

MyUrl = Range("B7").Value
MyUrl = "http://" & MyUrl & ":8080/Citics/switch.jsp?userName=" & Range("b5").Text & "&password=" + Range("b6").Text

'Get the HTML of the URL
Set IE = CreateObject("InternetExplorer.Application")

IE.navigate MyUrl

While IE.Busy
DoEvents
Wend

With ActiveSheet.UsedRange
    iEndRowT = .Rows.Count + .Row - 1
    iEndColumnT = .Columns.Count + .Column - 1
End With
If iEndRowT > 10 Then
    '遍历
    result = for_url(iEndRowT, IE)
   
    MsgBox "执行完成"
Else
    MsgBox "没有要操作的数据,正常退出"
End If
   
End Sub

 

 

Function for_url(ByVal iEndRowT, ByVal IE) As Integer
Application.ScreenUpdating = False '禁止刷新
For startrow = 11 To iEndRowT
     'excel名称
     Dim workname As String
     'sheet名称
     Dim sheetname As String
     workname = ActiveSheet.Range("B" & startrow).Value
     sheetname = ActiveSheet.Range("C" & startrow).Value
     If workname <> "" Then
        '文件路径
         Dim path As String
         path = ThisWorkbook.path & "\" & workname
        
         Dim Sht As Worksheet
         Set Sht = workbooks.Open(path).Sheets(sheetname)
        
         With ActiveWorkbook.Worksheets(sheetname).UsedRange
                 iEndRow = .Rows.Count + .Row - 1
                 iEndColumn = .Columns.Count + .Column - 1
         End With
   
         If iEndRow > 1 Then
                 iEndRow = iEndRow + 1
         End If
         '获取url
         Dim strurla As String
         strurla = ActiveSheet.Range("A" & startrow).Value
         strurla = Replace(strurla, "beginDate", "endDate2")
         strurla = Replace(strurla, "endDate", "endDate2")
        
          '获取开始日期
         recdateStart = ActiveSheet.Range("B8").Value
         strurla = strurla & "&beginDate=" & recdateStart & "&endDate=" & recdateStart
         'MsgBox strurla
         IE.navigate strurla
         While IE.Busy
         DoEvents
         Wend
   
         irow = iEndRow '从哪行开始显示
         '表格中插入数据
         result = insert(irow, IE, startrow, Sht)
       
         ActiveWorkbook.Save
     Else
        MsgBox "在" & startrow & "行是空行,请删除空行或者是有空格,请您补全空格,出问题的这一行将会跳过执行!"
     End If
Next startrow
Application.ScreenUpdating = True '恢复刷新
   
End Function

 

'表格中插入数据
Function insert(ByVal irow, ByVal IE, ByVal startrow, ByVal Sht) As Integer

    icol = 0

    Dim ilength As Integer
    ilength = IE.document.all.tags("td").Length

    Dim MyArray() As String
   
    ReDim MyArray(ilength + 1)
   
    k = 1
    For Each d In IE.document.all.tags("td")
        MyArray(k) = d.innerText
        k = k + 1
    Next
   
    Dim tr_length As Integer
    '插入的条数
    tr_length = IE.document.all.tags("tr").Length
   
    If tr_length = 2 Then
        tr_length = tr_length - 2
    End If
   
    '当条数只有一条的是很说明只有标题,不进行excel的插入,退出本次循环
    If tr_length <= 0 Then
        ActiveSheet.Range("D" & startrow).Value = 0
    Else
        ActiveSheet.Range("D" & startrow).Value = tr_length - 2
        tr_length = tr_length - 1 '去掉第一个tr
        For Each r In IE.document.all.tags("tr")
       
        cellcol = 1 '从那列开始显示
        Dim td_length As Integer
        td_length = IE.document.all.tags("td").Length - 2
        Dim next_row As Integer
        next_row = td_length / tr_length
        For coloop = 3 + icol To td_length + 2
            icol = icol + 1
            If icol > next_row Then '去掉标题,标题不往excel中写
                Sht.Cells(irow, cellcol) = MyArray(coloop)
                cellcol = cellcol + 1
                If (icol Mod (next_row) = 0) Then Exit For
            End If
         Next coloop
        
         irow = irow + 1
       
        Next
    End If

End Function

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OfficeTips Home || VBA Section || General Section || Download Section || Privacy Policy Useful PowerPoint VBA code snippets More Sharing Services Share | Share on gmail Share on google Share on facebook Share on twitter Determine the current slide in the Slide View mode: Sub SlideIDX() MsgBox "The slide index of the current slide is:" & _ ActiveWindow.View.Slide.SlideIndex End Sub Determine the current slide in Slide Show mode: Sub SlideIDX() MsgBox "The slide index of the current slide is:" & _ ActivePresentation.SlideShowWindow.View.Slide.SlideIndex End Sub Difference between SlideIndex property and SlideNumber property: The SlideIndex property returns the actual position of the slide within the presentation. The SlideNumber property returns the PageNumber which will appear on that slide. This property value is dependent on "Number Slide from" option in the Page Setup. Go to Page Setup and Change the value of "Number Slide from" to 2 and then while on the 1st slide in Slide View run the following Macro Sub Difference() MsgBox "The Slide Number of the current slide is:" & _ ActiveWindow.View.Slide.SlideNumber & _ " while the Slide Index is :" & _ ActiveWindow.View.Slide.SlideIndex End Sub Macro to exit all running slide shows: Sub ExitAllShows() Do While SlideShowWindows.Count > 0 SlideShowWindows(1).View.Exit Loop End Sub Code to refresh current slide during the slide show: Sub RefreshSlide() Dim lSlideIndex As Long lSlideIndex = SlideShowWindows(1).View.CurrentShowPosition SlideShowWindows(1).View.GotoSlide lSlideIndex End Sub Code to reset animation build for the current slide during the slide show: Sub ResetSlideBuilds() Dim lSlideIndex As Long lSlideIndex = SlideShowWindows(1).View.CurrentShowPosition SlideShowWindows(1).View.GotoSlide lSlideIndex, True End Sub Insert a slide after current slide Sub InsertSlide() Dim oView As View With ActivePresentation.Slides Set oView = ActiveWindow.View oView.GotoSlide .Add(oView.Slide.SlideIndex + 1, _ ppLayoutTitleOnly).SlideIndex Set oView = Nothing End With End Sub Copyright 1999-2011 (c) Shyam Pillai. All rights reserved.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值