Need a way to automate filtering and copying from many different columns
I'm new at macros and have been able to figure out a lot, but I don't even know where to start here. I've attached a portion of my .xlsm file.
What I need is a macro that will take data from Sheet1 and copy it to Sheet2, beginning at cell A2.
The problem is the specific data that I need copied over. I'm even having trouble wording this so it's easily understandable. Here we go:
For every column header "If failed new SR#", if a cell in each of those columns has a number in it, I need that number copied and pasted to Sheet2 IF AND ONLY IF the cell that is two cells to the right of it is blank.
Sheet2 column A will end up being a list of all of these numbers that have no date two cells to the right of it.
Examples from the attached file:
Cell R72 has a number in it. Cell T72 is blank. I need the number in cell R72 copied and pasted to Sheet2 beginning at cell A2.
Cell N93 has a number in it. Cell P93 is blank. I need the number in cell N93 copied and pasted to Sheet2 column A in the next available cell.
And so on until all of the "If failed new SR#" cells that do not have a "Date Installed" two columns over are copied into the list on Sheet2.
I really hope this makes sense, but I'm not even sure it's possible to do.
If this is possible, I would really appreciate some help because I'm having to do this manually right now and it takes some time filtering these columns down and copying the SR#'s I need one at a time.
Re: Need a way to automate filtering and copying from many different columns
Thanks for the quick reply Maras! Sorry for the delay in applying what you gave me. It's been pretty hectic the last few days and I haven't actually run this report since I posted for help.
I assigned your code to a button on a 3rd sheet and ran it. I made no changes. All I get after a couple seconds is the MsgBox "All SRs are closed". Nothing is on Sheet2.
Also, I'm not sure if this is worth noting because your code is WAY above my level (so far above that I'm having trouble following exactly what it's doing) , but I want to point out that there are two sets of numbers in the columns that are side by side:
The "If failed new SR#" and the "If failed new Order#" columns are always side by side and the numbers are always in the same format. How does your code here differentiate between the numbers in those two columns using ptrn = "#[-]##########" (I'm just curious and trying to learn something here).
According to your attachment as a VBA beginner starter :
PHP Code:
Sub Demo1()
Const F = "IF(¤=""If failed new SR#"",COLUMN(¤))", S = "Sheet2"
Dim V
Application.ScreenUpdating = False
Sheets(S).UsedRange.Offset(1).Clear
With Sheets("Sheet1").UsedRange.Columns
For Each V In Filter(.Parent.Evaluate(Replace(F, "¤", .Rows(1).Address)), False, False)
.Range("AK2").Formula = "=AND(NOT(ISBLANK(" & .Cells(2, Val(V)).Address(0, 0) & _
")),ISBLANK(" & .Cells(2, Val(V) + 2).Address(0, 0) & "))"
.AdvancedFilter xlFilterInPlace, .Range("AK1:AK2")
.Item(Val(V)).Offset(1).Copy Sheets(S).Cells(Rows.Count, 1).End(xlUp)(2)
Next
.Range("AK2").Clear
If .Parent.FilterMode Then .Parent.ShowAllData
End With
Application.ScreenUpdating = True
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
According to your attachment another fast demonstration :
PHP Code:
Sub Demo2()
Dim L&, V, C%, R&
L = 1
V = Sheets("Sheet1").UsedRange.Value2
Application.ScreenUpdating = False
With Sheets("Sheet2")
.UsedRange.Offset(1).Clear
For C = 1 To UBound(V, 2)
If V(1, C) = "If failed new SR#" Then
For R = 2 To UBound(V)
If IsEmpty(V(R, C + 2)) Then If Not IsEmpty(V(R, C)) Then L = L + 1: .Cells(L, 1).Value2 = V(R, C)
Next
End If
Next
Application.ScreenUpdating = True
End With
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
According to your attachment another fast demonstration :
PHP Code:
Sub Demo2()
Dim L&, V, C%, R&
L = 1
V = Sheets("Sheet1").UsedRange.Value2
Application.ScreenUpdating = False
With Sheets("Sheet2")
.UsedRange.Offset(1).Clear
For C = 1 To UBound(V, 2)
If V(1, C) = "If failed new SR#" Then
For R = 2 To UBound(V)
If IsEmpty(V(R, C + 2)) Then If Not IsEmpty(V(R, C)) Then L = L + 1: .Cells(L, 1).Value2 = V(R, C)
Next
End If
Next
Application.ScreenUpdating = True
End With
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
@Marc L: This seems to work perfectly!! It also makes me realize exactly how much I still need to learn. At first glance, I can't follow this code at all.
Merci mon ami.
Bookmarks