VBA range problem

lasher18

New Member
Joined
May 15, 2020
Messages
5
Office Version
  1. 2013
  2. 2011
Platform
  1. Windows
Hello everyone,

I have been doing a vacation tracker for my department, and got a macro that evaluates the assignment every week (Subranges).

The problem lies in those months with less than 31 days, such as June. See attached the screenshot.

What do I need to modify in the code to solve this so that the macro identifies the entire week range so that column is not left blank?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim HojaLog As Worksheet
    Dim rangolog As Range
    Dim NuevaFila As Integer
    Dim ColInicio As Integer
    Dim FechaInicio As Date
    Dim FechaCambio As Date
    Dim FechaLunes As Date
    Dim RangoCal_1 As Range
    Dim Despl As Integer

    'Si la celda modificada no está entre las filas 8 a 17, sale
    If (Not (Target.Row >= 8 And Target.Row <= 17) And Not (Target.Row >= 21 And Target.Row <= 30)) Or Target.Column > 373 Or Target.Column < 2 Then Exit Sub
    
    'Calcula fecha de incio del calendario en base a los datos de las celdas A1 y A2
    FechaInicio = CLng(CDate("01/" & LeaveTracker.Cells(1, 1).Value & "/" & LeaveTracker.Cells(2, 1).Value))
    
    'Construir fecha seleccinada
    FechaCambio = DateSerial(CInt(LeaveTracker.Cells(2, 1).Value), _
        CInt(LeaveTracker.Cells(1, 1).Value + LeaveTracker.Cells(3, 1).Value - 1), _
        CInt(LeaveTracker.Cells(5, Target.Column).Value))
    
    'Calcular desplazamiento del rango en base a meses de menos de 31 dias
    Despl = (LeaveTracker.Cells(3, 1) - 1) * 31 - (CLng(FechaCambio) - CLng(FechaInicio) - Day(FechaCambio)) - 1
    
    'Determinar columna del lunes para la semana definida
    FechaLunes = FechaCambio - Weekday(FechaCambio, vbSunday) + 2 '+ Despl
    
    'Columna seleccionada sera FechaLunes - FechaInicio
    ColInicio = CInt(FechaLunes - FechaInicio) + 2 + Despl
 
    Set RangoCal_1 = Union(LeaveTracker.Range(Cells(8, ColInicio), Cells(17, ColInicio + 4)), LeaveTracker.Range(Cells(21, ColInicio), Cells(30, ColInicio + 4)))
    
    Call Colorear(ColInicio)
 

Attachments

  • Capture.PNG
    Capture.PNG
    77.5 KB · Views: 27

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi there,
You could find the last day of your month, e.g.
VBA Code:
LastDayOfMonth = Day(DateSerial(LeaveTracker.Cells(2, 1).Value), LeaveTracker.Cells(1, 1).Value + 1, 0))
And use that variable in your other code.
Hope that helps,
Koen
 
Upvote 0
Hi rijnsent, thanks for your reply,

Should I remove other lines of the code? Because I tried it but it removes the weekends
Where should I place it?

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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