2016-12-03 3 views
0

макрос ищет столбец для ячеек со специальным текстом и записывает другой текст в другую ячейку во всех следующих строках, пока не найдет следующую ячейку со специальным текстом и сделает то же самое. эксvba excel macro (ячейка содержит определенный текст)

A1 contains text "(1)" then write text "a" in C2,C3,C4.... until it finds 
A10 contains text "(16)" then write text "b" in C11,C12,..... until it finds 
A24 contains text "(19)" th[enter image description here][1]en write "c" in C25,C26 ... 

Все остальные клетки между клетками, которые содержат текст содержат номера

enter image description here

+2

К сожалению, я не уверен, что вопрос в том? – StevenWalker

+1

Добро пожаловать в SO. Вам нужно показать некоторые усилия по программированию, чтобы решить проблему, прежде чем обращаться за помощью. –

ответ

0

вы можете пойти, как это:

Option Explicit 

Sub main() 
    Dim iArea As Long 
    Dim rng As Range 
    Dim texts As Variant 

    texts = Array("a", "b", "c") '<--| array containing your "texts", be sure therer are at least as much elements as cells with "('number')" in column A 

    With Worksheets("mySheet") '<--| change "mySheet" to your actual worksheet with data name 
     With .Range("A1", .Cells(.Rows.count, "A").End(xlUp)) '<--| reference its column "A" cells from row 1 down to last not empty one 
      With .Resize(.Rows.count + 1) 
       .Cells(.Rows.count).Value = "number : (0)" '<--| add "dummy" "specific" text. it'll be removed by the end of the macro 
       .AutoFilter field:=1, Criteria1:="=*(*)*" '<--| filter it with "*(*)*" string 
       If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any filtered cell found 
        Set rng = .SpecialCells(xlCellTypeVisible) '<--| set the range corresponding filtered cells 
        .Parent.AutoFilterMode = False '<--| remove autofilter and show all rows back 
        With rng '<--| reference filtered cells 
         For iArea = 1 To .Areas.count - 1 '<--| loop through them (each filtered cell is an 'Area') excluding last ("dummy") one 
          .Parent.Range(.Areas(iArea).Cells(2, 3), .Areas(iArea + 1).Cells(1, 3).Offset(-1)).Value = texts(iArea - 1) 'fill column "C" (third from referenced cell in column A) with 'texts' element corresponding to current area 
         Next iArea 
        End With 
       End If 
       .Cells(.Rows.count).ClearContents '<--| clear add "dummy" "specific" text 
      End With 
     End With 
    End With 
End Sub 
Смежные вопросы