Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Excel VBA script

I need some help writing a visual basic script to run in Excel on output from a database program. The logic would be as follows.
find "D" in column A and set x to the row number
if C(x+1) is not null then copy Ax and Bx to A(x+1) and B(x+1) respectively
repeat

Essentially it just needs to copy the cells from column A and B down one row if there is a value in column C on that next row.
I can usually cobble together vbscript ok but as I have never worked with excel VBA I am having difficulty and I can't seem to find examples to work from with this so any help would be appreciated.
0
zeetec
Asked:
zeetec
  • 5
  • 3
1 Solution
 
rspahitzCommented:
I would suggest doing this with formulas to find where the pieces are that you want.

In Columns D and E put this formula, starting with column 2:

D2:
=if(Search("D",A1)>0, if(C2<>"", A2,""), "")

E2:
=if(D2<>"", B2, "")

Now you should get values in columns D and E only in the places where you want to insert into columns A and B.

Turn on filters and filter D and E so they only show non-blanks.
Copy everything from D and E and paste into A and B.

(I haven't tested it yet, so make a backup first to ensure you can restore A and B as needed.)
0
 
zeetecAuthor Commented:
Hi Rspahitz,

Thanks for your suggestion but I need to run a recursive script to update the data in one step. Trying to use formulas is going to be very fiddly if not impossible with the main problem being that Ax and Bx may need to be copied down anywhere from 1 to 100 or more times depending on whether there is data in column c in the row below. Unless I am missing something, your formulas are useful only if you do filter copy filter copy filter copy etc which is not an option. I hope that make sense?
0
 
rspahitzCommented:
You should be able to apply the formula as far down as needed, filter once, then copy all and paste apply the results in one shot.  However, if you plan to do this more than once, then that might be too many steps.
Let me see if I can come up with a VBA solution.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
zeetecAuthor Commented:
Maybe I should give an example because I am not sure if I was clear about the problem. Here is an example of the data before and after the process.

 D1234 John Smith $123
                               $323
                               $243

 D1234 John Smith $123
 D1234 John Smith $323
 D1234 John Smith $243

Does that help make it clearer?
0
 
rspahitzCommented:
This seems to work.  notice that this will destroy whatever is located in columns A and B, whenever it find a "D" in column A's previous row and a null in column C.
For that reason, I am putting the values into columns 4 and 5 so you can test it.  If you find it does the job as expected, change the 4 and 5 to 1 and 2.
 
Sub UpdateSomeItems()
    Dim iRow As Integer
    Dim strColumnACell As String
    Dim iTransferCount As Integer
    
    iRow = 1
    iTransferCount = 0
    Do
        strColumnACell = Cells(iRow, 1).Value
        
        If Cells(iRow, 1).Value = "" Then
            ' exit when column A has no value in it
            Exit Do
        End If
        
        If InStr(strColumnACell, "D") > 0 Then
            ' Cell in column A contains a capital D
            If Cells(iRow + 1, 3).Value <> "" Then
                ' Transfer cells in column A and B down
                iRow = iRow + 1
                ' copy from column A (1)
                Cells(iRow, 4).Value = Cells(iRow - 1, 1).Value
                ' copy from column B (2)
                Cells(iRow, 5).Value = Cells(iRow - 1, 2).Value
                iTransferCount = iTransferCount + 1
            End If
        End If
        iRow = iRow + 1
    Loop
    MsgBox "Done transferring " & iTransferCount & " item" & IIf(iTransferCount = 1, "", "s") & ".", vbOKOnly Or vbInformation
End Sub

Open in new window

0
 
rspahitzCommented:
For the example you gave, I changed the logic a bit so it checks to see if there's something in column C first, then it checks the previous row to see if it contains a "D"

Also, since I'm not sure how you can tell when the data ends (which row) I simply put 50.  You can put something sufficiently large if you like, such as 10,000, but it might take longer to run.
 
Sub UpdateSomeItems()
    Dim iRow As Integer
    Dim strColumnCCell As String
    Dim iTransferCount As Integer
    
    iRow = 2
    iTransferCount = 0
    Do
        strColumnCCell = Cells(iRow, 3).Value
        
        'If Cells(iRow, 1).Value = "" Then
        If iRow = 50 Then
            ' exit when column A has no value in it
            Exit Do
        End If
        
        If strColumnCCell <> "" Then
            ' Cell in column C contains a value
            If InStr(Cells(iRow - 1, 1).Value, "D") > 0 Then
                If Cells(iRow, 1).Value = "" Then
                ' only apply if there's not something there already
                ' D found in column A of previouis row
                ' Transfer cells in column A and B down
                ' copy from column A (1)
                Cells(iRow, 1).Value = Cells(iRow - 1, 1).Value
                ' copy from column B (2)
                Cells(iRow, 2).Value = Cells(iRow - 1, 2).Value
                iTransferCount = iTransferCount + 1
                End If
            End If
        End If
        iRow = iRow + 1
    Loop
    MsgBox "Done transferring " & iTransferCount & " item" & IIf(iTransferCount = 1, "", "s") & ".", vbOKOnly Or vbInformation
End Sub

Open in new window

0
 
zeetecAuthor Commented:
That is perfect. Thank you. The file I have at the moment is 14000 rows and it only took a few seconds to complete so no problem there. Your code will help me greatly in future when working with Excel in VB thanks.
0
 
rspahitzCommented:
Glad it helped! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now