VBA Offset challenge/problem

ebea

Active Member
Joined
Jul 12, 2008
Messages
296
Office Version
  1. 2021
Platform
  1. Windows
Hi! I run into a problem, with the Offset function, and how to implement it in my code.
In Column H1:FT1 I have month's placed.
What I need to do, is to place the date in F2 in the correct month in the range H2:FT2 (marked in Yellow), when I run my code, from push on Button "Create M.". As now, when I run my code, It place the date in H2.
I have tried many ways around, to get the Offset function, placed correctly in my code, so it ends up in the correct Column, but so far, without any luck.

Hopefully someone have the answer ;)

The code are:

VBA Code:
Option Explicit
Sub months()
Dim x As Long
Dim ws As Worksheet
Set ws = Worksheets("Ark1")

ws.Range("H2") = WorksheetFunction.EDate(ws.Range("F2") + 0, 0)
Range("H2").AutoFill Destination:=Range(Cells(2, 8), Cells(2, 8 + Range("G2").Value))
End Sub

CustomerCostSalesStart dateMonths periodjan-24feb-24mar-24apr-24maj-24jun-24jul-24aug-24sep-24okt-24nov-24dec-24jan-25feb-25mar-25apr-25
A379,00399,0001-03-202424mar-24apr-24maj-24jun-24jul-24aug-24sep-24okt-24nov-24dec-24jan-25feb-25mar-25apr-25maj-25jun-25
20202020202020202020202020202020
B729,00759,0001-02-202436
C449,00459,0001-03-2024
D569,00599,0001-04-2024
E01-05-2024
Total


Regards
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Not sure if I correctly understood your goal but try these changes:
VBA Code:
Option Explicit
Sub months_new()
    Dim ws    As Worksheet
    Dim diff  As Integer
    Set ws = Worksheets("Ark1")
    With ws
        diff = DateDiff("m", .Range("C1"), .Range("F2")) '<- calculates offset by month (please check if I got the right cells)
        .Cells(2, 8 + diff) = WorksheetFunction.EDate(.Range("F2") + 0, 0)
        .Cells(2, 8 + diff).AutoFill Destination:=.Range(Cells(2, 8 + diff), Cells(2, 8 + diff + .Range("G2").Value))
    End With
End Sub
 
Last edited:
Upvote 0
Solution
Thank you, rollis13. It was Spot On.
I was around, something similar, but surely missed something ;)
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,215,194
Messages
6,123,569
Members
449,108
Latest member
rache47

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