Excel VB Macro - Shift left where blank cells

Read_This

New Member
Joined
Aug 13, 2014
Messages
37
Is there any way in a Excel VB macro where in column A there is data in most cells and, in the cells that are blank the data is in column B to delete the blank column A Cells, which in turn will shift left the data from column B cells – Ending up with all the data in column A and no blank cells. Ready for the VLOOKUP function.

For example:


before
col A

col B
apples
pears
oranges
bananas
grapes
mangos
strawberries


after
col A

col B
apples
pears
oranges
bananas
grapes
mangos
strawberries

<tbody>
</tbody>
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Read_This,

Sample raw data:


Excel 2007
AB
1apples
2pears
3oranges
4bananas
5grapes
6mangos
7strawberries
8
Sheet1


After the macro:


Excel 2007
AB
1apples
2pears
3oranges
4bananas
5grapes
6mangos
7strawberries
8
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ColumnsAB_ShiftfLeft()
' hiker95, 11/26/2014, ME820727
Dim lr As Long
lr = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
On Error Resume Next
Range("A1:B" & lr).SpecialCells(xlBlanks).Delete shift:=xlToLeft
On Error GoTo 0
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ColumnsAB_ShiftfLeft macro.
 
Upvote 0
Read_This,

Ready for the VLOOKUP function.

Just in case you had data to the right of column B that you did not want to move left, try the following:


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ColumnsAB_ShiftfLeft_V2()
' hiker95, 11/26/2014, ME820727
Dim a As Variant, i As Long
Dim lr As Long
lr = Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
a = Range("A1:B" & lr)
For i = 1 To lr
  If a(i, 1) = "" Then
    a(i, 1) = a(i, 2)
    a(i, 2) = ""
  End If
Next i
Range("A1:B" & lr).ClearContents
Range("A1").Resize(lr, 2).Value = a
End Sub

Then run the ColumnsAB_ShiftfLeft_V2 macro.
 
Last edited:
Upvote 0
Read_This,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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