2017-01-12 2 views
0

Создал этот код, но не смог добраться до части, чтобы прокручивать листы с определенными заголовками, чтобы удалить спасибо ребятам.Хотите удалить столбцы на основе строк в листе

Sub deleteCol() 

On Error Resume Next 
Dim Coldellr As Long 
Dim colval As String 
Dim wbCurrent As Workbook 
Dim wsCurrent As Worksheet 
Dim nLastCol, i As Integer 
Dim LngLp As Long 

Coldellr = Sheets("Coldel").Cells(Rows.Count, "A").End(xlUp).row 'Define LastRow 

Set wbCurrent = ActiveWorkbook 
Set wsCurrent = wbCurrent.ActiveSheet 
'This next variable will get the column number of the very last column that has data in it, so we can use it in a loop later 
nLastCol = wsCurrent.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 

'This loop will go through each column header and delete the column if the header contains "Percent Margin of Error" 
For i = nLastCol To 1 Step -1 
    For LngLp = 1 To Coldellr 
    Set colval = Sheets("Coldel").Range("a" & LngLp).Value 

    If InStr(1, wsCurrent.Cells(1, i).Value, colval, vbTextCompare) > 0 Then 
     wsCurrent.Columns(i).Delete Shift:=xlShiftToLeft 
    End If 
Next i 

End Sub 
+0

так что ваш код работает корректно для 'wsCurrent', и вам нужно всего лишь перевести его через все листы' wbCurrent'? – user3598756

ответ

0

Ваш код добавлен в цикл для каждого листа. Должно сработать.

Sub deleteCol() 

    On Error Resume Next 
    Dim Coldellr As Long 
    Dim colval As String 
    Dim wbCurrent As Workbook 
    Dim wsCurrent As Worksheet 
    Dim nLastCol, i As Integer 
    Dim LngLp As Long 

    Coldellr = Sheets("Coldel").Cells(Rows.Count, "A").End(xlUp).Row 'Define LastRow 

    Set wbCurrent = ActiveWorkbook 

    Dim sh As Worksheet '@nightcrawler23 

    For Each sh In wbCurrent.Sheets '@nightcrawler23 

     Set wsCurrent = sh '@nightcrawler23 

     'This next variable will get the column number of the very last column that has data in it, so we can use it in a loop later 
     nLastCol = wsCurrent.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 

     'This loop will go through each column header and delete the column if the header contains "Percent Margin of Error" 
     For i = nLastCol To 1 Step -1 
      For LngLp = 1 To Coldellr 

      ' Edit: 
      ' removed Set here as a value is being assigned 
      colval = Sheets("Coldel").Range("a" & LngLp).Value 

      If InStr(1, wsCurrent.Cells(1, i).Value, colval, vbTextCompare) > 0 Then 
       wsCurrent.Columns(i).Delete Shift:=xlShiftToLeft 
      End If 
     Next i 

    Next sh '@nightcrawler23 



End Sub 
+0

hmmm Я все еще получаю объект компиляции, требуемый на этой строке, который был у меня до Set colval = Листы («Coldel»). Диапазон («a» & LngLp). Значение – user3724482

+0

См. Пересмотренный код. Поскольку вы назначаете значение ячейки переменной, вам не нужно 'Set'. 'Set colval = Sheets (« Coldel »). Также был бы правильным. Range (« a »& LngLp). В вашем вопросе вы никогда не упоминали, что на этой линии произошла ошибка. Пожалуйста, не забудьте четко указать свои проблемы в fututre – nightcrawler23

Смежные вопросы