New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

[SOLVED] dragging data

Macros, VBA, Excel Automation, etc.

[SOLVED] dragging data

Postby marreco » Sun Jan 22, 2012 3:21 pm

Good night!

I would put a formula column "P".

if the cell in column "M" is different then the empty cell "P2" = MONTH (M2).

the formula there will be dragged down until no more data in the cells of column "M".

See the attachment.
  • 0

You do not have the required permissions to view the files attached to this post.
Last edited by marreco on Mon Jan 23, 2012 5:34 am, edited 1 time in total.
marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0

Re: dragging data

Postby Sisyphus » Sun Jan 22, 2012 11:16 pm

Hi,
The following code should do the job:
Code: Select all
Option Explicit

    Const FirstDataRow As Long = 2
    Const DateCol As String = "M"
    Const ResultCol As String = "P"

Public Sub WriteMonth()

    Dim R As Long
    Dim D As Variant
   
    With ActiveSheet
        For R = FirstDataRow To LastRow("M")
            D = .Cells(R, DateCol).Value
            If Len(D) And IsDate(D) Then
                .Cells(R, ResultCol).Value = Month(D)
            End If
        Next R
    End With
End Sub

Private Function LastRow(ByVal Col As String) As Long
    LastRow = ActiveSheet.Cells(65536, Col).End(xlUp).Row
End Function

Paste it into a normal code module and call it from Tools/Macro/Macros - WriteMonth & Run.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

[SOLVED] dragging data

Postby marreco » Mon Jan 23, 2012 5:34 am

It was perfect, you should know all of VBA.
Is it asking too much if I could know what each line of code does, I would like to learn more.
Thank you very much!

Have a nice day!
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0

Re: [SOLVED] dragging data

Postby Sisyphus » Tue Jan 24, 2012 3:19 am

Hi,
Here is a commented version of the above code:
Code: Select all
Option Explicit   ' every variable must be explicitly named in the following code

    Const FirstDataRow As Long = 2
    Const DateCol As String = "M"
    Const ResultCol As String = "P"

Public Sub WriteMonth()

    Dim R As Long
    Dim D As Variant
   
    With ActiveSheet
        ' pick each of the rows in the active worksheet, one at a time,
        ' starting from FirstDataRow until the row found in the Function 'LastRow'
        ' Let the number of that row be known as R
        For R = FirstDataRow To LastRow("M")
            D = .Cells(R, DateCol).Value  ' Let D have the same value as .Cells(R, DateCol)
            ' Only If D has a length AND D is a date Then execute the following lines of code
            If Len(D) And IsDate(D) Then
               ' Let .Cells(R, ResultCol) take the value of the month of the date in variable D
                .Cells(R, ResultCol).Value = Month(D)
            End If
        Next R    ' loop here to look at the next R
    End With
End Sub

Private Function LastRow(ByVal Col As String) As Long
    ' Let the return value of this function be equal to
    ' the row number of the first cell from the end in column 'Col'
    ' which is not empty
    LastRow = ActiveSheet.Cells(65536, Col).End(xlUp).Row
End Function
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: [SOLVED] dragging data

Postby marreco » Tue Jan 24, 2012 3:49 am

Hi..

I really enjoyed the comments so I can learn a little.

Thank you and have a nice day.
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 129 guests