Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Need an Autofill VBA

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    45
    Rep Power
    0

    Need an Autofill VBA

    I would like to use a VBA to drag and autofill the formula in D:G column, when A time and B has value correspond to C time, instead of drag and fill manually the large sum of rows. Thanks for any help
    Attached Files Attached Files
    Last edited by mrprofit; 05-13-2014 at 04:39 PM.

  2. #2
    Senior Member alansidman's Avatar
    Join Date
    Apr 2012
    Posts
    125
    Rep Power
    14
    I am not understanding this

    when A time and B has value correspond to C time
    Would you please explain what this means and provide several examples.

  3. #3
    Member
    Join Date
    Apr 2014
    Posts
    45
    Rep Power
    0
    Usually i manually drag and fill down the formula to many rows, now i just want to use VBA macro to autofill just the required row only. with this book4 example, when A time 14:18 has B value (value>0), then C time 14:18 (= A time 14:18) will autofill the formula for D6:G6. For A time 14:20, B has no value, so no autofill for D7:G7. If A time has B value til 14:22, then formula will fill til D8:G8 only.
    Last edited by mrprofit; 05-13-2014 at 06:42 PM.

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    45
    Rep Power
    0
    Appreciate any help

  5. #5
    Moderator
    Join Date
    Jul 2012
    Posts
    156
    Rep Power
    13
    mrprofit
    Your question is already viewed for more than 50 times and still no answer. You really need to clarify more.
    It may be obvious too you but with the example file you provided and the answer you gave in Post#3 it's still difficult to figure out what you really want to achieve.(at least to me it is)

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    45
    Rep Power
    0
    OK, thank you, please see attached file, sheet1 ~ sheet3 is the result the VBA to autofill down i needed.
    Attached Files Attached Files
    Last edited by mrprofit; 05-18-2014 at 06:54 PM.

  7. #7
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    14
    Hi mrprofit,

    Add code in Sheet3 module:

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        'Change column no accordingly
        If Target.Column = 3 Then
            Call lmp_Test
        End If
    
    
    End Sub
    Code:
    Sub lmp_Test()
    
    
        Dim rngData                 As Range
        Dim rngFindData             As Range
        Dim varData()               As Variant
        Dim varValueToCheck()       As Variant
        Dim varFinal()              As Variant
        Dim varRows                 As Variant
        Dim objDic                  As Object
        Dim lngLoop                 As Long
        Dim lngLoop1                As Long
    
        'Change these values accordingly. these values as per the attached file.
        Const strShtName            As String = "Sheet3"
        Const strDataStartCell      As String = "A2"
        Const lngTotalDataColumn    As Long = 2
        Const strDelima             As String = "|"
        Const strValueToCheckCell   As String = "C2"
    
    
        With ThisWorkbook
            With .Worksheets(strShtName)
                Set rngData = .Range(strDataStartCell)
                Set rngData = rngData.Resize(.Cells(.Rows.Count, rngData.Column).End(xlUp).Row, lngTotalDataColumn)
                If Not rngData Is Nothing Then
                    If WorksheetFunction.CountA(rngData) Then
                        varData = rngData.Value2
                        Set objDic = CreateObject("Scripting.Dictionary")
                        objDic.CompareMode = vbTextCompare
                        For lngLoop = LBound(varData) To UBound(varData)
                            If LenB(Trim(varData(lngLoop, 1))) > 0 Then
                                If Not objDic.Exists(CStr(varData(lngLoop, 1))) Then
                                    objDic.Item(CStr(varData(lngLoop, 1))) = lngLoop
                                Else
                                    objDic.Item(CStr(varData(lngLoop, 1))) = objDic.Item(CStr(varData(lngLoop, 1))) & strDelima & lngLoop
                                End If
                            End If
                        Next lngLoop
                        If objDic.Count Then
                            If WorksheetFunction.CountA(rngData.Resize(, 1).Offset(, 2)) Then
                                varValueToCheck = rngData.Resize(, 1).Offset(, 2).Value2
                                ReDim varFinal(1 To UBound(varValueToCheck), 1 To 4)
                                varFinal = rngData.Resize(, 1).Offset(, 3).Resize(, 4).Value2
                                With .Range(strDataStartCell)
                                    For lngLoop = LBound(varValueToCheck) To UBound(varValueToCheck)
                                        If objDic.Exists(CStr(varValueToCheck(lngLoop, 1))) Then
                                            varRows = objDic.Item(CStr(varValueToCheck(lngLoop, 1)))
                                            If InStr(varRows, strDelima) = 0 Then
                                                ReDim varRows(0 To 0)
                                                varRows(0) = objDic.Item(CStr(varValueToCheck(lngLoop, 1)))
                                            Else
                                                varRows = Split(varRows, strDelima)
                                            End If
                                            Set rngFindData = Nothing
                                            For lngLoop1 = LBound(varRows) To UBound(varRows)
                                                If rngFindData Is Nothing Then
                                                    Set rngFindData = .Offset(varRows(lngLoop1) - 1, 1)
                                                Else
                                                    Set rngFindData = Application.Union(rngFindData, .Offset(varRows(lngLoop1) - 1, 1))
                                                End If
                                            Next lngLoop1
                                            'Application.Goto rngFindData
                                            'For Opening
                                            If Not LenB(Trim(varFinal(lngLoop, 1))) Then
                                                varFinal(lngLoop, 1) = varData(varRows(LBound(varRows)), 2)
                                            End If
                                            'For High
                                            If Not LenB(Trim(varFinal(lngLoop, 2))) Then
                                                If Not rngFindData Is Nothing Then
                                                    varFinal(lngLoop, 2) = WorksheetFunction.Max(rngFindData)
                                                    If rngFindData.Cells.Count = 1 Then
                                                        If LenB(Trim(rngFindData.Value)) = 0 Then
                                                            varFinal(lngLoop, 2) = vbNullString
                                                        End If
                                                    End If
                                                End If
                                            End If
                                            'For Low
                                            If Not LenB(Trim(varFinal(lngLoop, 3))) Then
                                                If Not rngFindData Is Nothing Then
                                                    varFinal(lngLoop, 3) = WorksheetFunction.Min(rngFindData)
                                                    If rngFindData.Cells.Count = 1 Then
                                                        If LenB(Trim(rngFindData.Value)) = 0 Then
                                                            varFinal(lngLoop, 3) = vbNullString
                                                        End If
                                                    End If
                                                End If
                                            End If
                                            'For Close
                                            If Not LenB(Trim(varFinal(lngLoop, 4))) Then
                                                varFinal(lngLoop, 4) = varData(varRows(UBound(varRows)), 2)
                                            End If
                                        End If
                                        If LenB(Trim(varValueToCheck(lngLoop, 1))) = 0 Then
                                            varFinal(lngLoop, 1) = vbNullString
                                            varFinal(lngLoop, 2) = vbNullString
                                            varFinal(lngLoop, 3) = vbNullString
                                            varFinal(lngLoop, 4) = vbNullString
                                        End If
                                    Next lngLoop
                                End With
                                With .Range(strValueToCheckCell)
                                    .Offset(, 1).Resize(UBound(varFinal), UBound(varFinal, 2)).Value2 = varFinal
                                End With
                            End If
                        End If
                    End If
                End If
            End With
        End With
        
        Set rngData = Nothing
        Set rngFindData = Nothing
        Erase varData
        Erase varValueToCheck
        Erase varFinal
        varRows = Empty
        Set objDic = Nothing
        lngLoop = Empty
        lngLoop1 = Empty
    
    
    End Sub
    Last edited by LalitPandey87; 05-19-2014 at 09:51 AM.

  8. #8
    Member
    Join Date
    Apr 2014
    Posts
    45
    Rep Power
    0
    thank you, G5 in sheet3 is empty when i apply the vba, G5 value 101.766, and E6:F9 has 0 value in it, and possible to keep the formula in the cells?
    Last edited by mrprofit; 05-19-2014 at 10:56 AM.

  9. #9
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    14
    Are you asking to apply formula using vba or somthing else.
    If you want formula then why need VBA just put formula and drag it till the end and whenever you put value, it will show you the output values

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    45
    Rep Power
    0
    usig this VBA is fine, may be keep the formula in row 2 in case in future i need the formula,

Similar Threads

  1. VBA Code To Autofill Formula In Every Nth Row
    By analyst in forum Excel Help
    Replies: 1
    Last Post: 12-23-2013, 05:51 PM
  2. Replies: 6
    Last Post: 12-23-2013, 04:07 PM
  3. Autofill the data based on non blank cell in next row?
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 3
    Last Post: 11-29-2012, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •