Macros in Excel 2003

B

Bishop

I want to create a macro to perform the same task but I need it to
accommodate changes in data from spreadsheet to spreadsheet. For example,
say I have a spreadsheet that has data in the first 20 rows of column A. I
make a macro to find the first empty cell in column A which, in this case, is
A21 and then highlight all the rows above it. So rows 1 through 20 will be
highlighted. But let's say the next spreadsheet I run the macro in has data
in the first 50 rows of column A. Because I made the macro in the first
sheet the macro will find the first empty cell at A51 (which is right) but
then it highlights rows 1-20 just like in the first one. Surely, there's a
way to make a macro virtual, no? So no matter how many rows have data the
macro will find the first empty cell, whatever that is, and then highlight
all the rows above that. How can this be done?
 
P

PA

this code will select row 1 to the row of the last used cell in column A

Range(Cells(1, 1), Cells(Cells(Columns(1).Rows.Count, 1).End(xlUp).Row,
1)).EntireRow.Select

pa
 
B

Bishop

I copied this in exactly as it is here and it returned a syntax error. I'm
including a copy of my whole macro if that'll help.

Sub RepeatRepConsolidation()
'
' RepeatRepConsolidation Macro
' Macro recorded 3/3/2009 by bminter
'
' Keyboard Shortcut: Ctrl+k
'
Cells.Select
Selection.Copy
Sheets("Workspace").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.ClearFormats
Rows("1:1").Select
Selection.Cut
Sheets("Repeats").Select
ActiveSheet.Paste
Sheets("Workspace").Select
Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("P1") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal,
DataOption2 _
:=xlSortNormal
Sheets("Repeats").Select
Selection.Cut
Sheets("Workspace").Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Rows("2:2").Select
ActiveWindow.FreezePanes = True
Range("B:O,R:AA").Select
Selection.Delete Shift:=xlToLeft
Columns("A:C").Select
Selection.Copy
Sheets("Repeats").Select
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1:C5000").AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Range(Cells(1, 1), Cells(Cells(Columns(1).Rows.Count, 1).End(xlUp).Row,
1)).EntireRow.Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub
 
G

Gord Dibben

What does "highlighted" mean?

Selected or colored?

Sub hilight()
Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)).Select
Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp)).Interior.ColorIndex = 6
End Sub

You choose which line you want to keep.


Gord Dibben MS Excel MVP
 
B

Bishop

"Selected" is what I meant, yes. And thank you for the response. This works
great. Incidentally, I scoured the help files for this information but came
up with nothing. Is this something that's not in the help files or did it
simply evade me? One more question. I have a situation where I need to
consolidate a specific worksheet in multiple workbooks into one worksheet.
I'm looking over the consolidate help files and there doesn't seem to be a
way to do it. I did, however, see that you guys offer a Consolidation
Assistant for $50. Is that my only option for my particular need?
 
J

JLGWhiz

I did, however, see that you guys offer a Consolidation
Assistant for $50.

In case you think people on this site, who offer assistance, work for
Microsoft or Google or any other company offering products for sale on this
site, you should know that they are all volunteers located in different parts
of the world who just don't mind sharing what they know with others at no
cost, up to a point. Some of the volunteers are also in the business of
selling their services, but not through this media.
 
G

Gord Dibben

I'm not one of the "you guys".

I'm simply a retired old gent who answers questions(and asks some) on these
Excel news groups.

Very rarely do one of "you guys" show up on these groups.

Microsoft sponsors these groups but have very little input.

Check out Ron de Bruin's site for methods of Copy/Paste/Merge for getting
data from multiple books to one book.

http://www.rondebruin.nl/tips.htm

Maybe this one?

http://www.rondebruin.nl/summary2.htm


Gord Dibben MS Excel MVP
 
B

Bishop

Well, Gord, whoever you are I would like to say thank you for your
assistance. I took your advice and checked out Ron's instructions. I
"thought" I found what I was looking for but I can't seem to get it to work
right. Here's what I'm trying to do:

I have a folder called "Compilation Complete" on my C: drive. It has
several Excel Files in it. In each file (workbook) there is a sheet called
Rootcause. I'm trying to get all the data from each Rootcause sheet from all
the workbooks and compile in one single sheet. The code below simply creates
a destination sheet but there's no data there. Nothing. What am I doing
wrong?

Function RDB_Last(choice As Integer, rng As Range)
'The following function is used in the selection process from the source
'By Ron de Bruin, 5 May 2008
'A choice of 1 = last row.
'A choice of 2 = last column.
'A choice of 3 = last cell.
Dim lrw As Long
Dim lcol As Integer

Select Case choice

Case 1:
On Error Resume Next
RDB_Last = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0

Case 2:
On Error Resume Next
RDB_Last = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0

Case 3:
On Error Resume Next
lrw = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0

On Error Resume Next
lcol = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0

On Error Resume Next
RDB_Last = rng.Parent.Cells(lrw, lcol).Address(False, False)
If Err.Number > 0 Then
RDB_Last = rng.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0

End Select
End Function

Sub MergeAllWorkbooks()
'This program consolidates a specific worksheet from all workbooks
'in a folder. In this case I am consolidating all Rootcause worksheets
'from all PF workbooks for each individual center.

Dim FirstCell As String
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long


'This is the path to the folders where the workbooks are contained.
MyPath = "C:\Compilation Complete"

'Add a slash at the end of the path if needed.
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder, exit.
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the myFiles array with the list of Excel files
'in the search folder.
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop

'Set various application properties.
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With

'Add a new workbook with one sheet.
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

'Loop through all files in the myFiles array.
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0

If Not mybook Is Nothing Then
On Error Resume Next

'Change this range to accommodate the range in the worksheet.
With mybook.Worksheets(Rootcause)
FirstCell = "A2"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(1,
..Rows))
'Test if the row of the last cell is equal to or greater
than the row of the first cell.
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With


If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'If source range uses all columns then skip this file.
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0

If Not sourceRange Is Nothing Then

SourceRcount = sourceRange.Rows.Count

If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "There are not enough rows in the target
worksheet."
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else

'Copy the file name in column A.
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(FNum)
End With

'Set the destination range.
Set destrange = BaseWks.Range("B" & rnum)

'Copy the values from the source range to the
destination range.
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count,
..Columns.Count)
End With
destrange.Value = sourceRange.Value

rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If

Next FNum
BaseWks.Columns.AutoFit
End If

ExitTheSub:
' Restore the application properties.
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top