Morning All,
I have basic VBA knowledge and was hoping to get some help with one that's not working. I'm working in Excel 365.
Basically I'm taking data that's given and I need to do two main actions: 1. Based off of row G (which have three possible options - admin, operations, construction), break them out into new sheets in the same workbook. Then for one of the options (Operations), I need to break it out based off of five potential values in row M. I'll then be saving all of them into separate files.
I was trying to use this below to seperate it based off of row G. Then I was going to try and use it also for the second part/row M and run it for just the operations sheet.
Thanks!!!
Sub SplitSheetIntoMultipleSheetsBasedOnFundType()
Dim objWorksheet As Excel.Worksheet
Dim nLastRow, nRow, nNextRow As Integer
Dim strColumnValue As String
Dim objDictionary As Object
Dim varColumnValues As Variant
Dim varColumnValue As Variant
Dim objSheet As Excel.Worksheet
Set objWorksheet = ActiveSheet
nLastRow = objWorksheet.Range("G" & objWorksheet.Rows.Count).End(xlUp).Row
Set objDictionary = CreateObject("Scripting.Dictionary")
For nRow = 2 To nLastRow
strColumnValue = objWorksheet.Range("G" & nRow).Value
If objDictionary.Exists(strColumnValue) = False Then
objDictionary.Add strColumnValue, 1
End If
Next
varColumnValues = objDictionary.Keys
For i = LBound(varColumnValues) To UBound(varColumnValues)
varColumnValue = varColumnValues(i)
Set objSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
objSheet.Name = varColumnValue
objWorksheet.Rows(1).EntireRow.Copy objSheet.Rows(1)
For nRow = 2 To nLastRow
If CStr(objWorksheet.Range("G" & nRow).Value) = CStr(varColumnValue) Then
objWorksheet.Rows(nRow).EntireRow.Copy
nNextRow = objSheet.Range("G" & objSheet.Rows.Count).End(xlUp).Row + 1
objSheet.Range("A" & nNextRow).PasteSpecial xlPasteValuesAndNumberFormats
End If
Next
objSheet.Columns("A:Y").AutoFit
Next
End Sub
I have basic VBA knowledge and was hoping to get some help with one that's not working. I'm working in Excel 365.
Basically I'm taking data that's given and I need to do two main actions: 1. Based off of row G (which have three possible options - admin, operations, construction), break them out into new sheets in the same workbook. Then for one of the options (Operations), I need to break it out based off of five potential values in row M. I'll then be saving all of them into separate files.
I was trying to use this below to seperate it based off of row G. Then I was going to try and use it also for the second part/row M and run it for just the operations sheet.
Thanks!!!
Sub SplitSheetIntoMultipleSheetsBasedOnFundType()
Dim objWorksheet As Excel.Worksheet
Dim nLastRow, nRow, nNextRow As Integer
Dim strColumnValue As String
Dim objDictionary As Object
Dim varColumnValues As Variant
Dim varColumnValue As Variant
Dim objSheet As Excel.Worksheet
Set objWorksheet = ActiveSheet
nLastRow = objWorksheet.Range("G" & objWorksheet.Rows.Count).End(xlUp).Row
Set objDictionary = CreateObject("Scripting.Dictionary")
For nRow = 2 To nLastRow
strColumnValue = objWorksheet.Range("G" & nRow).Value
If objDictionary.Exists(strColumnValue) = False Then
objDictionary.Add strColumnValue, 1
End If
Next
varColumnValues = objDictionary.Keys
For i = LBound(varColumnValues) To UBound(varColumnValues)
varColumnValue = varColumnValues(i)
Set objSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
objSheet.Name = varColumnValue
objWorksheet.Rows(1).EntireRow.Copy objSheet.Rows(1)
For nRow = 2 To nLastRow
If CStr(objWorksheet.Range("G" & nRow).Value) = CStr(varColumnValue) Then
objWorksheet.Rows(nRow).EntireRow.Copy
nNextRow = objSheet.Range("G" & objSheet.Rows.Count).End(xlUp).Row + 1
objSheet.Range("A" & nNextRow).PasteSpecial xlPasteValuesAndNumberFormats
End If
Next
objSheet.Columns("A:Y").AutoFit
Next
End Sub