excel 在VBA中如何循环到最后一个非空行?

hrirmatl  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(393)

我有下面的VBA代码,它允许我从文件中的Excel行填充Internet Explorer上Intranet站点中的字段,但现在我想知道是否有一种方法可以循环到最后一行非空,而无需手动重复这些行,以下是代码:

`Sub template()
Application.DisplayAlerts = False
Application.WindowState = xlMinimized
On Error Resume Next
Dim objApp
Dim objIE
Dim objWindow
Dim ie As Object
Dim strURL
Set ie = CreateObject("InternetExplorer.Application")
Set objApp = CreateObject("Shell.Application")
Set objIE = Nothing
 
strURL = "http://Intranet"
 
For Each objWindow In objApp.Windows
  If (InStr(objWindow.Name, "Internet Explorer")) Then
    If (objWindow.LocationURL = strURL) Then
      Set objIE = objWindow
      Exit For
    End If
  End If
Next
 
If (objIE Is Nothing) Then
  Set objIE = CreateObject("InternetExplorer.Application")
  objIE.navigate (strURL)
End If
 
With objIE
  .Visible = True
  Do While .Busy Or .readyState <> 4
  Loop
 
  Do While .document.readyState <> "complete"
  Loop
 

**the first line:**
 
.document.getelementbyID("inputND").Value = Worksheets("Feuil1").Range("A2")
.document.getelementbyID("inputND").innerText = Worksheets("Feuil1").Range("A2") 
.document.getElementsByTagName("button")(1).Click
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("categorisation_1").Value = Worksheets("Feuil1").Range("B2") & ": Object" '.Value = ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("categorisation_2").Value = Worksheets("Feuil1").Range("D2") & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("categorisation_3").Value = Worksheets("Feuil1").Range("F2") & ": Object"
Application.Wait Now + TimeValue("00:00:01")
 
 
MsgBox "Please check before continuing", vbMsgBoxSetForeground + vbSystemModal
 
**the second line:**
 
.document.getelementbyID("link61").Click
.navigate "http://Intranet"
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("inputND").Value = Worksheets("Feuil1").Range("A3") 
.document.getelementbyID("inputND").innerText = Worksheets("Feuil1").Range("A3") 
.document.getElementsByTagName("button")(1).Click
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("categorisation_1").Value = Worksheets("Feuil1").Range("B3") & ": Object" '.Value = ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("categorisation_2").Value = Worksheets("Feuil1").Range("D3") & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getelementbyID("categorisation_3").Value = Worksheets("Feuil1").Range("F3") & ": Object"
Application.Wait Now + TimeValue("00:00:01")
 
MsgBox "Please check before continuing", vbMsgBoxSetForeground + vbSystemModal
.document.getelementbyID("link61").Click

**the third line:**
'........................
 
End With
Application.DisplayAlerts = True
 
End Sub`

第一行:Worksheets("Feuil1").Range("A2");Worksheets("Feuil1").Range("B2");Worksheets("Feuil1").Range("D2");Worksheets("Feuil1").Range("F2")
第二行:Worksheets("Feuil1").Range("A3");Worksheets("Feuil1").Range("B3");Worksheets("Feuil1").Range("D3");Worksheets("Feuil1").Range("F3")
第三行:Worksheets("Feuil1").Range("A4");Worksheets("Feuil1").Range("B4");Worksheets("Feuil1").Range("D4");Worksheets("Feuil1").Range("F4")
依此类推,直到最后一个非空行。

我不希望代码的以下部分每次在MsgBox之后重复,我希望代码进入sheet1的第二行,以此类推,直到最后一个非空行

.document.getelementbyID("inputND").Value = Worksheets("Feuil1").Range("A2")
    .document.getelementbyID("inputND").innerText = Worksheets("Feuil1").Range("A2") 
    .document.getElementsByTagName("button")(1).Click
    Application.Wait Now + TimeValue("00:00:01")
    .document.getelementbyID("categorisation_1").Value = Worksheets("Feuil1").Range("B2") & ": Object" '.Value = ": Object"
    Application.Wait Now + TimeValue("00:00:01")
    .document.getelementbyID("categorisation_2").Value = Worksheets("Feuil1").Range("D2") & ": Object"
    Application.Wait Now + TimeValue("00:00:01")
    .document.getelementbyID("categorisation_3").Value = Worksheets("Feuil1").Range("F2") & ": Object"
    Application.Wait Now + TimeValue("00:00:01")
 
MsgBox "Please check before continuing", vbMsgBoxSetForeground + vbSystemModal

