Help with VBA code if cell is blank in column C put in formula, then convert to value

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
888
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am looking to clean up some data in another Macro I have and am trying to figure out the code for one part of the clean up.
Basically in column C there are ship dates and in column B there are due dates. When there is a blank cell in column C I want it to bring over the value from column B.

The below code I have is giving me an error so I am missing something...

VBA Code:
Dim rCell As Range

For Each rCell In Range("C2:C")

    If rCell.Value = "" Then

        rCell.Formula = "=[@[Due Date]]"
        rCell.Value = rCell.Value
        
    End If
    
Next rCell

Any help would be appreciated.

Thank you! :)
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I have not looked at your code in any detail, but the following caught my eye right away. This line of code...

For Each rCell In Range("C2:C")

has no row number after the second C... you cannot have an open-ended range like that. By the way for future reference, it is always a good idea to tell us the error number and its description and the line of code the program stopped on so we don't have to guess what or where your problem is.
 
Upvote 0
You need to specify the last row of data on this line
VBA Code:
For Each rCell In Range("C2:C")
 
Upvote 0
You need to specify the last row of data on this line
VBA Code:
For Each rCell In Range("C2:C")
the last row of data could vary. Is there a line I could write that would determine which is the last row of data to apply this to?
 
Upvote 0
Do you have a column that is guaranteed to have the last row of data in it?
 
Upvote 0
As it appears that your data is in a table, what is the name of the table & what is the name of the column where you want the values
 
Upvote 0
As it appears that your data is in a table, what is the name of the table & what is the name of the column where you want the values
The name of the table is "SR" and
column C is: "Ship Date"
Column B is: "Due Date"
Column G is: "Part Number"

Thank you very much!
 
Upvote 0
In that case use
VBA Code:
For Each rCell In ActiveSheet.ListObjects("SR").ListColumns("Ship Date").DataBodyRange
 
Upvote 0
Solution
In that case use
VBA Code:
For Each rCell In ActiveSheet.ListObjects("SR").ListColumns("Ship Date").DataBodyRange
I tried the below code but it seems to freeze/take an extremely long time. My data currently has 4925 rows. Is there maybe a better way of doing this?
I was unable to test it before as I was getting an error but as is now this part will definitely slow down the macro...

VBA Code:
Dim rCell As Range

For Each rCell In ActiveSheet.ListObjects("SR").ListColumns("Ship Date").DataBodyRange

    If rCell.Value = "" Then

        rCell.Formula = "=[@[Due Date]]"
        rCell.Value = rCell.Value
        
    End If
    
Next rCell
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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