【新人发帖】关于VBA的一个很小的问题,怎么都解决不了,求各位老师指点刚学VBA不久,感觉VBA程序很容易看懂,但
【新人发帖】关于VBA的一个很小的问题,怎么都解决不了,求各位老师指点
刚学VBA不久,感觉VBA程序很容易看懂,但是自己编程的时候经常会卡在很多小问题上,而且还调试不出来,为了节省时间,加快学习进度,特地跑来这里向各路大神请教,先谢谢了
Dim keyword As String
Dim key_row As Integer
Dim key_column As Integer
Private Sub CommandButton1_Click()
ActiveWorkbook.Sheets.Add Before:=Worksheets(1)
Worksheets(1).Name = "查询结果"
If keyword <> "" Then
keyword = ""
End If
keyword = TextBox1.Value
Call myfind
End Sub
Private Sub CommandButton4_Click()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
Worksheets("查询结果").Delete
End Sub
Public Function myfind() As String
Dim b As Integer
Dim i As Integer
Dim j As Integer
Dim m As Integer
Dim key_row As Integer
For i = 1 To 12
If Worksheets(i + 1).Cells(key_row, 3) <> "" Then
key_row = key_row + 1
End If
b = key_row
For j = 1 To b
If keyword = Worksheets(i + 1).Cells(j, 3) Then
With Worksheets(i + 1)
Worksheets("查询结果").Cells(Sum, 2) = .Cells(j, 2):
Worksheets("查询结果").Cells(Sum, 3) = .Cells(j, 3):
Worksheets("查询结果").Cells(Sum, 4) = .Cells(j, 4):
Worksheets("查询结果").Cells(Sum, 5) = .Cells(j, 5):
Worksheets("查询结果").Cells(Sum, 6) = .Cells(j, 6):
Worksheets("查询结果").Cells(Sum, 7) = .Cells(j, 7):
Worksheets("查询结果").Cells(Sum, 8) = .Cells(j, 8):
Worksheets("查询结果").Cells(Sum, 9) = .Cells(j, 9)
End With
End If
Next j
Next i
End Function
总是报错:运行时错误“1004”应用程序定义或对象定义错误
这个只是一个半成品,我想通过一个窗体输入要查询的值,然后在EXCEL的12个sheet中进行查找,然后新建一个“查询结果”工作表,让查找到的结果在这里显示出来,感觉是一个很简单的问题,编程思路也是有的,就是在很多小的细节上一错再错,请老师们指点一二,谢谢
[解决办法]我给稍微修改了以下,这个运行起来没有报错,可是也没有把我要找的规格图号找出来,求各位帮忙看一下,谢谢
Dim row_id As Integer
Dim key_id As Integer
Dim endrow As Integer
Dim keyword As String
Dim firstrow As Integer
Dim sheet_i As Integer
Private Sub CommandButton1_Click()
sub_tab_sheet
sub_find_keyword
End Sub
Public Sub sub_tab_sheet() '切换各表格,实现自动查询
sheet_i = 1
For sheet_i = 1 To 12
Sheets(sheet_i + 1).Select
sub_find_keyword
Next sheet_i
End Sub
Public Sub sub_find_keyword() '查找输入的规格图号
keyword = ""
keyword = TextBox1.Value
firstrow = 4
sub_find_endrow
For key_id = firstrow To endrow
If keyword = Left(ActiveSheet.Cells(key_id, 3), Len(keyword)) Then
sub_copy_result
End If
Next key_id
End Sub
Public Sub sub_find_endrow() '找到表格的最后一行
row_id = Cells(65536, 3).End(xlUp).Row
endrow = row_id
End Sub
Public Sub sub_copy_result() '将各表格中查询到的结果复制到表“查询结果”中
Sheets("查询结果").Cells(key_id, 3).Value = Sheets(sheet_i).Cells(key_id, 3).Value
End Sub
[解决办法]
没有人吗,来个高手帮帮忙啊,谢谢了