Is there any way that 'filling' can be restricted to values only and not formatting, perhaps through VBA?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

I know that it is possible to right-click and drag and then select 'fill without formatting', but is there any formatting changes can be disabled for dragging/filling, perhaps through a VBA code? :biggrin:

Thanks for any input!
 
Thanks much! 🤗 Just tested in my actual work file, and it's working well there too, so now it's a step improved 🍻

If you could also come up with a code to take care of dragging any cell in the same range (i.e. just dragging values and no formatting), please let me know.
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If you could also come up with a code to take care of dragging any cell in the same range (i.e. just dragging values and no formatting), please let me know.

This should get you started, It's not complete, but it will give you a starting point:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    If Not Intersect(Target, Range("E3:E42")) Is Nothing And Target.CountLarge > 1 Then
        With Application
            .EnableEvents = False
            .Undo
            Cells(Target.Row - 1, Target.Column).AutoFill Destination:=Range(Cells(Target.Row - 1, _
                    Target.Column), Cells(Target.Row - 1 + Target.CountLarge, Target.Column)), Type:=xlFillValues
            .EnableEvents = True
        End With
    End If
End Sub
 
Upvote 0
OMG, your code is already working well, so as far as my tests showed, it is complete and so magical!!! I tested dragging at various points within the range, and I even dragged upwards, and it worked perfectly well!!!

Thank you! 🤗
 
Upvote 0
Now just one more question. If I want to apply this code to more than one range in the same sheet, what should I do? Would I copy and paste the code multiple times and change the ranges, or is there a more efficient way? :biggrin:
 
Upvote 0
Actually, I take it back; it's not so magical. I realized I cannot delete anything now in the range 😭😭😭 As soon as delete a piece of the range, the contents of the cells magically reappear, so please help if you can 😂
 
Upvote 0
Actually, I take it back; it's not so magical. I realized I cannot delete anything now in the range 😭😭😭 As soon as delete a piece of the range, the contents of the cells magically reappear, so please help if you can 😂

That is why I said it was a start. ;)

You can, however, delete cells, one at a time in the current condition of the code. :)
 
Upvote 0
Now just one more question. If I want to apply this code to more than one range in the same sheet, what should I do? Would I copy and paste the code multiple times and change the ranges, or is there a more efficient way? :biggrin:

To do that you could use 'Or's ... for example:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    If Not Intersect(Target, Range("E3:E42")) Is Nothing And Target.CountLarge > 1 Or _
            Not Intersect(Target, Range("F3:F42")) Is Nothing And Target.CountLarge > 1 Or _
            Not Intersect(Target, Range("G3:G42")) Is Nothing And Target.CountLarge > 1 Then
        With Application
            .EnableEvents = False
            .Undo
            Cells(Target.Row - 1, Target.Column).AutoFill Destination:=Range(Cells(Target.Row - 1, _
                    Target.Column), Cells(Target.Row - 1 + Target.CountLarge, Target.Column)), Type:=xlFillValues
            .EnableEvents = True
        End With
    End If
End Sub
 
Upvote 0
Thank you. The 'Or's work well when only one cell in one column is dragged. But I tried dragging two cells selected together in columns E and F and it didn't work (only E got expanded but double the number of cells I dragged 😳).

If you can solve these two issues, i.e. deleting more than once cell at a time and dragging more than one cell at a time, you'll be the absolute genius :) 🤗
 
Upvote 0
The following should allow the deletions:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    If Application.WorksheetFunction.CountA(Target) = 0 Then Exit Sub                           ' Check/Allow deletetions
'
    If Not Intersect(Target, Range("E3:E42")) Is Nothing And Target.CountLarge > 1 Or _
            Not Intersect(Target, Range("F3:F42")) Is Nothing And Target.CountLarge > 1 Or _
            Not Intersect(Target, Range("G3:G42")) Is Nothing And Target.CountLarge > 1 Then
        With Application
            .EnableEvents = False
            .Undo
            Cells(Target.Row - 1, Target.Column).AutoFill Destination:=Range(Cells(Target.Row - 1, _
                    Target.Column), Cells(Target.Row - 1 + Target.CountLarge, Target.Column)), Type:=xlFillValues
            .EnableEvents = True
        End With
    End If
End Sub
 
Upvote 0
OMG, this is superb!!! Thank you! 🤗

One last step left now, for multi-column dragging :biggrin:


If it helps, I just tested three-column drag which resulted in an error, and when I hit debug, these two lines of code were highlighted:

Cells(Target.Row - 1, Target.Column).AutoFill Destination:=Range(Cells(Target.Row - 1, _
Target.Column), Cells(Target.Row - 1 + Target.CountLarge, Target.Column)), Type:=xlFillValues
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,278
Messages
6,171,157
Members
452,385
Latest member
Dottj

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