Full code I am working with is shown below. I have made a number of modifications that show I can work without the Activate step. I have also eliminated the .value qualifier by declaring variables at the start of the module.
My latest debug problem is that I can get the subroutine CombFeatBoMs() to execute the Do..Loop first time round, but it fails (Macro Error) on the second loop. All variables are changing to show expected values. Any ideas?
Thanks
Kenneth
Dim UnitFeatStr As String
Dim UnitID As String
Dim FeatID As String
Dim ColCount As Integer
Dim RowCount As Integer
Sub CreateProtoBoMv3()
'ABoM contains a worksheet for each Feature - Fxxx convention. Worksheet carries Fxxx as name
'set variable to store name of sheet with Unit Feature Strings
UnitFeatStr = ActiveSheet.Name: ColCount = 0: UnitID = "Null"
'select unit from list
'Worksheet(UnitFeatStr).Select
'set variable UnitID to cell value where cell contains unit ID
Do Until UnitID = ""
ColCount = 1
'Set UnitID = Sheets(UnitFeatStr).Cells(1, ColCount)
UnitID = Sheets(UnitFeatStr).Cells(1, ColCount)
If UnitID = "" Then GoTo Label1:
'create worksheet for unit using UnitID
Sheets(1).Select ' add a sheet in first, leftmost, place
Worksheets.Add
Sheets(1).Name = UnitID
Call CombFeatBoMs
Label1:
Loop
'select unitfeature
'set variable Feat to cell value where cell contains Feature ID
'copy Feature BoM to worksheet UnitID
'next feature
'create pivot table to summarise parts and quantities - new worksheet
'next unit
End Sub
Sub CombFeatBoMs()
'this subroutine combines the list of parts for each feature worksheet
'into a single worksheet for the unit/buildtype identified.
'select unitfeature
'set variable Feat to cell value where cell contains Feature ID
'copy Feature BoM to worksheet UnitID
'next feature
'create pivot table to summarise parts and quantities - new worksheet
'next unit
RowCount = 2
'Set FeatID = Sheets(UnitFeatStr).Cells(RowCount, ColCount)
FeatID = Sheets(UnitFeatStr).Cells(RowCount, ColCount)
MsgBox (FeatID & RowCount & ColCount & UnitID)
' copy headings
'Sheets(FeatID.Value).Activate
Sheets(FeatID).Activate
Range("A5").EntireRow.Select
' Selection.Copy Destination:=Sheets(UnitID.Value).Range("A1")
Selection.Copy Destination:=Sheets(UnitID).Range("A1")
'HELP - the Do loop below executes the first time round and
'HELP returns the expected values of the variables
'HELP On the second traverse of the loop it fails on line
'HELP Worksheets(FeatID).Range("A5").Select
'HELP with a 'Macro Error'
' work through sheets copying the data from each to the UnitID tab
Do Until FeatID = ""
MsgBox (FeatID & RowCount & ColCount & UnitID)
' Worksheets(FeatID.Value).Activate ' make the sheet active
' Worksheets(FeatID.Value).Range("A5").Select
Worksheets(FeatID).Range("A5").Select
Selection.CurrentRegion.Select ' select all cells in this sheets
' select all lines except title
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
' copy cells selected in the new sheet on last line
' Selection.Copy Destination:=Sheets(UnitID.Value).Range("A65536").End(xlUp)(2)
Selection.Copy Destination:=Sheets(UnitID).Range("A65536").End(xlUp)(2)
'increment row counter for next FeatID
' Set RowCount = RowCount + 1
' Set FeatID = Sheets(UnitFeatStr).Cells(RowCount, ColCount)
RowCount = RowCount + 1
FeatID = Sheets(UnitFeatStr).Cells(RowCount, ColCount)
Loop
End Sub