Excel VBA – How to Execute Function in Excel

microsoft excelmicrosoft-excel-2007microsoft-excel-2010vbaworksheet-function

I'm trying to get this function in VBA but I'm new to it. I don't seem to know how I can get this done:

What I'm trying to do is, for each cell in column B that's numeric, I want to take the next two cells in column H i.e if it's B7, I'd like to take H8 and H9 (next two row values in H column) and concatenate them (they're strings) and save them in I7 (same row as B7) and finally delete the values in H8,H9.

Here's where I got to, I know it isn't much.

Private Sub CommandButton1_Click()
Dim B As Range, H As Range, I As Range
For Each B In ClientTable.Columns(2).Cells
    If IsNumeric(B) Then
    (What should be done here?)    
    End If
Next B
End Sub

Best Answer

I found IsNumeric(B) to be true for blank cells, so I changed your If statement accordingly.  This worked for me:

    If IsNumeric(B) And B <> "" Then
        Cells(B.Row, 9) = Cells(B.Row + 1, 8) & Cells(B.Row + 2, 8)
        Cells(B.Row + 1, 8) = ""
        Cells(B.Row + 2, 8) = ""
    End If
    If B.Row > 9 Then Exit For

Obviously 8 and 9 are the column numbers for H and I, respectively.  I added the

If B.Row > 9 Then Exit For

line because I had a problem with the loop not terminating; if

For Each B In ClientTable.Columns(2).Cells

works for you, you can take out the If B.Row > 9 Then Exit For.

Related Question