Exclude Specific Columns from Range in VBA

alpha_xero

New Member
Joined
Sep 14, 2022
Messages
6
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
Hello,
I have a code that returns unique rows from one sheet to another.
I want to exclude specific columns from the range from B5:P1048576 and show them in Sheets("Item-Wise Summary").Range("A5:O5")

VBA Code:
Sub RemoveDuplicates()

Dim rngDatabase As Range

'define the database

   Set rngDatabase = Sheets("Transaction Entry").Range("B5:P1048576")

'filter the database to a new range with a unique set to true

rngDatabase.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Item-Wise Summary").Range("A5:O5"), Unique:=True

End Sub
 

Attachments

  • Screenshot 2022-09-14 181124.jpg
    Screenshot 2022-09-14 181124.jpg
    125.7 KB · Views: 25
  • Screenshot 2022-09-14 181218.jpg
    Screenshot 2022-09-14 181218.jpg
    109.3 KB · Views: 26

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Exclude Specific Columns from Range in VBA
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Did you understand the reply you received on the cross posting ?
If on the destination sheet you only put the column headings of the columns you want and they match "exactly" the headings on the source sheet, it will only transfer the headings that match to the destination sheet.
 
Upvote 0
Did you understand the reply you received on the cross posting ?
If on the destination sheet you only put the column headings of the columns you want and they match "exactly" the headings on the source sheet, it will only transfer the headings that match to the destination sheet.
Hey Alex,

I don't know why I can't reply on the cross post I've tried soo many times.
Thank you for your reply but I don't think that'll work because this is what the macro actually does.

Watch This

I need all those columns in the "Transaction Entry" sheet but instead of the pivot table, I need a sheet of unique rows of each item and calculate stock based on the actions using sumifs.

In the "Item-Wise Summary" sheet I just need to exclude some of the columns from Range "A5:O5" but I can not move those away because on the other sheet I need those columns.

I've tried using something like this but it doesn't seem to work

VBA Code:
Sub RemoveDuplicates()
  
   Dim rngDatabase1 As Range
   Dim rngDatabase2 As Range
   Dim uRng As Range
'define the database
   Set rngDatabase1 = Sheets("Transaction Entry").Range("B5:D1048576")
   Set rngDatabase2 = Sheets("Transaction Entry").Range("I5:P1048576")
   Set uRng = Sheets("Transaction Entry").Range(rngDatabase1, rngDatabase2)
'filter the database to a new range with unique set to true
uRng.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Rack-Wise Stock Report").Range("A5:K5"), Unique:=True
End Sub
 
Upvote 0
I'm afraid that is not clear at all.
Your images seem to indicate that the only column being dropped is column A.
Please provide an XL2BB of a sample of the data you are starting with and what you are expecting as a result.
The images also indicate that you have a number of merged columns. If that is the case that is a terrible idea and I can't see any reason for merging cells based on the images.

Xl2BB
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Alright here's the XL2BB of the 2 worksheets.


G&F Inventory File V1.xlsm
ABCDEFGHIJKLMNOPQRST
1Good & Fast LTD
2
3Received Issue Transaction Entry
5Transaction DateTracking No.WO No./ PR No./ Master Refer No.PO No. / Job No.Supplier ChallanSupplier NameSection NameDirect/IndirectItem CategoryItem Name & discriptionSIZEGSMCOLOR Unit Price UOMRack LocationAction NameQuantityIssued Material Receiver NameRemarks
613-Sep-22INK45WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDG-6,Gloss Granium RedN/AN/AN/A480.00KgBW-R1-B1Opening Stock10
713-Sep-22INK45WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDG-6,Gloss Granium RedN/AN/AN/A480.00Kg BW-R1-B1 Issue to Production10
813-Sep-22INK45WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDG-6,Gloss Granium RedN/AN/AN/A480.00Kg BW-R1-B1 Receive From Supplier10
913-Sep-22INK45WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDG-6,Gloss Granium RedN/AN/AN/A480.00Kg BW-R1-B1 Return from Production5
1013-Sep-22INK40WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDH021,Orange-cN/AN/AN/A480.00 Kg BW-R2-B1 Return to Supplier50
11
Transaction Entry
Cells with Data Validation
CellAllowCriteria
P6:P10List='Rack No Library'!$E$4:$E$65
Q6:Q10List='G&F_Library'!$P$4:$P$13







G&F Inventory File V1.xlsm
ABCDEFGHIJKLMNOP
1Good & Fast LTD
2Rack Wise Stock Report
3
4 Subtotal: (35)
5Tracking No.WO No./ PR No./ Master Refer No.PO No. / Job No.Supplier ChallanSupplier NameSection NameDirect/IndirectItem CategoryItem Name & discriptionSIZEGSMCOLOR Unit Price UOMRack LocationQuantity
6INK45WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDG-6,Gloss Granium RedN/AN/AN/A480.00KgBW-R1-B115
7INK40WO-1011182862LIZS.SupplierOffset Section (Ink Item)DirectInkDH021,Orange-cN/AN/AN/A480.00 Kg BW-R2-B1 -50
8
9
10
Rack-Wise Stock Report
 
