Need help in writing a VBA code

imereysoriano

New Member
Joined
Jun 23, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
C​
D​
E​
F​
G​
Session CodeDemoPracticalLectureSeminar
D0133
D0233

In the example table above, I would like to write a VBA code to list the Session Code + Lesson Type like this

D01 Demo
D01 Practical
D02 Lecture
D02 Seminar

I was able to look for a code but it just gives me the cell address that matches a criteria. Here's the code

Public Sub SearchForText()
Dim rngSearchRange As Range
Dim vntTextToFind As Variant
Dim strFirstAddr As String
Dim lngMatches As Long
Dim rngFound As Range

On Error GoTo ErrHandler
vntTextToFind = Application.InputBox( _
Prompt:="Enter text to find:", _
Default:="Search...", _
Type:=2 _
)
If VarType(vntTextToFind) = vbBoolean Then Exit Sub

On Error Resume Next
Set rngSearchRange = Application.InputBox( _
Prompt:="Enter range for search:", _
Default:=ActiveCell.Parent.UsedRange.Address, _
Type:=8 _
)

On Error GoTo ErrHandler
If rngSearchRange Is Nothing Then Exit Sub
Set rngFound = rngSearchRange.Find( _
What:=CStr(vntTextToFind), _
LookIn:=xlValues, _
LookAt:=xlPart _
)

If rngFound Is Nothing Then
MsgBox "No matches were found.", vbInformation
Else
With ThisWorkbook.Sheets.Add
With .Range("A1:B1")
.Value = Array("Cell", "Value")
.Font.Bold = True
End With
strFirstAddr = rngFound.Address
Do
lngMatches = lngMatches + 1
.Cells(lngMatches + 1, "A").Value = rngFound.Parent.Name & "!" _
& rngFound.Address(0, 0)
.Cells(lngMatches + 1, "B").Value = rngFound.Value
Set rngFound = rngSearchRange.FindNext(rngFound)
Loop Until (rngFound.Address = strFirstAddr)
.Columns("A:B").AutoFit
End With
End If
Exit Sub

ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub

Would really appreciate your help. Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
PQ can do it for you easily

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Session Code", type text}, {"Demo", Int64.Type}, {"Practical", Int64.Type}, {"Lecture", Int64.Type}, {"Seminar", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Session Code"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Value"})
in
    #"Removed Columns"

Book1
ABCDE
1Session CodeDemoPracticalLectureSeminar
2D0133
3D0233
4
5
6Session CodeAttribute
7D01Demo
8D01Practical
9D02Lecture
10D02Seminar
Sheet1
 
Upvote 0
PQ can do it for you easily

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Session Code", type text}, {"Demo", Int64.Type}, {"Practical", Int64.Type}, {"Lecture", Int64.Type}, {"Seminar", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Session Code"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Value"})
in
    #"Removed Columns"

Book1
ABCDE
1Session CodeDemoPracticalLectureSeminar
2D0133
3D0233
4
5
6Session CodeAttribute
7D01Demo
8D01Practical
9D02Lecture
10D02Seminar
Sheet1
Hello. Thanks for the response.

Sorry. I am not familiar with power query. I am currently using just Excel.
How can I convert my file to PQ if I may ask?
 
Upvote 0
Power Query is part of Excel and very powerful for handling large data set transformations / sorting

1687517978664.png


Select the range of the data you are importing, paste into Advanced editor the code above and load back into your excel sheet . Loads of resources on how to use it available and walkthroughs
 
Upvote 0
Power Query is part of Excel and very powerful for handling large data set transformations / sorting

View attachment 94123

Select the range of the data you are importing, paste into Advanced editor the code above and load back into your excel sheet . Loads of resources on how to use it available and walkthroughs
Got this. This is very helpful.

Is there a way I can add a macro to have this done?
 
Upvote 0
To do what? Once you have this set up and if the data changes in the worksheet , if the range has not changed just refresh, if the range has changed just amend the range and rerun. You don't need a macro.
PQ Step-by-step
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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