Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Hey,
I have successfully used this macro to take information from one worksheet to another, however I need to be able to do this with multiple in the same workbook. In one workbook I have four master worksheets and in each master worksheet there are names in column A which have corrosponding worksheets. I need to have the row connected to the name in the masters copied into each corrosponding worksheet and these names may be changed throughout the day weekly in the master sheets. This is the formula for the macro I used to do this with one name: Sub copyit() Dim MyRange, MyRange1 As Range Sheets("LIA").Select lastrow = Cells(Rows.Count, "A").End(xlUp).Row Set MyRange = Sheets("LIA").Range("A1:A400" & lastrow) For Each c In MyRange If c.Value = "ST" Then If MyRange1 Is Nothing Then Set MyRange1 = c.EntireRow Else Set MyRange1 = Union(MyRange1, c.EntireRow) End If End If Next If Not MyRange1 Is Nothing Then MyRange1.Copy End If Sheets("ST").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub 'LIA' is one of the master sheets and 'ST' is the name I had it copy from column A into the worksheet titled 'ST', I have about 13 of these names just in the master LIA and each have their own worksheet and then 3 other master sheets with their own names and corrosponding worksheets all in one workbook. Can I make changes to the existing macro formula to allow for this complicated need or do I have to make 20 different macros? If someone could be a genius and help me sort this out I would be in awe. -- Thanks, Kendra |
#2
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Try
'See comments like this line 'This assumes that the sheet with each name already exists 'otherwise you wil have to add code to check for existence and then add it ' Sub copyit() Dim shName As String Dim MyRange As Range 'Dim shCount As Integer 'shCount = Worksheets.Count 'Dim lastRow(shCount) As Long 'For i = 1 To shCount 'lastRow(i) = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row 'Next Dim srcSheet(4) As String 'Change the names to the four sheets you want to copy from srcSheet(1) = "LIA" srcSheet(2) = "LIA" srcSheet(3) = "LIA" srcSheet(4) = "LIA" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.Name < srcSheet(1) And ws.Name < srcSheet(2) And ws.Name < srcSheet(3) And ws.Name < srcSheet(4)) Then Sheets(ws.Name).UsedRange.ClearContents End If Next For i = 1 To 4 Set MyRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In MyRange shName = c.Value If shName < "" Then c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(Sheets(shName).C ells(Rows.Count, "A").End(xlUp).Row + 1, 1) End If Next Next End Sub "Kendra" wrote: Hey, I have successfully used this macro to take information from one worksheet to another, however I need to be able to do this with multiple in the same workbook. In one workbook I have four master worksheets and in each master worksheet there are names in column A which have corrosponding worksheets. I need to have the row connected to the name in the masters copied into each corrosponding worksheet and these names may be changed throughout the day weekly in the master sheets. This is the formula for the macro I used to do this with one name: Sub copyit() Dim MyRange, MyRange1 As Range Sheets("LIA").Select lastrow = Cells(Rows.Count, "A").End(xlUp).Row Set MyRange = Sheets("LIA").Range("A1:A400" & lastrow) For Each c In MyRange If c.Value = "ST" Then If MyRange1 Is Nothing Then Set MyRange1 = c.EntireRow Else Set MyRange1 = Union(MyRange1, c.EntireRow) End If End If Next If Not MyRange1 Is Nothing Then MyRange1.Copy End If Sheets("ST").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub 'LIA' is one of the master sheets and 'ST' is the name I had it copy from column A into the worksheet titled 'ST', I have about 13 of these names just in the master LIA and each have their own worksheet and then 3 other master sheets with their own names and corrosponding worksheets all in one workbook. Can I make changes to the existing macro formula to allow for this complicated need or do I have to make 20 different macros? If someone could be a genius and help me sort this out I would be in awe. -- Thanks, Kendra |
#3
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Thank you, and sorry about the 2nd post.
I do have a couple questions to clarify the code you provided. Other than: (I've named these sheets as seen) srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" Where do I indicate the name for the macro to find and copy the corrosponding row into the matching worksheet. Ex: I need it to find all rows with 'ST', 'SE', and 'BW' in the 'LIA' worksheet and sort them into the worksheets named as such. Also I need it to paste starting at 'A6' Thanks in advance for your help. -- Thanks, Kendra "Sheeloo" wrote: Try 'See comments like this line 'This assumes that the sheet with each name already exists 'otherwise you wil have to add code to check for existence and then add it ' Sub copyit() Dim shName As String Dim MyRange As Range 'Dim shCount As Integer 'shCount = Worksheets.Count 'Dim lastRow(shCount) As Long 'For i = 1 To shCount 'lastRow(i) = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row 'Next Dim srcSheet(4) As String 'Change the names to the four sheets you want to copy from srcSheet(1) = "LIA" srcSheet(2) = "LIA" srcSheet(3) = "LIA" srcSheet(4) = "LIA" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.Name < srcSheet(1) And ws.Name < srcSheet(2) And ws.Name < srcSheet(3) And ws.Name < srcSheet(4)) Then Sheets(ws.Name).UsedRange.ClearContents End If Next For i = 1 To 4 Set MyRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In MyRange shName = c.Value If shName < "" Then c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(Sheets(shName).C ells(Rows.Count, "A").End(xlUp).Row + 1, 1) End If Next Next End Sub "Kendra" wrote: Hey, I have successfully used this macro to take information from one worksheet to another, however I need to be able to do this with multiple in the same workbook. In one workbook I have four master worksheets and in each master worksheet there are names in column A which have corrosponding worksheets. I need to have the row connected to the name in the masters copied into each corrosponding worksheet and these names may be changed throughout the day weekly in the master sheets. This is the formula for the macro I used to do this with one name: Sub copyit() Dim MyRange, MyRange1 As Range Sheets("LIA").Select lastrow = Cells(Rows.Count, "A").End(xlUp).Row Set MyRange = Sheets("LIA").Range("A1:A400" & lastrow) For Each c In MyRange If c.Value = "ST" Then If MyRange1 Is Nothing Then Set MyRange1 = c.EntireRow Else Set MyRange1 = Union(MyRange1, c.EntireRow) End If End If Next If Not MyRange1 Is Nothing Then MyRange1.Copy End If Sheets("ST").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub 'LIA' is one of the master sheets and 'ST' is the name I had it copy from column A into the worksheet titled 'ST', I have about 13 of these names just in the master LIA and each have their own worksheet and then 3 other master sheets with their own names and corrosponding worksheets all in one workbook. Can I make changes to the existing macro formula to allow for this complicated need or do I have to make 20 different macros? If someone could be a genius and help me sort this out I would be in awe. -- Thanks, Kendra |
#4
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Try this
(I have sacrificed efficiency for clarity).. Sub copyit() Dim i, j As Integer j = 1 Dim shName As String Dim myRange As Range Dim sh As Worksheet Dim destRow(3) As Integer destRow(1) = 5 destRow(2) = 5 destRow(3) = 5 Dim srcSheet(4) As String srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.name < srcSheet(1) And ws.name < srcSheet(2) And ws.name < srcSheet(3) And ws.name < srcSheet(4)) Then Sheets(ws.name).UsedRange.ClearContents End If Next For i = 1 To 4 Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In myRange shName = c.Value 'ST', 'SE', and 'BW' If (shName = "ST" Or shName = "SE" Or shName = "BW") Then If shName = "ST" Then j = 1 End If If shName = "SE" Then j = 2 End If If shName = "BW" Then j = 3 End If destRow(j) = destRow(j) + 1 c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(destRow(j), 1) End If Next c Next i End Sub "Kendra" wrote: Thank you, and sorry about the 2nd post. I do have a couple questions to clarify the code you provided. Other than: (I've named these sheets as seen) srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" Where do I indicate the name for the macro to find and copy the corrosponding row into the matching worksheet. Ex: I need it to find all rows with 'ST', 'SE', and 'BW' in the 'LIA' worksheet and sort them into the worksheets named as such. Also I need it to paste starting at 'A6' Thanks in advance for your help. -- Thanks, Kendra "Sheeloo" wrote: Try 'See comments like this line 'This assumes that the sheet with each name already exists 'otherwise you wil have to add code to check for existence and then add it ' Sub copyit() Dim shName As String Dim MyRange As Range 'Dim shCount As Integer 'shCount = Worksheets.Count 'Dim lastRow(shCount) As Long 'For i = 1 To shCount 'lastRow(i) = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row 'Next Dim srcSheet(4) As String 'Change the names to the four sheets you want to copy from srcSheet(1) = "LIA" srcSheet(2) = "LIA" srcSheet(3) = "LIA" srcSheet(4) = "LIA" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.Name < srcSheet(1) And ws.Name < srcSheet(2) And ws.Name < srcSheet(3) And ws.Name < srcSheet(4)) Then Sheets(ws.Name).UsedRange.ClearContents End If Next For i = 1 To 4 Set MyRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In MyRange shName = c.Value If shName < "" Then c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(Sheets(shName).C ells(Rows.Count, "A").End(xlUp).Row + 1, 1) End If Next Next End Sub "Kendra" wrote: Hey, I have successfully used this macro to take information from one worksheet to another, however I need to be able to do this with multiple in the same workbook. In one workbook I have four master worksheets and in each master worksheet there are names in column A which have corrosponding worksheets. I need to have the row connected to the name in the masters copied into each corrosponding worksheet and these names may be changed throughout the day weekly in the master sheets. This is the formula for the macro I used to do this with one name: Sub copyit() Dim MyRange, MyRange1 As Range Sheets("LIA").Select lastrow = Cells(Rows.Count, "A").End(xlUp).Row Set MyRange = Sheets("LIA").Range("A1:A400" & lastrow) For Each c In MyRange If c.Value = "ST" Then If MyRange1 Is Nothing Then Set MyRange1 = c.EntireRow Else Set MyRange1 = Union(MyRange1, c.EntireRow) End If End If Next If Not MyRange1 Is Nothing Then MyRange1.Copy End If Sheets("ST").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub 'LIA' is one of the master sheets and 'ST' is the name I had it copy from column A into the worksheet titled 'ST', I have about 13 of these names just in the master LIA and each have their own worksheet and then 3 other master sheets with their own names and corrosponding worksheets all in one workbook. Can I make changes to the existing macro formula to allow for this complicated need or do I have to make 20 different macros? If someone could be a genius and help me sort this out I would be in awe. -- Thanks, Kendra |
#5
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
I keep getting a 'Compile Error: Syntax Error' pop up.
I feel like a complete idiot but I just can not get this to work and these lines: If (ws.name < srcSheet(1) And ws.name < srcSheet(2) And ws.name < srcSheet(3) And ws.name < srcSheet(4)) Then Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) Are red in the module. Sorry in advance and thank you. -- Thanks, Kendra "Sheeloo" wrote: Try this (I have sacrificed efficiency for clarity).. Sub copyit() Dim i, j As Integer j = 1 Dim shName As String Dim myRange As Range Dim sh As Worksheet Dim destRow(3) As Integer destRow(1) = 5 destRow(2) = 5 destRow(3) = 5 Dim srcSheet(4) As String srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.name < srcSheet(1) And ws.name < srcSheet(2) And ws.name < srcSheet(3) And ws.name < srcSheet(4)) Then Sheets(ws.name).UsedRange.ClearContents End If Next For i = 1 To 4 Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In myRange shName = c.Value 'ST', 'SE', and 'BW' If (shName = "ST" Or shName = "SE" Or shName = "BW") Then If shName = "ST" Then j = 1 End If If shName = "SE" Then j = 2 End If If shName = "BW" Then j = 3 End If destRow(j) = destRow(j) + 1 c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(destRow(j), 1) End If Next c Next i End Sub "Kendra" wrote: Thank you, and sorry about the 2nd post. I do have a couple questions to clarify the code you provided. Other than: (I've named these sheets as seen) srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" Where do I indicate the name for the macro to find and copy the corrosponding row into the matching worksheet. Ex: I need it to find all rows with 'ST', 'SE', and 'BW' in the 'LIA' worksheet and sort them into the worksheets named as such. Also I need it to paste starting at 'A6' Thanks in advance for your help. -- Thanks, Kendra "Sheeloo" wrote: Try 'See comments like this line 'This assumes that the sheet with each name already exists 'otherwise you wil have to add code to check for existence and then add it ' Sub copyit() Dim shName As String Dim MyRange As Range 'Dim shCount As Integer 'shCount = Worksheets.Count 'Dim lastRow(shCount) As Long 'For i = 1 To shCount 'lastRow(i) = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row 'Next Dim srcSheet(4) As String 'Change the names to the four sheets you want to copy from srcSheet(1) = "LIA" srcSheet(2) = "LIA" srcSheet(3) = "LIA" srcSheet(4) = "LIA" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.Name < srcSheet(1) And ws.Name < srcSheet(2) And ws.Name < srcSheet(3) And ws.Name < srcSheet(4)) Then Sheets(ws.Name).UsedRange.ClearContents End If Next For i = 1 To 4 Set MyRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In MyRange shName = c.Value If shName < "" Then c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(Sheets(shName).C ells(Rows.Count, "A").End(xlUp).Row + 1, 1) End If Next Next End Sub "Kendra" wrote: Hey, I have successfully used this macro to take information from one worksheet to another, however I need to be able to do this with multiple in the same workbook. In one workbook I have four master worksheets and in each master worksheet there are names in column A which have corrosponding worksheets. I need to have the row connected to the name in the masters copied into each corrosponding worksheet and these names may be changed throughout the day weekly in the master sheets. This is the formula for the macro I used to do this with one name: Sub copyit() Dim MyRange, MyRange1 As Range Sheets("LIA").Select lastrow = Cells(Rows.Count, "A").End(xlUp).Row Set MyRange = Sheets("LIA").Range("A1:A400" & lastrow) For Each c In MyRange If c.Value = "ST" Then If MyRange1 Is Nothing Then Set MyRange1 = c.EntireRow Else Set MyRange1 = Union(MyRange1, c.EntireRow) End If End If Next If Not MyRange1 Is Nothing Then MyRange1.Copy End If Sheets("ST").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub 'LIA' is one of the master sheets and 'ST' is the name I had it copy from column A into the worksheet titled 'ST', I have about 13 of these names just in the master LIA and each have their own worksheet and then 3 other master sheets with their own names and corrosponding worksheets all in one workbook. Can I make changes to the existing macro formula to allow for this complicated need or do I have to make 20 different macros? If someone could be a genius and help me sort this out I would be in awe. -- Thanks, Kendra |
#6
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Don't be harsh on yourself...
If (ws.name < srcSheet(1) And ws.name < srcSheet(2) And ws.name < srcSheet(3) And ws.name < srcSheet(4)) Then The above (from IF to THEN) should be on one line Similary the whole of (from SET to .Row)) Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) should be on one line These should also be on one line Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) "Kendra" wrote: I keep getting a 'Compile Error: Syntax Error' pop up. I feel like a complete idiot but I just can not get this to work and these lines: If (ws.name < srcSheet(1) And ws.name < srcSheet(2) And ws.name < srcSheet(3) And ws.name < srcSheet(4)) Then Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) Are red in the module. Sorry in advance and thank you. -- Thanks, Kendra "Sheeloo" wrote: Try this (I have sacrificed efficiency for clarity).. Sub copyit() Dim i, j As Integer j = 1 Dim shName As String Dim myRange As Range Dim sh As Worksheet Dim destRow(3) As Integer destRow(1) = 5 destRow(2) = 5 destRow(3) = 5 Dim srcSheet(4) As String srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.name < srcSheet(1) And ws.name < srcSheet(2) And ws.name < srcSheet(3) And ws.name < srcSheet(4)) Then Sheets(ws.name).UsedRange.ClearContents End If Next For i = 1 To 4 Set myRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In myRange shName = c.Value 'ST', 'SE', and 'BW' If (shName = "ST" Or shName = "SE" Or shName = "BW") Then If shName = "ST" Then j = 1 End If If shName = "SE" Then j = 2 End If If shName = "BW" Then j = 3 End If destRow(j) = destRow(j) + 1 c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(destRow(j), 1) End If Next c Next i End Sub "Kendra" wrote: Thank you, and sorry about the 2nd post. I do have a couple questions to clarify the code you provided. Other than: (I've named these sheets as seen) srcSheet(1) = "LIA" srcSheet(2) = "MCP" srcSheet(3) = "SLCP" srcSheet(4) = "CCP" Where do I indicate the name for the macro to find and copy the corrosponding row into the matching worksheet. Ex: I need it to find all rows with 'ST', 'SE', and 'BW' in the 'LIA' worksheet and sort them into the worksheets named as such. Also I need it to paste starting at 'A6' Thanks in advance for your help. -- Thanks, Kendra "Sheeloo" wrote: Try 'See comments like this line 'This assumes that the sheet with each name already exists 'otherwise you wil have to add code to check for existence and then add it ' Sub copyit() Dim shName As String Dim MyRange As Range 'Dim shCount As Integer 'shCount = Worksheets.Count 'Dim lastRow(shCount) As Long 'For i = 1 To shCount 'lastRow(i) = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row 'Next Dim srcSheet(4) As String 'Change the names to the four sheets you want to copy from srcSheet(1) = "LIA" srcSheet(2) = "LIA" srcSheet(3) = "LIA" srcSheet(4) = "LIA" 'Comment the next five lines if you do not want to clear the name sheets For Each ws In Worksheets If (ws.Name < srcSheet(1) And ws.Name < srcSheet(2) And ws.Name < srcSheet(3) And ws.Name < srcSheet(4)) Then Sheets(ws.Name).UsedRange.ClearContents End If Next For i = 1 To 4 Set MyRange = Sheets(srcSheet(i)).Range("A1:A" & Sheets(srcSheet(i)).Cells(Rows.Count, "A").End(xlUp).Row) For Each c In MyRange shName = c.Value If shName < "" Then c.EntireRow.Copy _ Destination:=Sheets(shName).Cells(Sheets(shName).C ells(Rows.Count, "A").End(xlUp).Row + 1, 1) End If Next Next End Sub "Kendra" wrote: Hey, I have successfully used this macro to take information from one worksheet to another, however I need to be able to do this with multiple in the same workbook. In one workbook I have four master worksheets and in each master worksheet there are names in column A which have corrosponding worksheets. I need to have the row connected to the name in the masters copied into each corrosponding worksheet and these names may be changed throughout the day weekly in the master sheets. This is the formula for the macro I used to do this with one name: Sub copyit() Dim MyRange, MyRange1 As Range Sheets("LIA").Select lastrow = Cells(Rows.Count, "A").End(xlUp).Row Set MyRange = Sheets("LIA").Range("A1:A400" & lastrow) For Each c In MyRange If c.Value = "ST" Then If MyRange1 Is Nothing Then Set MyRange1 = c.EntireRow Else Set MyRange1 = Union(MyRange1, c.EntireRow) End If End If Next If Not MyRange1 Is Nothing Then MyRange1.Copy End If Sheets("ST").Select Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False End Sub 'LIA' is one of the master sheets and 'ST' is the name I had it copy from column A into the worksheet titled 'ST', I have about 13 of these names just in the master LIA and each have their own worksheet and then 3 other master sheets with their own names and corrosponding worksheets all in one workbook. Can I make changes to the existing macro formula to allow for this complicated need or do I have to make 20 different macros? If someone could be a genius and help me sort this out I would be in awe. -- Thanks, Kendra |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Macro Help Needed - Excel 2007 - Print Macro with Auto Sort | Excel Worksheet Functions | |||
My excel macro recorder no longer shows up when recording macro | Excel Discussion (Misc queries) | |||
Macro needed to Paste Values and prevent Macro operation | Excel Discussion (Misc queries) | |||
Macro needed to Paste Values and prevent Macro operation | Excel Discussion (Misc queries) |