VBA copy and paste w/ conditions

hiccup2020

New Member
Joined
May 28, 2020
Messages
3
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I'm trying to copy elements in column B to column C if column A is blank. Please help!

Thanks!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the Board!

For row 1, the formula to put in column C would be:
=IF(A1="",B1,"")
and copy down for all your rows of data.
 
Upvote 0
Actually, it's little more complex. Column A I have unique ID, Column B includes work activity associated with this unique ID and the person doing the work. The person doing the work will not be assigned a unique ID so there will be a blank cell. I would like to move the 'person doing the work' to column C but at the same time align it with the work activity.

This is how data comes to me.
IDWork Activity
1​
Lay out foundation
Cement expert
2​
Build framework
Builder
Engineer

This is how I would like it to be

IDWork ActivityResources
1​
Lay out foundationCement Expert
2​
Build frameworkBuilder
2​
Build frameworkEngineer

I'm not sure if this can be done just through formulas in excel. I'm thinking that a macro needs to be used??
 
Upvote 0
That is significantly different than you original question.

I see that you are new here, so here is a little friendly tip to maximize your chances of getting the help you need. The last reponse you posted, that is what you want as your original post, so people can see exactly what you are after from the beginning. Otherwise, you may get someone who responds, and then finds out the request is over their head, and they may not be able to help. And then your thread is taken off the "Zero Reply Posts" listing, so many people will not see it as a new, unanswered thread. So the chances of getting help are greatly reduced, since less people are seeing it.

OK, back to your question. Here is some VBA code that should do what you want. I added lots of comments to the code to explain what each step is doing.
VBA Code:
Sub MyMacro()

    Dim r As Long

    Application.ScreenUpdating = False
    
'   Set intitial row value of 3
    r = 3
    
'   Put title on column C
    Range("C1") = "Resources"

'   Loop through column B
    Do
'       Exit if no more items in column B
        If Cells(r, "B") = "" Then Exit Do
'       Check to see if there is a value in column A
        If Cells(r, "A") <> "" Then
'           If there is, add one to r to move to next row
            r = r + 1
        Else
'           Check to see if there is a value in column C in row above
            If Cells(r - 1, "C") <> "" Then
'               If there is, move column B entry to column C...
                Cells(r, "C") = Cells(r, "B")
'               ... and copy values in columns A and B from row above
                Cells(r, "A") = Cells(r - 1, "A")
                Cells(r, "B") = Cells(r - 1, "B")
'               Move to next row
                r = r + 1
            Else
'               If there is no value in column C in row above, move value up there...
                Cells(r - 1, "C") = Cells(r, "B")
'               ...and delete current row
                Rows(r).Delete
            End If
        End If
    Loop
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 0
That makes sense. Thanks for your advice and council. I'll keep that in mind going forward. This is so awesome! Thank you!!!!
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top