How to move cell to LEFT?

Rampage598

New Member
Joined
Mar 11, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Currently using this to move one cell to right:
ActiveCell.Offset(1, 0)

I am not getting how to move one cell to left....
 
See if this does what you want (notice no "ActiveCell" or "Select" in the code):
VBA Code:
Sub test2()

Dim rng As Range
Dim lastCol As String
Dim lastRow As Long

Sheets("FinalData").Select

'Set range variable to last column in row 2 with data
Set rng = Cells(2, Columns.Count).End(xlToLeft)

'Find last column letter of this cell in row 2
lastCol = Split(rng.Address, "$")(1)

'Find last row in column A with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Populate new headers in row 2
rng.Offset(0, 1) = "Running"
rng.Offset(0, 2) = "Cycling"
rng.Offset(0, 3) = "Strength(Mins)"

'Populate formulas into row 2
rng.Offset(1, 1).Formula = "=Sumif($B$2:" & lastCol & "$2,""*Running*"",B3:" & lastCol & "3)"
rng.Offset(1, 2).Formula = "=Sumif($B$2:" & lastCol & "$2,""*Cycling*"",B3:" & lastCol & "3)"
rng.Offset(1, 3).Formula = "=Sumif($B$2:" & lastCol & "$2,""*Strength(Mins)*"",B3:" & lastCol & "3)"

'Copy down for all rows
Range(rng.Offset(1, 1), rng.Offset(1, 3)).Copy Range(rng.Offset(1, 1), rng.Offset(lastRow - 2, 1))

End Sub
 
Upvote 0
Solution

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
See if this does what you want (notice no "ActiveCell" or "Select" in the code):
VBA Code:
Sub test2()

Dim rng As Range
Dim lastCol As String
Dim lastRow As Long

Sheets("FinalData").Select

'Set range variable to last column in row 2 with data
Set rng = Cells(2, Columns.Count).End(xlToLeft)

'Find last column letter of this cell in row 2
lastCol = Split(rng.Address, "$")(1)

'Find last row in column A with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Populate new headers in row 2
rng.Offset(0, 1) = "Running"
rng.Offset(0, 2) = "Cycling"
rng.Offset(0, 3) = "Strength(Mins)"

'Populate formulas into row 2
rng.Offset(1, 1).Formula = "=Sumif($B$2:" & lastCol & "$2,""*Running*"",B3:" & lastCol & "3)"
rng.Offset(1, 2).Formula = "=Sumif($B$2:" & lastCol & "$2,""*Cycling*"",B3:" & lastCol & "3)"
rng.Offset(1, 3).Formula = "=Sumif($B$2:" & lastCol & "$2,""*Strength(Mins)*"",B3:" & lastCol & "3)"

'Copy down for all rows
Range(rng.Offset(1, 1), rng.Offset(1, 3)).Copy Range(rng.Offset(1, 1), rng.Offset(lastRow - 2, 1))

End Sub
Thanks for your help :) its working properly
 
Upvote 0
You are welcome!

I tried to document my code to explain what I am doing in each section.
Let me know if you have any questions about any of the code.
 
Upvote 0
You are welcome!

I tried to document my code to explain what I am doing in each section.
Let me know if you have any questions about any of the code.
Can you tell me how to add "Day wise total" on above Cycling header?
1647021664930.png

Like this
 
Upvote 0
Add this to the header section of code:
VBA Code:
rng.Offset(-1, 2) = "Day wise total"

Basically, we have hard-coded the "rng" range variable to be the last column in row 2 with data (your last header), and then just working from there using Offset (always starting out from that cell).
 
Upvote 0
Add this to the header section of code:
VBA Code:
rng.Offset(-1, 2) = "Day wise total"

Basically, we have hard-coded the "rng" range variable to be the last column in row 2 with data (your last header), and then just working from there using Offset (always starting out from that cell).
Once Again Thankyou
 
Upvote 0

Forum statistics

Threads
1,216,089
Messages
6,128,750
Members
449,466
Latest member
Peter Juhnke

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