If cell is blank, copy adjacent cell into blank cell (VBA)

Mindtriks

New Member
Joined
Apr 11, 2016
Messages
14
Hi - So I am currently trying to work on some code that will help with the following:

In Column G, I have a bunch of different data points, however some of the cells are blank. If a cell is blank I want it to look at the cell in the adjacent column (Column F) and copy the data from that cell into the empty cell in Column G. I am still new to VBA and any help is greatly appreciated.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
In Column G, I have a bunch of different data points

Mindtriks,

1. What is the raw data worksheet name?

2. Are there titles in row 1?

3. Are there formulas in column G?

4. Can we see examples of what is in column G?

5. Can we see examples of what is in column F?
 
Last edited:
Upvote 0
Hiker - See Below

1. It is sheet 1, but I will just be using worksheet.activesheet
2. Data starts on row 2, headers are on row 1.
3. No formulas in Column G.
4. Column G is just a list of names.
5. Column F is also a list of names.

Column G and F are populated with names, however when the data is pulled from our database these names can be affiliated with 1 or more different fields. For example, Column G and Column F, both have a name tied to a unique data set, but I need to consolidate those 2 columns.
 
Upvote 0
So I am currently trying to work on some code

Mindtriks,

Can we see all of your current macro code?

When posting VBA code, please use Code Tags - like this:

[code=rich]

'Paste your code here.

[/code]
 
Upvote 0
Re: If cell is blank, copy adjacent cell into blank cell (VBA)
Hiker - See Below

1. It is sheet 1, but I will just be using worksheet.activesheet
2. Data starts on row 2, headers are on row 1.
3. No formulas in Column G.
4. Column G is just a list of names.
5. Column F is also a list of names.

Column G and F are populated with names, however when the data is pulled from our database these names can be affiliated with 1 or more different fields. For example, Column G and Column F, both have a name tied to a unique data set, but I need to consolidate those 2 columns.


Mindtriks,

So that I can get it right on the first try, I would like to see your raw data workbook/worksheet Sheet1.


You can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

Dropbox
 
Last edited:
Upvote 0
Try this:
Code:
Sub Test()
Application.ScreenUpdating = False
Dim c  As Range
    For Each c In Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row)
        If c.Value = "" Then c.Value = c.Offset(, -1).Value
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mindtriks,

Here is a macro solution for you to consider that does not do any looping thru the rows in the active worksheet in column G, and, should be very fast.

Code:
Sub Mindtriks()
' hiker95, 08/22/2017, ME1020002
Application.ScreenUpdating = False
Dim lr As Long
With ActiveSheet
  lr = .Columns("F:G").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
  On Error Resume Next
  With .Range("G2:G" & lr)
    .SpecialCells(xlCellTypeBlanks).Formula = "=RC[-1]"
    .Value = .Value
  End With
  On Error GoTo 0
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:
Code:
Sub Test()
Application.ScreenUpdating = False
Dim c  As Range
    For Each c In Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row)
        If c.Value = "" Then c.Value = c.Offset(, -1).Value
    Next
Application.ScreenUpdating = True
End Sub
I used the above code for mine and it works magic :) Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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