所以我对VBA很新,但在SQL和一些基本的编码实践方面有很多经验。
我尝试编写一个循环来查找某些标题的名称,然后将其删除,但在第一个if
函数中,它会抛出error 91 "Object Variable or With block variable not set"
...知道我做错了什么?源代码如下:
'Deletes system specific fields that aren't required
Public Sub Delete_Fields_Per_System()
Dim v_count_colum As Integer
Dim v_count_colum_letter As String
Dim i2 As Integer
With Sheets("Results_Detail")
i2 = 0
' Declare loop to replace four different columns for this system
Do While i2 < 4
'Change String to different headers for each loop
If i2 = 0 Then
v_count_colum = .Cells.Find("Create Time").Column
ElseIf i2 = 1 Then
v_count_colum = .Cells.Find("Post Time").Column
ElseIf i2 = 2 Then
v_count_colum = .Cells.Find("Approver ID").Column
Else
v_count_colum = .Cells.Find("Approver Name").Column
End If
'For each header, find number in the list, then corresponding letter and delete
If v_count_colum > 26 Then
v_count_colum_letter = Chr(Int((v_count_colum - 1) / 26) + 64) & Chr(((v_count_colum - 1) Mod 26) + 65)
Else
v_count_colum_letter = Chr(v_count_colum + 64)
.Columns(v_count_colum_letter).EntireColumn.Delete
End If
i2 = i2 + 1
Loop
End With
End Sub
答案 0 :(得分:1)
看起来搜索无法找到所提供的字符串,这意味着它在尝试分配“没有任何内容”时会导致错误。到整数变量。
价值是&#34;创造时间&#34;肯定在你正在寻找的表格上?尝试搜索工作表上明确的内容,然后它应该能够将列整数分配给变量。
如果您需要搜索工作表,即使在您搜索的字符串不明显的情况下,您最好将找到的单元格指定给某个范围,然后如果范围不是什么,您可以指定列整数值。以下应该适合你。
Dim rng As Range
If i2 = 0 Then
Set rng = .Cells.Find("Create Time")
ElseIf i2 = 1 Then
Set rng = .Cells.Find("Post Time")
ElseIf i2 = 2 Then
Set rng = .Cells.Find("Approver ID")
Else
Set rng = .Cells.Find("Approver Name")
End If
If Not rng Is Nothing Then
v_count_colum = rng.Column
rng = Nothing
End If