VBA - copy cell if it is not empty

Kristina96

New Member
Joined
Sep 30, 2019
Messages
33
Hi everyone,

I have the following problem:

I have a small table with 2 columns (E and F):
I want to copy all values from column F into column E but only if there is a value in column F. I cannot use the "normal" copy function as that would copy empty cells. There are values in column E that are supposed to be kept if there is no value in column F.
All of this should be done with VBA as it is part of a macro.
I am very thankful for every idea or help!
Thank you and regards
Kristina
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello Kristina,

As a starting point, see if the following does the task for you:-


Code:
Sub Test()

    Dim c As Range
    Dim lr As Long: lr = Sheet1.Range("E" & Rows.Count).End(xlUp).Row

For Each c In Range("F1:F" & lr)
      If Not IsEmpty(c) Then
      c.Offset(, -1).Value = c.Value
      End If
Next c

End Sub

Test it separately in a copy of your workbook first.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Another way:

Code:
    With Range("F1", Cells(Rows.count, "F").End(xlUp))
    .Copy
    .Offset(, -1).PasteSpecial paste:=xlPasteValues, [COLOR=#0000ff]SkipBlanks:=True[/COLOR]
    End With
 
Upvote 0
Thank you for your help!
I tried this because I only want to look as rows 31 to 34 in the columns:
Code:
Dim c As Range
Dim lr As Long: lr = Worksheets("Sheet1").Range("E31:E34" & Rows.Count).End(xlUp).Row


For Each c In Range("F31:F34" & lr)
      If Not IsEmpty(c) Then
      c.Offset(, -1).Value = c.Value
      End If
Next c

I get an error in the second line when running the macro. Do you know what could be the problem?
 
Upvote 0
You forgot to remove the row number 34 after the E (Rows.Count returns the maximum row number, so you want to remove the row number reference of the range you are attaching that to).
So change it here:
Code:
Dim lr As Long: lr = Worksheets("Sheet1").Range([COLOR=#ff0000]"E31:E" & Rows.Count[/COLOR]).End(xlUp).Row
and here:
Code:
For Each c In Range([COLOR=#ff0000]"F31:F" & lr[/COLOR])
 
Last edited:
Upvote 0
I don't get an error anymore but it is not copying the cells. There are more rows in columns E and F below that should remain unchanged aswell. How can I solve this?
Would it be easier to change akuinis code and let it stop in row 34?
 
Last edited:
Upvote 0
OK, I was only commenting on the issue with your code, but did not analzye it or the original question.
If you just want to focus on a specific range, you should not need to find the last row.

So, does this simplified version do what you want?
Code:
Dim c As Range

For Each c In Range("F31:F34")
      If Not IsEmpty(c) Then
      c.Offset(, -1).Value = c.Value
      End If
Next c
 
Upvote 0
Try:
Code:
    With Range("F31:F34")
    .Copy
    .Offset(, -1).PasteSpecial paste:=xlPasteValues, SkipBlanks:=True
    End With
 
Upvote 0
That makes sense but it is still not copying the values. The macro is running without error but nothing changes...
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,242
Members
448,951
Latest member
jennlynn

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