运行时错误91对象变量或未设置变量

时间:2018-05-15 10:06:06

标签: excel vba excel-vba runtime-error

当代码到达'在数据库中找到第一个空行时,它突出显示黄色的下两行并显示运行时错误91.任何人都可以告诉我哪里出错了。三江源。

Private Sub diaryenter_Click()
    Dim iRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("sheet1")
    'find first empty row in database
    iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    'copy the data to the database
    With ws
      .Cells(iRow, 115).Value = Me.autodatet.Value
      .Cells(iRow, 116).Value = Me.fdcomments.Value
      .Cells(iRow, 121).Value = Me.fdgs.Value
    End With
    'clear the data
    Me.autodatet.Value = ""
    Me.fdcomments.Value = ""
    Me.fdgs.Value = ""
    Diaryentryf.Hide
    ThisWorkbook.Save
End Sub

3 个答案:

答案 0 :(得分:2)

xlByRows而非xlRows

iRow = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, _
                     SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

答案 1 :(得分:1)

也许添加一个测试,确实存在要停止的数据。如果找不到任何内容,则会出错。

  If Application.WorksheetFunction.Counta(ws.UsedRange) = 0 Then
        iRow = 1
   Else
        iRow = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, _
                             SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
   End If

归功于@Jeeped for xlByRows。

答案 2 :(得分:0)

继续@QHarr评论你应该检查Sub Test1() Dim rLastCell As Range Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("sheet1") Set rLastCell = ws.Cells.Find(What:="*", SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues) If rLastCell Is Nothing Then iRow = 1 Else iRow = rLastCell.Row+1 End If End Sub 是否找到任何内容 (如果没有,它不能返回它没有找到的单元格的行号)。

import java.io.*;
import java.net.*;

public class HTTP {

public static String getHTML() throws Exception {
    StringBuilder result = new StringBuilder();

    String urlToRead = "http://user:pw@ipaddress:port";

    URL url = new URL(urlToRead);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}

public static void main(String[] args) throws Exception {
    System.out.println(getHTML());
}
}

编辑 - 将删除,因为QHarr给出了类似的答案,但找到它的方法是不同的。