Loop range keep same value format

christian2016

Board Regular
Joined
Oct 6, 2016
Messages
123
Hi Guys,

Is there anyway to keep the same value format when looping a range.

Example - cell.value is a date or a percentage, when writing the cell.value to a particular cell it retains the same format.

I know there is a way to do this via copy and paste but there are lots of cell values i need to use in if statements and to write to cells and dont want to copy and paste all the time.

Any help is greatly appreciated.

Thanks
 

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
Well you can do it like this. Using copy and paste is not required. Paste is assumed.
VBA Code:
Sub My_Cell_Value()
'Modified 12/16/2021  5:23:00 AM  EST
Dim i As Long

    For i = 1 To 26
        Cells(i, 1).Copy Cells(i, 2)
    Next

End Sub

If you would show us some of your code we may be able to use Case
Instead of a lot of if statements.
 
Upvote 0
Here is a example of searching for dates in column A and if found copies column A Value and formatting to column B
Using Case not IF
VBA Code:
Sub My_Cell_Value()
'Modified 12/16/2021  5:50:00 AM  EST
Dim i As Long
    For i = 1 To 26
        Select Case Cells(i, 1)
            Case "1/1/2021", "1/5/2021", "1/8/2021"
                Cells(i, 1).Copy Cells(i, 2)
            Case "1/15/2021", "1/18/2021", "1/21/2021"
                Cells(i, 1).Copy Cells(i, 2)
        End Select
    Next
End Sub
 
Upvote 0
Solution
Here is a example of searching for dates in column A and if found copies column A Value and formatting to column B
Using Case not IF
VBA Code:
Sub My_Cell_Value()
'Modified 12/16/2021  5:50:00 AM  EST
Dim i As Long
    For i = 1 To 26
        Select Case Cells(i, 1)
            Case "1/1/2021", "1/5/2021", "1/8/2021"
                Cells(i, 1).Copy Cells(i, 2)
            Case "1/15/2021", "1/18/2021", "1/21/2021"
                Cells(i, 1).Copy Cells(i, 2)
        End Select
    Next
End Sub
Thank you so much. Didn't know about select cases rather than if statements. I know how they work now. Will incorporate within my vba code.
 
Upvote 0
For a range of dates:
Try this using Case:
Case "1/15/2021" To "1/21/2021"
This would be from "1/15/2021" To "1/21/2021
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,517
Members
448,968
Latest member
Ajax40

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