Monday, November 16, 2015

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

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...