Consulting

Results 1 to 4 of 4

Thread: Solved: Linking sub's and selecting cells for a sub

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Posts
    24
    Location

    Solved: Linking sub's and selecting cells for a sub

    Ok I have a macro that i have that is working great. but what I need to do it link the next macro that I need to run. I'm in need of using this macro.

    I need it to select column b and column d.

    If someone can help me that would be great.

    I would also like to know how to tell it Column 2 and column 4.

    on day I will understand how it works. I'm starting to understand. I have been using the help center and it does a bit for me.

    I also need to know how to call a macro to run. I will be running one called "Sub CopyFromWorksheets()" that I need to call up and run.

    I'm going to link three Subs together. I saw it in a post this morning but I can't remember which one it was in, I know it is simple. sorry for asking simple questions.

    I saw it like this

    Sub CopyFromWorksheets()

    'do some stuff

    Call Sub

    'do some more stuff

    End sub


    I think?

    Lastly what is the best way to save a way for a person that is completely not in the know. to run a series or macros. by a tool bar. or should I add to an exsisting area or the workbook?

    [vba]
    Sub FillColBlanks()
    'by Dave Peterson 2004-01-06
    'fill blank cells in column with value above
    Dim wks As Worksheet
    Dim rng As Range
    Dim lastrow As Long
    Dim col As Long
    Set wks = ActiveSheet
    With wks
    'col = ActiveCell.Column
    Set rng = .UsedRange 'try to reset the lastcell
    lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row
    Set rng = Nothing
    On Error Resume Next
    Set rng = .Range(.Cells(2, col), .Cells(lastrow, col)) _
    .Cells.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If rng Is Nothing Then
    MsgBox "No blanks found"
    Exit Sub
    Else
    rng.FormulaR1C1 = "=R[-1]C"
    End If
    'replace formulas with values
    With .Cells(1, col).EntireColumn
    .Value = .Value
    End With
    End With
    End Sub
    [/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like this. I've used an array to store the numbers, as a demo of one method. For two calls, you could enter the Call statement twice. You can use Call with parameters in brackets or just name the routine and list the parameter(s). eg

    [VBA]
    Sub Test
    Call FillColBlanks(2)
    FillColBlanks 4
    End sub
    [/VBA]



    [VBA]
    Option Explicit

    Sub Test()
    Dim arr, a
    arr = Array(2, 4)
    For Each a In arr
    Call FillColBlanks(a)
    Next
    End Sub

    Sub FillColBlanks(Col)
    'by Dave Peterson 2004-01-06
    'fill blank cells in column with value above
    Dim wks As Worksheet
    Dim rng As Range
    Dim lastrow As Long
    Set wks = ActiveSheet
    With wks
    'col = ActiveCell.Column
    'Not reqd. Set rng = .UsedRange 'try to reset the lastcell
    lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row
    ' Not reqd. Set rng = Nothing
    On Error Resume Next
    Set rng = .Range(.Cells(2, Col), .Cells(lastrow, Col)) _
    .Cells.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If rng Is Nothing Then
    MsgBox "No blanks found"
    Exit Sub
    Else
    rng.FormulaR1C1 = "=R[-1]C"
    End If
    'replace formulas with values
    With .Cells(1, Col).EntireColumn
    .Value = .Value
    End With
    End With
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Nov 2007
    Posts
    24
    Location

    Code linked to code

    thanks for the help.

    Iit works great i called it twice.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Twice is fine. More than twice, consider using an array. If you keep your code short, it's usually more efficient and easier to maintain.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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