Drag and Fill Code

IrishLad

Board Regular
Joined
May 21, 2014
Messages
101
Excel 2012
ABCDEFGHIJKLMN
1Column Header 1Column Header 2Column Header 3Column Header 4Column Header 5Column Header 6Column Header 7Column Header 8Column Header 9Column Header 10Column Header 11Column Header 12
29111191652
3Before3123965
4Running1049836
5Code281255525
62728935
763621118
85171418
9579240652248175731
10
11
12Column Header 1Column Header 2Column Header 4Column Header 7Column Header 9Column Header 11Column Header 12
139111191652
14After3123965
15Running1049836
16Code281255525
172728935
1863621118
195171418
20579240652248175731

<tbody>
</tbody>
Sheet1



The above is for illustration purposes only, cell references are not actual.

I am looking for a piece of code that when ran, will automatically drag and fill into blank columns, columns that actually have data in them.

The actual range is H1501:AP1501, this represents the yellow cells as above, if any cells in this range are blank, the whole column in the blank cell range
should be filled with the first column to the right that is not blank.

The code should account for a sequence of blank cells before encountering a cell with data in the range H1501:AP1501

The column range to be filled would be H1:H1501 , I1:I1501 and so on

I cannot just delete the blank columns as the above scenario runs all down the worksheet and across a far bigger range than the first given
so I will copy and amend the initial code to run in stages as I work down the worksheet.

I'm still completely new to code and an Excel newbie, so please bear that in mind when replying and forgive my lack of "Techspeak"

Thanks In Advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You can try this:-
This code will deletes columns in the "Usedrange" that have No data after row 2 and returns values as shown in your results . !!
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Mar44
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = ActiveSheet.UsedRange
[COLOR="Navy"]For[/COLOR] n = Rng.Columns.Count To 1 [COLOR="Navy"]Step[/COLOR] -1
    [COLOR="Navy"]If[/COLOR] Application.CountA(Cells(2, n).Resize(Rng.Rows.Count)) = 0 [COLOR="Navy"]Then[/COLOR]
         Columns(n).Delete
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
freddaveharry
31328
41429
51530
text1text2text3text4text5text6text7
text11text12text13text14text15text16text17
text21text22text23text24text25text26text27
text31text32text33text34text35text36text37
text41text42text43text44text45text46text47
text51text52text53text54text55text56text57
text61text62text63text64text65text66text67
macro
For z = 1 To 4
For j = 1 To 6original data table
For k = 2 To 4
If Cells(k, j) = "" Then Sum = Sum + 1fredbilldavetomgeorgeharry
Next k31328
If Sum = 3 Then GoTo 50 Else GoTo 10041429
50 Sum = 051530
For m = 1 To 4
Cells(m, j) = Cells(m, j + 1)
Cells(m, j + 1) = ""text1text2text3text4text5text6text7
Next mtext11text12text13text14text15text16text17
100 Sum = 0text21text22text23text24text25text26text27
Next jtext31text32text33text34text35text36text37
Next ztext41text42text43text44text45text46text47
End Subtext51text52text53text54text55text56text57
text61text62text63text64text65text66text67

<colgroup><col width="64" span="9" style="width:48pt"> <col width="64" style="width:48pt"> <col width="64" span="8" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0
The code in post #2 seems to do what you want. It takes a different approach than what you suggested. Instead of filling the blank cells with the contents from the right, it simply deletes the blank cells and the filled cells automatically come along. If you want to restrict the action to the first 1501 rows, you could alter it

Code:
Sub MG01Mar44
Dim Rng As Range, n As Long
Set Rng = ActiveSheet.UsedRange
For n = Rng.Columns.Count To 1 Step -1
    With Cells(1, n).EntireColumn
        If Application.CountA(.Range("A2:A1500")) = 0 Then
             .Range("A1:A1501).Delete shift:=xlToLeft
        End If
    End With
Next n
End Sub
 
Upvote 0
Mike, thank you ever so much for replying, I ran the code in post #2, it ran for 5 mins plus, but no drag and fill took place when finished. Now with your code I am getting a compile error, syntax error whatever that is. Please remember that you're dealing with a 53 yr old, new to computing and excel here in the last 6 months.
 
Upvote 0
I missed a quotation mark
Code:
.Range("A1:A1501").Delete shift:=xlToLeft

The 5 minute run time suggests that your UsedRange is much larger than you expect.
 
Upvote 0
Mike, please read my opening post again. Nowhere in your code does it refer to the range that I specified

Columns A to G are static and don't need to be filled whatsoever.

I want the code to search across the range H1501 to AP1501, if any of these cells are BLANK as in my opening post example (not containing zero or anything else) then the nearest column with data to the blank columns right should be dragged into the blank column
 
Upvote 0
col E has become col D in your example

my solution is generic in that it scrutinises cols 1 to 6

I can easily make it look at cols 8 to 42

your original post has clearly confused us

i find blank columns, close them up without disturbing cells lower down
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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