Basic VBA Copy Paste

SensualCarrots

New Member
Joined
Mar 21, 2015
Messages
46
I found several thing out there that get me close, but I don't really know VBA at all, so trying to modify to my needs was, well... rather unsuccessful. I need a rather simple macro.

Basically,

If A25 is empty, copy F24 and paste values in A25, else if A26 is empty, copy F24 and paste values in A26, else if A27 is empty..... and so forth down to A33, and if A33 is not empty, then end and do nothing. F24 is derived from a formula, so it's very important to paste special: paste values.

I also need a macro that will basically clear the form, and wipe out any data in a wide range of cells that I can select. Any help would be appreciated! Thanks!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here is a macro that will do your first part:
Code:
Sub MyMacro1()


    Dim rng1 As Range
    Dim cell As Range
    
    Set rng1 = Range("A25:A33")
    
    For Each cell In rng1
        If cell = "" Then
            cell = Range("F24").Value
            Exit For
        End If
    Next cell
        
End Sub
For your second part, if you just want to clear out whatever cells you have selected, try this:
Code:
Sub MyMacro2()
    Selection.ClearContents
End Sub
 
Upvote 0
First part works great, thanks! Second part needs to be a specified group of cells that are all over the place. Clearing all cells may potentially work since everything that I don't want cleared will be locked, but not sure if that will throw an error or not.
 
Upvote 0
This version will clear only the unlocked cells in your selection without any error messages:
Code:
Sub MyMacro2()
    
    Dim cell As Range
    
    Application.ScreenUpdating = False
        
    For Each cell In Selection
        If cell.Locked = False Then
            cell.ClearContents
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Actually, that only does the ones that are selected as well. I came up with something, seems to be working. Is this the most elegant way?

Code:
Sub Button3_Click()Answer = MsgBox("Are you sure you want to clear All User Data? This cannot be undone!", vbYesNoCancel + vbInformation, "Application Message")
If Answer = vbYes Then Else Exit Sub
Sheets("Sheet1").Range("B1:B6, C2:F2, G4:H12, A19, B20, A22, B23, A25:B33, A35:B35, F21:F22").ClearContents




End Sub
 
Last edited:
Upvote 0
Actually, that only does the ones that are selected as well.
That is correct. I did it based on what you said here:
I also need a macro that will basically clear the form, and wipe out any data in a wide range of cells that I can select.
So, you could select any range of cells that you want, and it will only clear the ones that are not protected.

What you did above will work fine. The main difference is that you have to identify exactly which ones to clear ahead of time, instead of just selecting a large range, and letting the code figure it out.
If that doesn't change much, your method would be more efficient, as you are pinpointing the exact cells you want to clear, instead of looping through a bunch of other cells too.
It just initially sounded to me like you wanted something that wasn't so restrictive, where you need to specifically identify the cells to clear.
 
Upvote 0
Ahh yes, forgive me. I meant to ask for a range of cells that I the creator of the spreadsheet can predetermine. I should have worded that better, but I see where you were going now. Thanks again!
 
Upvote 0
No worries!

Glad you got what you need.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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