Help with VBA - Changing column headers?

nerdygirl1225

New Member
Joined
Jul 27, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
So I am trying to use this macro. Basically I want to sort column W ascending, then use the formula "=W2*-1" in column X, delete all values in column X that equal zero and W is blank, then copy and paste the values from column X to column I. When I do this it seems to work but it is changing the column header instead of starting in W2, X2, and I2... Can anyone help?

' Sort column W from smallest to largest
Range("$W$1:$W$" & Range("W" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("$W$2"), Order1:=xlAscending

' Add the formula "=W2*-1" to column X
Range("X2").Formula = "=W2*-1"
Range("X2").AutoFill Destination:=Range("X2:X" & Range("W" & Rows.Count).End(xlUp).Row)

' Delete values in column X where column Y is blank and X = 0
For a1 = 2 To Range("W" & Rows.Count).End(xlUp).Row
If IsEmpty(Range("W2")) And Range("X2") = 0 Then
Range("X" & a1).ClearContents
End If
Next a1

' Copy and paste values from column X to column I starting with X2
Range("X2:X" & Range("W" & Rows.Count).End(xlUp).Row).Copy
Range("I2").PasteSpecial xlPasteValues
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,

To follow your thought process ... you can test following
VBA Code:
Sub TestNerdy()
Dim i As Long

' Sort column W from smallest to largest
Range("$W$2:$W$" & Range("W" & Rows.Count).End(xlUp).Row).Sort Key1:=Range("$W$2"), Order1:=xlAscending

' Add the formula "=W2*-1" to column X
Range("X2").Formula = "=W2*-1"
Range("X2").AutoFill Destination:=Range("X2:X" & Range("W" & Rows.Count).End(xlUp).Row)

' Your Sorting has placed all Blanks at the bottom
' and these cells are no longer taken into account

' Copy and paste values from column X to column I starting with X2
Range("I2:I" & Range("W" & Rows.Count).End(xlUp).Row).Value = Range("X2:X" & Range("W" & Rows.Count).End(xlUp).Row).Value

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,983
Members
449,276
Latest member
surendra75

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