2016-04-09 3 views
-1

私はVBAの初心者ですが、MSの進歩は秀でています。だから私はVBAを学ぶことにとても興味があります。 OKこれが私の最初の質問は、私は、ファイル名が= Sheet1の名前であるシートをエクセルフォーマットする必要があり、それが列のどこかにあるActullyここ=シート名のセルアドレスを調べる

です「」私は&は、このセルの下&を超えるすべての行を削除選択したいです空白のセル/行がなくなるまで。

私はInStr & find関数で多く試してみましたが成功しませんでした。また、B5のようなセルのアドレスを見つけようとしますが、それはできません。

+3

ポスト:ここ

コードです – newguy

答えて

0

ようこそStackOverflow。あなたはすでにnewguyに知らされているので、質問を投稿するときには、これまでに試したことを示す必要があります。コード、プリントスクリーンなど。

あなたの説明は明らかではありません)、しかし私が理解したことに基づいて、私はあなたのために小さなコードサンプルを作成しました。コードを機能ブロックに分解して、達成しようとしていることをよりよく理解できるようにしました。あなたがこれまでに試したものを

'the following function will find cell with the specific text 
Private Function FindCell(ws As Worksheet, strToSearch As String, Optional sColumn As String = "A") As Integer 
    Dim iCounter As Integer 

    'as you do not know where exactly it exists, we loop from first cell in the particular row 
    'to the very last celll 
    With ws 
     For iCounter = 1 To .Range("A65000").End(xlUp).Row ' or .UsedRange.Rows.Count, or any other method 
      If .Range(sColumn & iCounter).Value = strToSearch Then 
       'yay, we have found the cell! 
       'pass out the reference 
       FindCell = iCounter 

       'now call exit function as we no longer need to continue searching for the cell (we have already found it!) 
       Exit Function 
      End If 
     Next iCounter 
    End With 

    'in case the cell does not exist, we can return -1 
    FindCell = -1 
End Function 

'the following cell will search the very first cell to the top (starting from specific row), which is blank/empty 
Private Function FindEmptyCell(ws As Worksheet, iStartRow As Integer, Optional sColumn As String = "A") As Integer 
     'This function does the same, as if you have selected specific cell 
     'and then pressed left Ctrl + Up arrow at the same time 
     'Try it! 
     'You can do the same with Right + Left + Bottom arrow as well 
     FindEmptyCell = ws.Range(sColumn & iStartRow).End(xlUp).Row + 1 
End Function 

Private Sub EraseRows() 
    Dim iStartCell As Integer 
    Dim iEndCell As Integer 
    On Error GoTo 0 
    'First let's find the "bottom" cell which is the cell with the specific text you are looking for 
    iEndCell = FindCell(ActiveSheet, "TextIAmLookingFor") 

    'now let's see find the top blank cell (so that we get the range of from - to that you want to erase) 
    iStartCell = FindEmptyCell(ActiveSheet, iEndCell) 

    'now we can delete the rows! 
    'iEndCell-1 because you don't want to erase the cell with your search string, right? 
    ActiveSheet.Rows(CStr(iStartCell) & ":" & CStr(iEndCell - 1)).EntireRow.Delete (xlUp) 
End Sub 
関連する問題