我希望现在能说得更清楚,但如果你有任何问题,请问月份,谢谢

sshcrbum

sshcrbum1#

您只会得到最后一个填充行:

lastRow = Cells(Rows.Count, 1).End(xlUp).Row

并且,根据您要关注的列,您可以放置适当的索引:1 - A、2 - B等,作为Cells中的第二个参数

vxf3dgd4

vxf3dgd42#

我在checkbox方法上做了一些改进,所以我做了如下操作:

Sub Addcheckboxes()
Dim cell, LRow As Single
Dim chkbx As CheckBox
Dim MyLeft, MyTop, MyHeight, MyWidth As Double
 
Application.ScreenUpdating = False
LRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
 
For cell = 2 To LRow
    If Cells(cell, "A").Value <> "" Then
        MyLeft = Cells(cell, "O").Left
        MyTop = Cells(cell, "O").Top
        MyHeight = Cells(cell, "O").Height
        MyWidth = Cells(cell, "O").Width
        ActiveSheet.CheckBoxes.Add(MyLeft, MyTop, MyWidth, MyHeight).Select
        With Selection
            .Caption = ""
            .Value = xlOff
            .Display3DShading = False
        End With
    End If
Next cell
 
Application.ScreenUpdating = True
 
End Sub
 
 
Sub Template()
Application.DisplayAlerts = False
For Each chkbx In ActiveSheet.CheckBoxes
    If chkbx.Value = 1 Then
        For r = 1 To Rows.Count
            If Cells(r, 1).Top = chkbx.Top Then
Application.WindowState = xlMinimized
'On Error Resume Next
Dim objApp
Dim objIE
Dim objWindow
Dim ie As Object
Dim strURL
Set ie = CreateObject("InternetExplorer.Application")
Set objApp = CreateObject("Shell.Application")
Set objIE = Nothing
 
strURL = "http://intranet"
 
'Identify the IE window and connect.
 
For Each objWindow In objApp.Windows
  If (InStr(objWindow.Name, "Internet Explorer")) Then
    If (objWindow.LocationURL = strURL) Then
      Set objIE = objWindow
      Exit For
    End If
  End If
Next
 
If (objIE Is Nothing) Then
  Set objIE = CreateObject("InternetExplorer.Application")
  objIE.navigate (strURL)
End If
 
With objIE
  .Visible = True
  Do While .Busy Or .readyState <> 4
  Loop
 
  Do While .document.readyState <> "complete"
  Loop
 
.document.getElementById("inputND").Value = Worksheets("Sheet1").Range("A" & r)
.document.getElementById("inputND").innerText = Worksheets("Sheet1").Range("A" & r) 
.document.getElementsByTagName("button")(1).Click
Application.Wait Now + TimeValue("00:00:02")
.document.getelementsByClassName("grc-label ml-2")(0).getelementsByClassName("flag-icon fas fa-mobile-alt fa-lg ng-star-inserted")(0).Click
Application.Wait Now + TimeValue("00:00:01")
.document.getelementsByClassName("btn btn-grc-outline-mobile btn-rounded mr-1 mb-3 ladda-button")(0).Click
Application.Wait Now + TimeValue("00:00:01")
.document.querySelector(".ng-star-inserted [value='5: Object']").Selected = True
.document.getElementById("categorisation_1").Value = Worksheets("Sheet1").Range("B" & r) & ": Object" '.Value = ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("categorisation_2").Value = Worksheets("Sheet1").Range("D" & r) & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("categorisation_3").Value = Worksheets("Sheet1").Range("F2" & r) & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("categorisation_4").Value = Worksheets("Sheet1").Range("H2" & r) & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("categorisation_5").Value = Worksheets("Sheet1").Range("J2" & r) & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("categorisation_6").Value = Worksheets("Sheet1").Range("L2" & r) & ": Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("champContactClient").selectedIndex = 1
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("champVoie").Value = "0: Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("champGravite").Value = "1: Object"
Application.Wait Now + TimeValue("00:00:01")
.document.getElementById("champCommentaire").Value = Worksheets("Sheet1").Range("N" & r)
Application.Wait Now + TimeValue("00:00:01")
MsgBox "Merci de vérifier avant de continuer", vbMsgBoxSetForeground + vbSystemModal
.document.getElementById("link61").Click
.navigate "http://intranet"
 
End With
 Exit For
            End If
        Next r
    End If
Next
Application.DisplayAlerts = True
 
End Sub

检查的第一行工作得很好,但第二行不行,在执行宏后,我用黄色突出显示了以下代码行:

If chkbx.Value = 1 Then

相关问题