#excel #excel-2007 #vba
#excel #excel-2007 #vba
Вопрос:
У меня есть рабочая книга с одним «исходным» рабочим листом и несколькими целевыми листами. по сути, исходный лист содержит информацию, которую мне нужно сопоставить и разделить на членов команды. У меня есть следующий код, который замораживает Excel на мне, как будто он застрял в бесконечном цикле. VBA существует на VBA исходного листа.
Sub SearchForString()
Dim ws As Worksheet
Dim x As Integer
Dim y As Integer
Dim z as Integer
x = 1
y = 1
z = 4 'in this case we are looking at column D as the last non-criteria column
For Each ws In Worksheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7"))
x = 1 'setting back to row 1 to grab headers
y = 1
ws.UsedRange.ClearContents
Worksheets(ws.Name).Cells(y, 1) = Cells(x, 1)
Worksheets(ws.Name).Cells(y, 1).Font.Bold = True
Worksheets(ws.Name).Cells(y, 2) = Cells(x, 2)
Worksheets(ws.Name).Cells(y, 2).Font.Bold = True
Worksheets(ws.Name).Cells(y, 3) = Cells(x, 3)
Worksheets(ws.Name).Cells(y, 3).Font.Bold = True
Worksheets(ws.Name).Cells(y, 4) = Cells(x, 4)
Worksheets(ws.Name).Cells(y, 4).Font.Bold = True
'begin the copy loop
x = 2 'setting forward to the first row to start evaluating for copy
y = 2
z = z 1 'increments along the columns we are matching in the array
Do while Cells(x, 1) <> vbNullString 'make sure we have an active row
If Cells(x, z) = "Yes" Then ' looks for row plus column for match
Do While Worksheets(ws.Name).Cells(y, 2) <> vbNullString
y = y 1 'setting the row to start pasting
Loop
Worksheets(ws.Name).Cells(y, 1) = Cells(x, 1)
Worksheets(ws.Name).Cells(y, 2) = Cells(x, 2)
Worksheets(ws.Name).Cells(y, 3) = Cells(x, 3)
Worksheets(ws.Name).Cells(y, 4) = Cells(x, 4)
x = x 1 'increment to next row
End If
Loop
Next ws
End Sub
Я не могу определить, что будет вставлять его в бесконечный цикл, как это кажется. Что-нибудь бросается в глаза кому-нибудь?
Ответ №1:
Если ячейки (x, z) <> «Да», x никогда не увеличивается, а ячейки (x, 1) <> vbNullString остается true
Комментарии:
1. вот и все, у меня все еще было x = x 1 в цикле if.