2017-01-12 9 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この行には以前にあったコンパイルエラーオブジェクトがまだ残っています colval = Sheets( "Coldel")を設定してください。範囲( "a"&LngLp)。値 – user3724482

+0

改訂コードを参照してください。セルの値を変数に代入するので、 'Set'は必要ありません。 'Set colval = Sheets(" Coldel ")。Range(" a "&LngLp)'も正しいでしょう。あなたの質問では、その行にエラーが発生しているとは言いませんでした。 fututreであなたの問題を明確に述べてください – nightcrawler23

関連する問題