Friday 8 February 2019

How can I execute this function using VBA in excel?


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

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.


No comments:

Post a Comment

Where does Skype save my contact&#39;s avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...