PDA

View Full Version : Solved: Linking sub's and selecting cells for a sub



coserria
03-26-2008, 11:37 AM
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?


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

mdmackillop
03-26-2008, 11:57 AM
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


Sub Test
Call FillColBlanks(2)
FillColBlanks 4
End sub





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

coserria
03-26-2008, 01:25 PM
thanks for the help.

Iit works great i called it twice.

mdmackillop
03-26-2008, 04:00 PM
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.