Macro to extract cetain coulmns and then sort data

EleKTron

New Member
Joined
Feb 1, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello MrExcel community
Hoping to find help here as i am struggling a bit wuth building a macro using VBA

My requirement is to select few particular colums and extract to a particular named sheet and then sort it according to criteria of one particular column
the problem i am currently facing is when i am running a main module with multiple sub modules it doesnt run them all,

Coulmn A - Text
Column B - Numbers
Column F - Text


So initial sheet is -
1643804158040.png


intermediate result should be extract of only highlighted columns A B F in seperate sheet -
1643804240543.png


and finally a sort as based on column F -
1643804521071.png


VBA Code

Sub FindAColumn()
'trial
Dim xRg As Range
Dim xRgUni As Range
Dim xFirstAddress As String
Dim xStr As String

On Error Resume Next

xStr = "A"
Set xRg = Range("A1:P1").Find(xStr, , xlValues, xlPart, , , True)
If Not xRg Is Nothing Then
xFirstAddress = xRg.Address
Do
If xRgUni Is Nothing Then
Set xRgUni = Range(xFirstAddress)
Else
Set xRgUni = Application.Union(xRgUni, xRg)
End If

Set xRg = Range("A1:P1").FindNext(xRg)

Loop While (Not xRg Is Nothing) And (xRg.Address <> xFirstAddress)
End If
xRgUni.EntireColumn.Select

Selection.Copy

Findrightsheet

Set destination = Sheets("Dataformatted")
'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column + 1

If destination.Cells(1, 1) = "" And emptyColumn = 2 Then emptyColumn = 1

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

End Sub

But this does not work after the 1st column if i have a seperate module with same code replacing with coulmn B and C,

Please help !
thanks!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
i would like to sort column B but the data should be ordered accordingly
i tried the record macro option but i am unable to adapt it to my sheet

Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add2 Key:=Range("B2:B13" _
), SortOn:=xlSortOnValues, order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet11").Sort
.SetRange Range("A1:C13")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

i want to use this function but with 2 adaptations

1. the data should not take the 1st row into account as it is header
2. the data from column B should be sorted from B2 to the end (this last value may vary on file to file basis hence cannot specify the range )

This needs to be achieved because the final result must be as howen below

and range of values if Column B based on column F -
1643804521071.png



Could someone please help me with this?

thanks!
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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