How do I make this macro create a new sheet after comparing values to a set variable?

妖精的绣舞 提交于 2020-01-25 07:29:10

问题


I am writing a macro that splits out data to another sheet after comparing values in column A to a set variable.

Column A has all Work Order numbers. I need the macro to create a new sheet for each new work order, and copy all lines to that sheet. It needs to name the sheet with a concatenation of the value found in column A and Column B, and then copy all rows associated with that Work Order to the new sheet.

The prefix is the Work Order. Prefix 12581 should get its own sheet, named 12581-4, and prefix 12695 should get its own sheet named 12695-4. All rows for 12581 should be copied into the sheet named 12581.

This needs to be a loop since there are 39000 rows to sift through.

I thought I'd need to declare a variable for the Work Order/prefix and have the macro compare it to the values in column A. If the value in column A does not match the variable it would create a new sheet and copy over all columns associated with that Work Order.

My code so far:

Sub SortDataByWOxyz()

Option Explicit

Dim Variable_One as Integer
Dim Variable_Two As Integer
Dim cell As Range
Dim Sht_name As String
Dim intLastRow As Integer
Dim intSuffix As Integer

Set Variable_One = 0
Set intSuffix = .Range("b2", .Range("b2").End(xlDown))

intLastRow = ActiveSheet.UsedRange.RowsCount
Set cell = Range ("a2:a" & intLastRow) 'this is so the macro
doesn't keep trying to process rows without values



For Each cell In Sheets(1).Range("A2:F39986") 'Ok, another issue
is that I can't be certain the range of every report I am applying
this macro to. Is there some way to make the range globally applicable, like:
Dim SourceRange As Range
Set SourceRange= .Range("a2", .Range("A2").End(xlDown).End(xlToRight))


If cell.Value <> Variable_One Then

'Grab prefix - suffix
'Create sheet with sheet name
'Copy data

Sheets.Add.Name = Range(a2:a" & intSuffix).value
cell.EntireRow.CopyDestination:=Sheets(Sht_name).Range("a" & cell.Row)

Else: cell.EntireRow.Copy Destination:=Sheets(Sht_name).Range("a" & cell.Row)
End If
Next

End Sub

来源:https://stackoverflow.com/questions/59291658/how-do-i-make-this-macro-create-a-new-sheet-after-comparing-values-to-a-set-vari

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!