Copy first non-blank column to the left using macro

elizabethlll

New Member
Joined
Jun 20, 2018
Messages
7
Hello, I'm new to posting though reference threads from time to time - thank you!

I'm trying to create a macro that selects a range of cells in the current column, and then copies information from the cells in the column directly to the left in the range. I have gotten that to work successfully this far with the following (there are 51 cells I'm interested in):

Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(51, 0)).Select
Selection.FillRight

This would happen daily, each day is a new column that copies the previous column when the user clicks a button that I've assigned the macro to. My problem is that after every 5 days, there are two blank columns (these represent Sat and Sun, for which no data is entered). So the user would come in Monday, run the macro to populate from the previous "day" (previous column) and it would come back with blanks. Is there a way to write a macro that populates fro the previous "day" (column) unless that column is blank, in which case it would populate from last "day" (column) with values? I should say that it's OK if there are some blank CELLS in the column from which I'm populating, but the whole column can't be blank.

Thank you!
 
whoops, i made a couple of mistakes in there, sorry, was playing around for a different function, must have copied that in middle. Give me another second.
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Here, try this. Paste itno module, call with

Code:
call UpdateToday

and the code is:

Code:
Sub UpdateToday()
 Dim rSource As Range, rDest As Range
 Dim lOffset As Long
 
 lOffset = -1
 Set rDest = Range(ActiveCell, ActiveCell.Offset(16, 0))
 Set rSource = rDest.Offset(0, lOffset)
    
    Do While Not FindWeekday(rSource)
        lOffset = lOffset - 1
        Set rSource = rSource.Offset(0, lOffset)
        MsgBox ("DoWhile::: rSource: " & rSource.Address)
    Loop
 
 rDest.Value = rSource.Value
 
 Set rSource = Nothing
 Set rDest = Nothing
End Sub


Public Function FindWeekday(rCheck As Range) As Boolean
    If Application.WorksheetFunction.CountA(rCheck) > 0 Then
      FindWeekday = True
      'MsgBox ("Found Weekday")
      Else
        'MsgBox ("Found Weekend")
        FindWeekday = False
    End If
End Function


HTH,

Jon
 
Upvote 0
Thanks Jon! Unfortunately, I'm still coming up blank for Monday's in my sheet. Unfortunately, this is far beyond my skills to trouble-shoot!
 
Upvote 0
Here is my sample data. Does this look like your table? I click on Monday, it pushes Friday's data in (it actually gives a message i forgot to comment out first). Let's make sure the structure is what i think it is first, then we'll look.

Rich (BB code):
Fri 06/15Sat 06/16Sun 06/17Mon 06/18Tue 06/19Wed 06/20Thu 06/21Fri 06/22Sat 06/23Sun 06/24Mon 06/25Tue 06/26Wed 06/27Thu 06/28Fri 06/29
data1159874869841033878588801786984159
data23575664654923522055774654357
data3645448331093481112633645
data413274116541873172821741416541132
data5325157516482673998516325
data633655665489212101546654336
data7548998446541533310498159876545489
data84798465498311348265501113485347984
data935466579456101257035105814563546
data1035456477946543656513310302151030569465436354
data1165488665106555376516555370654886
data125645641346555697761068616324655564
data133366362136546489395796582702658606865464893366
data1444465465646699012192126365646444
data1585856160567741019121683010268677485856
data165698654065114765976365521266091011475698
data1788787465583963314329152165583887
<colgroup><col><col><col><col span="2"><col span="2"><col span="2"><col><col><col><col><col><col><col></colgroup><tbody> </tbody>
 
Upvote 0
And - just in case - try re-copying the text below. If you dont' get an error, you should get a pop-up message which contains the Source Range Address.

Code:
Sub UpdateToday()
 Dim rSource As Range, rDest As Range
 Dim lOffset As Long
 
 lOffset = -1
 Set rDest = Range(ActiveCell, ActiveCell.Offset(16, 0))
 Set rSource = rDest.Offset(0, lOffset)
    
    Do While Not FindWeekday(rSource)
        lOffset = lOffset - 1
        Set rSource = rSource.Offset(0, lOffset)
        MsgBox ("DoWhile::: rSource: " & rSource.Address)
    Loop
 
 rDest.Value = rSource.Value
 
 Set rSource = Nothing
 Set rDest = Nothing
End Sub


Public Function FindWeekday(rCheck As Range) As Boolean
    If Application.WorksheetFunction.CountA(rCheck) > 0 Then
      FindWeekday = True
      'MsgBox ("Found Weekday")
      Else
        'MsgBox ("Found Weekend")
        FindWeekday = False
    End If
End Function
 
Upvote 0
Your sample data looks good, except I don't have the day in the format. So for instance, I just have 6/26/2018 NOT Tue 06/26. I use a different date table to assign what day of the week, fiscal year, etc.
 
Upvote 0
Did you get this to work, and if not, what is the msg box saying when it comes up?

(remember, if something errs, you have to reset the project before code will run again)

You may try copying that last code bit, just to be sure. Also, uncomment some of the message boxes to see what is going off. Mine is still working each time i have tried.

Thanks,

Jon
 
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,846
Members
449,471
Latest member
lachbee

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