Add/Remove columns, date range, based on value of a cell

dkmiller16

New Member
Joined
Oct 31, 2013
Messages
13
I am working on a weight loss % tracker. I specify the start date, M2, as well as the weigh in intervals, M3. I would like to have a place to enter how many cycles or intervals that we want to do this for. In the example below I have 5 weigh ins. If I want to run this for 8 weeks I can insert columns, copy/paste formulas and then make sure all the formulas are calculating properly. That's a bit time consuming but possible on my end. I would like to share this with friends that are not at all used to working in Excel and changing the cycle amount wouldn't be an option for them. I tried all kinds of searches but I wasn't sure how to word the search and was drawing a blank.

Thanks


Weight Loss.png
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
What abou
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim LstRw As Long, PrvsLstCol As Long, CurrLstCol As Long, PrvsVal As Long, CurtVal As Long
Dim SrcRng As Range, FllRng As Range
 
PrvsLstCol = Cells(3, Columns.Count).End(xlToLeft).Column
LstRw = Cells(Cells.Rows.Count, PrvsLstCol).End(xlUp).Row
 
    Application.EnableEvents = False
     If Not Intersect(Target, Cells(3, PrvsLstCol)) Is Nothing Then
        Application.Undo
        DateVal = Cells(2, PrvsLstCol).Value
        PrvsVal = Cells(3, PrvsLstCol).Value
        Application.Undo
        CurtVal = Cells(3, PrvsLstCol).Value
               ' Rest Last Column
               CurrLstCol = (CurtVal * 2) + 3
               'Titel
                With Cells(1, 1).Resize(1, CurrLstCol)
                .HorizontalAlignment = xlCenterAcrossSelection
                .Interior.Color = .Cells(1, 1).Interior.Color
                End With
                'Date
                With Cells(2, CurrLstCol)
                .Value = DateVal
                End With
                With Cells(2, PrvsLstCol)
                .Value = ""
                End With
                'Cycle
                With Cells(3, CurrLstCol)
                .Value = CurtVal
                End With
                With Cells(3, PrvsLstCol)
                .Value = ""
                End With
                '
                With Cells(5, PrvsLstCol).Resize(LstRw + 1 - 5, 1)
                .Copy Destination:=Cells(5, CurrLstCol)
                .Value = ""
                End With
              
            If CurtVal > PrvsVal Then
                With Cells(5, PrvsLstCol - 2).Resize(1, 2)
               .AutoFill Destination:=Cells(5, PrvsLstCol - 2).Resize(1, 2 + (CurrLstCol - PrvsLstCol))
                End With
            Else
                With Cells(1, CurrLstCol + 1).Resize(LstRw + 1 - 1, PrvsLstCol - CurrLstCol)
                .Clear
                End With
            End If
 
  
    
     End If
    Application.EnableEvents = True
 
End Sub
 
Upvote 0
Try this
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim LstRw As Long, PrvsLstCol As Long, CurrLstCol As Long, PrvsVal As Long, CurtVal As Long
Dim SrcRng As Range, FllRng As Range
 
PrvsLstCol = Cells(3, Columns.Count).End(xlToLeft).Column
LstRw = Cells(Cells.Rows.Count, PrvsLstCol).End(xlUp).Row
 Dim r As Range
    Application.EnableEvents = False
     If Not Intersect(Target, Cells(3, PrvsLstCol)) Is Nothing Then
        Application.Undo
        DateVal = Cells(2, PrvsLstCol).Value
        PrvsVal = Cells(3, PrvsLstCol).Value
        Application.Undo
        CurtVal = Cells(3, PrvsLstCol).Value
               ' Rest Last Column
               CurrLstCol = (CurtVal * 2) + 3
               'Titel
                With Cells(1, 1).Resize(1, CurrLstCol)
                .HorizontalAlignment = xlCenterAcrossSelection
                .Interior.Color = .Cells(1, 1).Interior.Color
                End With
                With Cells(2, 2).Resize(1, CurrLstCol - 4)
                .HorizontalAlignment = xlCenterAcrossSelection
                .Interior.Color = .Cells(1, 2).Interior.Color
                End With
                With Cells(3, 2).Resize(1, CurrLstCol - 4)
                .HorizontalAlignment = xlCenterAcrossSelection
                .Interior.Color = .Cells(1, 2).Interior.Color
                End With
                'Date
                With Cells(2, CurrLstCol)
                .Value = DateVal
                End With
                With Cells(2, PrvsLstCol)
                .Value = ""
                End With
                'Cycle
                With Cells(3, CurrLstCol)
                .Value = CurtVal
                End With
                With Cells(3, PrvsLstCol)
                .Value = ""
                End With
                '
                With Cells(5, PrvsLstCol).Resize(LstRw + 1 - 5, 1)
                .Copy Destination:=Cells(5, CurrLstCol)
                .Value = ""
                End With
              
            If CurtVal > PrvsVal Then
                With Cells(5, PrvsLstCol - 2).Resize(2, 2)
               .AutoFill Destination:=Cells(5, PrvsLstCol - 2).Resize(2, 2 + (CurrLstCol - PrvsLstCol))
                End With
            Else
                With Cells(1, CurrLstCol + 1).Resize(LstRw + 1 - 1, PrvsLstCol - CurrLstCol)
                .Clear
                End With
            End If
            
        With Cells(7, 1).Resize(LstRw + 1 - 7, CurrLstCol)
            .Cells.FormatConditions.Delete
            .FormatConditions.Add Type:=xlExpression, Formula1:= _
            "=" & .Address(False, True) & "<>"""""
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
           .FormatConditions(.FormatConditions.Count).Borders.LineStyle = xlContinuous
            .FormatConditions(1).StopIfTrue = False
        End With
 
 
    
     End If
    Application.EnableEvents = True
 
End Sub
with this table
Add Remove columns, date range, based on value of a cell.xlsm
ABCDEFGHIJKLM
1Weight Loss % Tracker
2WEIGHTLOSS COMPETITIONSet Competition Start Date1/1/2021
3SetWeight Measureed Interval [DAYS]:5
4
5NameStarting weight [LP]weight [LP]Resultweight [LP]Resultweight [LP]Resultweight [LP]Resultweight [LP]Resultweight [LP]
604/01/202111/01/202118/01/202125/01/202101/02/202108/02/20215
7XX5
8YY5
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A7:M8Expression=$A7:$M8<>""textNO

to be like this
Add Remove columns date range based on value of a cell.gif
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

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