Upvote 0
Based on that the only columns being dropped appear to be Column A "Transaction date" and Column Q "Action Name".
Quantity seems to be a calculation that is dependant on the Action Name. Ideally it needs a key / lookup table to determine which Action Names are positive and which are negative ? How many actions are there all up and do you have a lookup table for it ?
How are you doing it now ?

Is the Rack-Wise sheet going to be blank except for the headings before running the macro ?
 
Upvote 0
Haven't heard back from on the questions but give this a try.

VBA Code:
Sub CreateStockReport_Dict()

    Dim tranSht As Worksheet, stockSht As Worksheet
    Dim tranRng As Range, stockRng As Range
    Dim tranArr As Variant, sumArr As Variant, outArr As Variant
    Dim stockHdgRow As Long
    Dim tranLastRow As Long, stockLastRow As Long
    Dim i As Long, j As Long, z As Long, k As Long
    Dim ActionName As String, tranValAdd As Double
    
    Dim tranDict As Object, dictKey As String

        
    Set tranSht = Worksheets("Transaction Entry")
    With tranSht
        tranLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set tranRng = .Range(.Cells(5, "A"), .Cells(tranLastRow, "R"))
        tranArr = tranRng
    End With
    
    Set stockSht = Worksheets("Rack-Wise Stock Report")
    With stockSht
        stockLastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        stockHdgRow = 5
        Set stockRng = .Range(.Cells(stockHdgRow, "A"), .Cells(stockHdgRow, "P"))
    End With
    
    ReDim outArr(1 To UBound(tranArr), 1 To stockRng.Columns.Count)
        
    Set tranDict = CreateObject("Scripting.dictionary")
    
    For i = 2 To UBound(tranArr)
        ' --- Create dictionary Key from multiple columns ---
        dictKey = ""
        For k = 2 To UBound(tranArr, 2) - 2
            dictKey = dictKey & "|" & tranArr(i, k)
        Next k
        
        ' --- Add Sign to Value ---
        ActionName = tranArr(i, 17)
        tranValAdd = tranArr(i, 18)
        'Action Names at time of writing
        'Opening Stock|Issue to Production|Receive From Supplier|Return from Production|Return to Supplier
        Select Case ActionName
            Case "Issue to Production", "Return to Supplier"
                ' Stock Out
                tranValAdd = tranValAdd * -1
            Case Else
                ' Stock In
                ' Sign unchanged - positive
        End Select
        
        ' --- Load Tran Sheet dictionary and Load Output Array Summing Qty ---
        If Not tranDict.exists(dictKey) Then
            j = j + 1
            tranDict(dictKey) = j
            For z = 1 To UBound(outArr, 2) - 1
                outArr(j, z) = tranArr(i, z + 1)
            Next z
            outArr(j, UBound(outArr, 2)) = tranValAdd
        Else
            outArr(tranDict(dictKey), UBound(outArr, 2)) = outArr(tranDict(dictKey), UBound(outArr, 2)) + tranValAdd
        End If
    Next i
    
    With stockSht
        .Range("A" & stockLastRow + 1).Resize(j, UBound(outArr, 2)) = outArr
    End With

End Sub
 
Upvote 0
Hey Alex,
Sorry for the late reply I was sick. I think I failed to explain properly.
Actually, it has nothing to do with the actions. Actions are there for another calculation.

What I am doing is actually creating a pivot table without using the pivot table function. The first sheet is for transaction entry where one item may be entered multiple times for various actions.
On the second sheet, I want only a single row for one item if everything matches the same. If the item is on a different rack location or has a different attribute, it will be in a separate row.
For this, my code works just fine. I just want to exclude "E:I" columns from the range while pulling the data from the first sheet.

Can you please look at my code and tell me what I'm doing wrong?


VBA Code:
Sub RemoveDuplicates()
  
   Dim rngDatabase1 As Range
   Dim rngDatabase2 As Range
   Dim uRng As Range
'define the database
   Set rngDatabase1 = Sheets("Transaction Entry").Range("B5:D1048576")
   Set rngDatabase2 = Sheets("Transaction Entry").Range("I5:P1048576")
   Set uRng = Sheets("Transaction Entry").Range(rngDatabase1, rngDatabase2)
'filter the database to a new range with a unique set to true
uRng.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Rack-Wise Stock Report").Range("A5:O5"), Unique:=True
End Sub
 
Upvote 0
I'm afraid your code doesn't seem to make sense.
In your uRng you have 11 columns but your output is 15 columns.
The columns you are leaving out are also in your output sheet.
I don't believe you can use a non-contiguous range of columns in an Advanced Filter.

Did you try the code I gave you ?
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,670
Members
452,993
Latest member
FDARYABEE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top