Change code to work on selection of cells (rather than the cell addresses in the code)

ellison

Active Member
Joined
Aug 1, 2012
Messages
343
Office Version
  1. 365
Platform
  1. Windows
Hi, we've got a really handy piece of code (thank you to Kevin who posted it), which fills cells with the contents of the next non-blank cell above.
The code is:

SQL:
Sub FasterFill_V4()
    Dim ws As Worksheet, a, i As Long, s As String
    Set ws = Worksheets("Sheet1")                   '<-- *** Change to actual sheet name ***
    a = ws.Range("B2:B" & ws.Cells(Rows.Count, "A").End(xlUp).Row)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then s = a(i, 1)
        b(i, 1) = s
    Next i
    ws.Range("B2").Resize(UBound(b, 1), 1).Value = b
End Sub

We'd like if possible to change it so that the code will work on a selection of cells. So that we can then select those cells using scrolling and the mouse.

Original version before the above code is executed:

change-code-to-work-on-selection-of-cells-question.xlsm
AB
1ROWV1
22alpha
33
44
55
661
77
88
99
10102
1111
1212
1313
1414charlie
1515
1616
1717delta
1818
1919
2020
2121
2222
2323
2424
2525
Sheet1



After the Kevin's code (above) is executed:

change-code-to-work-on-selection-of-cells-question.xlsm
AB
1ROWV1
22alpha
33alpha
44alpha
55alpha
661
771
881
991
10102
11112
12122
13132
1414charlie
1515charlie
1616charlie
1717delta
1818delta
1919delta
2020delta
2121delta
2222delta
2323delta
2424delta
2525delta
Sheet1




Thanks for taking a look!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
See if this does what you are intending. Changes are in blue:

Rich (BB code):
Sub FasterFill_V5()
    Dim ws As Worksheet, a, i As Long, s As String
    Set ws = ActiveSheet
    a = Selection
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then s = a(i, 1)
        b(i, 1) = s
    Next i
    ws.Range("B2").Resize(UBound(b, 1), 1).Value = b
End Sub
 
Upvote 0
@Joe4 - I think a single read / write using an array is still faster than the special cells-formula approach over a large recordset.

ie on 10k records @kevin9999's original code 0.0625
the special cells-formula approach 0.2265625

equivalent formula version was this:
VBA Code:
Sub Fill_From_Above()
    Dim startTime As Double  
    startTime = Timer
  
  With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
 
  Debug.Print Timer - startTime, "Formula Approach"
End Sub
 
Upvote 0
@Joe4 - I think a single read / write using an array is still faster than the special cells-formula approach over a large recordset.

ie on 10k records @kevin9999's original code 0.0625
the special cells-formula approach 0.2265625

equivalent formula version was this:
VBA Code:
Sub Fill_From_Above()
    Dim startTime As Double 
    startTime = Timer
 
  With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
 
  Debug.Print Timer - startTime, "Formula Approach"
End Sub
Ah, I guess I needed to look a bit more closely to see that the code is actually through an array and not the individual cells.
 
Upvote 0
See if this does what you are intending. Changes are in blue:

Rich (BB code):
Sub FasterFill_V5()
    Dim ws As Worksheet, a, i As Long, s As String
    Set ws = ActiveSheet
    a = Selection
    ReDim b(1 To UBound(a, 1), 1 To 1)
   
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then s = a(i, 1)
        b(i, 1) = s
    Next i
    ws.Range("B2").Resize(UBound(b, 1), 1).Value = b
End Sub
Works absolutely beautifully, huge thanks!
 
Upvote 0
Sorry - just started using this and noticed a bit of glitch:
rather than overwriting the original info with the result, the results are being put in cell B2 and below.
Is it possible to change that to "over-writing"?

This is the code that I'm using:

SQL:
Sub FasterFill_V5()
    Dim ws As Worksheet, a, i As Long, s As String
    Set ws = ActiveSheet
    a = Selection
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then s = a(i, 1)
        b(i, 1) = s
    Next i
    ws.Range("B2").Resize(UBound(b, 1), 1).Value = b
End Sub
 
Upvote 0
Try changing the last line to
Selection.Value = b
(I have logged off for the night)
 
Upvote 0
Solution

Forum statistics

Threads
1,215,110
Messages
6,123,147
Members
449,098
Latest member
Doanvanhieu

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