Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Help Modifying Macro - Merge multiple sheets based on a condition

Hello,

I have the following code below. I have a total of 7 seperate worksheets
within a workbook that I want this macro to run on. Essentially I want to be
able to copy any row that has a number in column B. This macro works great
for one worksheet, but I want to be able to take all 7 sheets and combine
into one sheet.

Thanks in advance.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
With Source
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value < "" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))
End If
End If
Next
If Not RowsWithNumbers Is Nothing Then
RowsWithNumbers.EntireRow.Copy Destination.Range("A1")
End If
End With
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Help Modifying Macro - Merge multiple sheets based on a condition

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
For Each sht In Sheets
If UCase(sht.Name) < ("SHEET2") Then
With sht
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value <
"" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X,
"B"))
End If
End If
Next X
If Not RowsWithNumbers Is Nothing Then
LastRow = Destination.Cells(Rows.Count, "B").End(xlUp).Row
RowsWithNumbers.EntireRow.Copy Destination.Range("A" & (LastRow
+ 1))
End If
End With
End If
Next sht
End Sub

"ScottMSP" wrote:

Hello,

I have the following code below. I have a total of 7 seperate worksheets
within a workbook that I want this macro to run on. Essentially I want to be
able to copy any row that has a number in column B. This macro works great
for one worksheet, but I want to be able to take all 7 sheets and combine
into one sheet.

Thanks in advance.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
With Source
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value < "" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))
End If
End If
Next
If Not RowsWithNumbers Is Nothing Then
RowsWithNumbers.EntireRow.Copy Destination.Range("A1")
End If
End With
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Help Modifying Macro - Merge multiple sheets based on a condit

Joel,

The macro failed. It looks like it failed on this line:

RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))

Thoughts?

Thanks in advance.

"Joel" wrote:

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
For Each sht In Sheets
If UCase(sht.Name) < ("SHEET2") Then
With sht
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value <
"" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X,
"B"))
End If
End If
Next X
If Not RowsWithNumbers Is Nothing Then
LastRow = Destination.Cells(Rows.Count, "B").End(xlUp).Row
RowsWithNumbers.EntireRow.Copy Destination.Range("A" & (LastRow
+ 1))
End If
End With
End If
Next sht
End Sub

"ScottMSP" wrote:

Hello,

I have the following code below. I have a total of 7 seperate worksheets
within a workbook that I want this macro to run on. Essentially I want to be
able to copy any row that has a number in column B. This macro works great
for one worksheet, but I want to be able to take all 7 sheets and combine
into one sheet.

Thanks in advance.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
With Source
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value < "" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))
End If
End If
Next
If Not RowsWithNumbers Is Nothing Then
RowsWithNumbers.EntireRow.Copy Destination.Range("A1")
End If
End With
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Help Modifying Macro - Merge multiple sheets based on a condit

Union won't work across multiple worksheets. We need to set the Union to
nothing as we change worksheets.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
For Each sht In Sheets
If UCase(sht.Name) < ("SHEET2") Then
Set RowsWithNumbers = Nothing
With sht
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value <
"" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X,
"B"))
End If
End If
Next X
If Not RowsWithNumbers Is Nothing Then
LastRow = Destination.Cells(Rows.Count, "B").End(xlUp).Row
RowsWithNumbers.EntireRow.Copy Destination.Range("A" & (LastRow
+ 1))
End If
End With
End If
Next sht
End Sub


"ScottMSP" wrote:

Joel,

The macro failed. It looks like it failed on this line:

RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))

Thoughts?

Thanks in advance.

"Joel" wrote:

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
For Each sht In Sheets
If UCase(sht.Name) < ("SHEET2") Then
With sht
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value <
"" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X,
"B"))
End If
End If
Next X
If Not RowsWithNumbers Is Nothing Then
LastRow = Destination.Cells(Rows.Count, "B").End(xlUp).Row
RowsWithNumbers.EntireRow.Copy Destination.Range("A" & (LastRow
+ 1))
End If
End With
End If
Next sht
End Sub

"ScottMSP" wrote:

Hello,

I have the following code below. I have a total of 7 seperate worksheets
within a workbook that I want this macro to run on. Essentially I want to be
able to copy any row that has a number in column B. This macro works great
for one worksheet, but I want to be able to take all 7 sheets and combine
into one sheet.

Thanks in advance.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
With Source
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value < "" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))
End If
End If
Next
If Not RowsWithNumbers Is Nothing Then
RowsWithNumbers.EntireRow.Copy Destination.Range("A1")
End If
End With
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Help Modifying Macro - Merge multiple sheets based on a condit

Thanks Joel. It appears to have worked!

"Joel" wrote:

Union won't work across multiple worksheets. We need to set the Union to
nothing as we change worksheets.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
For Each sht In Sheets
If UCase(sht.Name) < ("SHEET2") Then
Set RowsWithNumbers = Nothing
With sht
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value <
"" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X,
"B"))
End If
End If
Next X
If Not RowsWithNumbers Is Nothing Then
LastRow = Destination.Cells(Rows.Count, "B").End(xlUp).Row
RowsWithNumbers.EntireRow.Copy Destination.Range("A" & (LastRow
+ 1))
End If
End With
End If
Next sht
End Sub


"ScottMSP" wrote:

Joel,

The macro failed. It looks like it failed on this line:

RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))

Thoughts?

Thanks in advance.

"Joel" wrote:

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
For Each sht In Sheets
If UCase(sht.Name) < ("SHEET2") Then
With sht
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value <
"" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X,
"B"))
End If
End If
Next X
If Not RowsWithNumbers Is Nothing Then
LastRow = Destination.Cells(Rows.Count, "B").End(xlUp).Row
RowsWithNumbers.EntireRow.Copy Destination.Range("A" & (LastRow
+ 1))
End If
End With
End If
Next sht
End Sub

"ScottMSP" wrote:

Hello,

I have the following code below. I have a total of 7 seperate worksheets
within a workbook that I want this macro to run on. Essentially I want to be
able to copy any row that has a number in column B. This macro works great
for one worksheet, but I want to be able to take all 7 sheets and combine
into one sheet.

Thanks in advance.

Sub CopyRowsWithNumbersInB()
Dim X As Long
Dim LastRow As Long
Dim Source As Worksheet
Dim Destination As Worksheet
Dim RowsWithNumbers As Range
Set Source = Worksheets("Clinical Nursing")
Set Destination = Worksheets("Sheet2")
With Source
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
For X = 2 To LastRow
If IsNumeric(.Cells(X, "B").Value) And .Cells(X, "B").Value < "" Then
If RowsWithNumbers Is Nothing Then
Set RowsWithNumbers = .Cells(X, "B")
Else
Set RowsWithNumbers = Union(RowsWithNumbers, .Cells(X, "B"))
End If
End If
Next
If Not RowsWithNumbers Is Nothing Then
RowsWithNumbers.EntireRow.Copy Destination.Range("A1")
End If
End With
End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sumif across multiple sheets in excel 2007 based on a condition dave@homedeliverygroup Excel Worksheet Functions 6 May 13th 10 07:42 PM
Sum across sheets based on a condition GoBow777 Excel Worksheet Functions 3 March 8th 08 08:21 PM
Reading Values Between Sheets Based On The Condition born2achieve Excel Programming 3 January 23rd 08 12:31 AM
Referencing Across Sheets Based On Condition Ramesh.S, India Excel Worksheet Functions 1 October 12th 06 02:48 PM
Merge ranges based on condition BrianDP1977[_15_] Excel Programming 3 December 9th 05 01:14 AM


All times are GMT +1. The time now is 12:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"