Using ActiveCell to Enter Data, Sort, and Move Cells.

mlarson

Well-known Member
Joined
Aug 25, 2011
Messages
509
Office Version
  1. 2010
Platform
  1. Windows
Hi all! I have a question I hope you can help me with. Thanks in advance for your help!

I would like a code that can do all of this in one Run… I’d like it to use ActiveCell (and not a specific address like A2 or B4). So, when I run the macro, it starts at the active cell and enters ten numbers vertically down staring with 1 in the active cell, then 3 in the row beneath it, 5 in the next row, then in subsequent rows 7, 9, 2, 4, 6, 8, and 10.

Then sort those 10 rows that we just entered numbers in (in Column A) but also include column B and C in that sort. So, for example, if the ActiveCell is A5, then the range would be A5:C14.

Then move to the right one cell the words in Column C (within the range) that are in same row as the numbers 2, 4, 6, 8, and 10 in Column A, so that the words in those rows of Column C go to Column D.

Finally, make those in Column B within that range (not the entire column B) unbold, and make those in Column C within that range bold.

I hope I explained this clearly. Again, thanks for your help!

1676144312277.png
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
With A6 being the activecell the code below turns
Book1.xlsb
ABCD
4
5
6A
7B
8C
9D
10E
11F
12G
13H
14I
15J
Sheet1


into

Book1.xlsb
ABCDE
4
5
61A
73B
85C
97D
109E
112F
124G
136H
148I
1510J
Sheet1


Then

Book1.xlsb
ABCDE
4
5
61A
72F
83B
94G
105C
116H
127D
138I
149E
1510J
Sheet1


VBA Code:
Sub mlarson()
    Dim SrtRng As Range, i As Integer

    Application.ScreenUpdating = False

    ActiveCell.Resize(10) = Application.Transpose(Array(1, 3, 5, 7, 9, 2, 4, 6, 8, 10))
    Set SrtRng = ActiveCell.Resize(10, 3)

    SrtRng.Sort Key1:=SrtRng.Cells(1, 1), Order1:=xlAscending, Header:=xlNo

    For i = 2 To 10 Step 2
        SrtRng(i, 4).Value = SrtRng(i, 3).Value
        SrtRng(i, 3).ClearContents
    Next

    Application.ScreenUpdating = True

End Sub

P.S. please can you use XL2BB to post screenshots in future as I (like many) don't want to have to retype your data
 
Upvote 0
P.S. please can you use XL2BB to post screenshots in future as I (like many) don't want to have to retype your data

Ugh, sorry about that and apologies for that oversight on my part.

And thank you for the code, works very well! Is there something I could add to it to make the words that end up in Column D within the range (not the entire column) to be bold? And can I make sure the words in Column C within the range are not bold?
 
Upvote 0
Rich (BB code):
Sub mlarson2()
    Dim SrtRng As Range, i As Integer

    Application.ScreenUpdating = False

    ActiveCell.Resize(10) = Application.Transpose(Array(1, 3, 5, 7, 9, 2, 4, 6, 8, 10))
    Set SrtRng = ActiveCell.Resize(10, 3)

    SrtRng.Sort Key1:=SrtRng.Cells(1, 1), Order1:=xlAscending, Header:=xlNo

    For i = 2 To 10 Step 2
        SrtRng(i, 4).Value = SrtRng(i, 3).Value
        SrtRng(i, 3).ClearContents
    Next
    
    SrtRng(1, 4).Resize(10).Font.Bold = True
    SrtRng(1, 3).Resize(10).Font.Bold = False
    

    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
This is what I came up with:

VBA Code:
Sub Test()
'
    Dim DataRange       As Range
    Dim NumberArray     As Variant
'
    NumberArray = Array(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
    Set DataRange = Selection.Resize(UBound(NumberArray) + 1, 4)
'
    Selection.Resize(UBound(NumberArray) + 1) = Application.Transpose(NumberArray)
'
    With Range(Selection.Offset((UBound(NumberArray) + 1) / 2, 2).Address & ":" & Selection.Offset(UBound(NumberArray), 2).Address)
        .Font.FontStyle = "Bold"
        .Insert Shift:=xlToRight
    End With
'
    DataRange.Sort Key1:=Range(Selection.Address), Order1:=xlAscending, Header:=xlNo
End Sub
 
Upvote 0
You're welcome.
The code posted by @johnnyL should be faster if you are dealing with much larger data sets than you have posted. It just needs to cater for the last condition that you have added
And can I make sure the words in Column C within the range are not bold?
 
Upvote 0
You're welcome.
The code posted by @johnnyL should be faster if you are dealing with much larger data sets than you have posted. It just needs to cater for the last condition that you have added

VBA Code:
Sub Test2()
'
    Dim NumberArray     As Variant
'
    NumberArray = Array(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
'
    Selection.Resize(UBound(NumberArray) + 1) = Application.Transpose(NumberArray)
'
    With Range(Selection.Offset((UBound(NumberArray) + 1) / 2, 2).Address & ":" & Selection.Offset(UBound(NumberArray), 2).Address)
        .Font.FontStyle = "Bold"
        .Insert Shift:=xlToRight
    End With
'
    Selection.Resize(UBound(NumberArray) + 1, 4).Sort Key1:=Range(Selection.Address), Order1:=xlAscending, Header:=xlNo
    Selection.Resize(UBound(NumberArray) + 1, 3).Font.FontStyle = "Regular"
End Sub

Better?
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,841
Members
449,051
Latest member
excelquestion515

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