stinkingcedar
New Member
- Joined
- May 2, 2016
- Messages
- 23
Hey everyone,
So I will spare you some of the smaller details, but I am basically writing a code that cycles through all sheets in a workbook, looks for a specific range and then copies and pastes it to another sheet accordingly. The trouble part of the code right now is right at the beginning of the loop:
Some issues with this were expected, because even though the code cycles through every sheet, not every sheet contains the key phrase that the code is looking for that allows it to find the range and copy it. So essentially, I want it to search through each sheet and for the sheets that don't have the phrase I want the macro just to skip over it and move to the next sheet. Thank you!
So I will spare you some of the smaller details, but I am basically writing a code that cycles through all sheets in a workbook, looks for a specific range and then copies and pastes it to another sheet accordingly. The trouble part of the code right now is right at the beginning of the loop:
Code:
Option Explicit
Private Sub CommandButton1_Click()
'Variable declaration
Dim WSCount As Integer, StartCellRow As Integer
Dim i As Integer
Dim sht As Worksheet
Dim region As String
'Getting count of total number of worksheets in workbook
WSCount = Worksheets.Count
Application.ScreenUpdating = False
Worksheets("Americas Data Load").Range("E4:Y903").ClearContents
Worksheets("Americas Data Load").Range("A3:A903").ClearContents
Worksheets("International Data Load").Range("E4:Y903").ClearContents
Worksheets("International Data Load").Range("A4:A903").ClearContents
Worksheets("Latin America Data Load").Range("E4:Y903").ClearContents
Worksheets("Latin America Data Load").Range("A4:A903").ClearContents
'Start of loop, goes from the first project input data sheet to the last sheet in the book
'Because of this, all non project input data sheets must come before all other tabs for this macro to work properly
For i = 1 To WSCount
'Looks for an identifier in cell D1 for each project, then selects and copies the correct range based on the key statement
region = Sheets(i).Range("D1").Text
Set sht = Sheets(i)
Sheets(i).UsedRange
[COLOR=#ff0000] StartCellRow = sht.Cells.Find("Please DO NOT TOUCH formula driven:", LookAt:=xlWhole, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row + 1[/COLOR]
sht.Range(sht.Cells(StartCellRow, 6), sht.Cells(StartCellRow + 29, 27)).Copy
'Portion of code that seperates each project input data sheet based on the identifier
Select Case region
'If project falls under Americas
Case Is = "A"
'Pastes selected range under last pasted range in data load
Sheets("Americas Data Load").Range("E" & Rows.Count).End(xlUp).Offset(1).PasteSpecial (xlPasteValues)
sht.Range("E1").Copy
'Pastes project name based on the contents of cell A4
If Sheets("Americas Data Load").Range("A4") = "" Then
Sheets("Americas Data Load").Range("A4").PasteSpecial (xlPasteValues)
Else
Sheets("Americas Data Load").Range("A" & Rows.Count).End(xlUp).Offset(30).PasteSpecial (xlPasteValues)
End If
'If project falls under International
Case Is = "I"
'Pastes selected range under last pasted range in data load
Sheets("International Data Load").Range("E" & Rows.Count).End(xlUp).Offset(1).PasteSpecial (xlPasteValues)
sht.Range("E1").Copy
'Pastes project name based on the contents of cell A4
If Sheets("International Data Load").Range("A4") = "" Then
Sheets("International Data Load").Range("A4").PasteSpecial (xlPasteValues)
Else
Sheets("International Data Load").Range("A" & Rows.Count).End(xlUp).Offset(30).PasteSpecial (xlPasteValues)
End If
'If project falls under Latin America
Case Is = "L"
'Pastes selected range under last pasted range in data load
Sheets("Latin America Data Load").Range("E" & Rows.Count).End(xlUp).Offset(1).PasteSpecial (xlPasteValues)
sht.Range("E1").Copy
'Pastes project name based on the contents of cell A4
If Sheets("Latin America Data Load").Range("A4") = "" Then
Sheets("Latin America Data Load").Range("A4").PasteSpecial (xlPasteValues)
Else
Sheets("Latin America Data Load").Range("A" & Rows.Count).End(xlUp).Offset(30).PasteSpecial (xlPasteValues)
End If
Case Else
Exit Sub
End Select
'End of Loop, will continue to cycle through each worksheet until it reaches the last in the book
Next i
End Sub
Some issues with this were expected, because even though the code cycles through every sheet, not every sheet contains the key phrase that the code is looking for that allows it to find the range and copy it. So essentially, I want it to search through each sheet and for the sheets that don't have the phrase I want the macro just to skip over it and move to the next sheet. Thank you!