Merge code

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
Hi
Is it possible to put the code into 1 line like my first line is? When I tried this, only the first cell filled in.


So if any one of these cells is blank, "Spare" will fill in.

VBA Code:
 If Range("C5,E5,G5,I5,K5,M5") = "" Then Range("C5,E5,G5,I5,K5,M5") = "Spare"

 If Range("C5") = "" Then Range("C5") = "Spare"
    If Range("E5") = "" Then Range("E5") = "Spare"
    If Range("G5") = "" Then Range("G5") = "Spare"
    If Range("I5") = "" Then Range("I5") = "Spare"
     If Range("K5") = "" Then Range("K5") = "Spare"
    If Range("M5") = "" Then Range("M5") = "Spare"
/CODE]

TIA
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You can't do it that way, but there are other ways to do what you want.

VBA Code:
    Dim R As Range, S As String

    For Each R In Range("C5,E5,G5,I5,K5,M5")
        S = S & Trim(R.Text)
    Next R
    If S = "" Then
        Range("C5,E5,G5,I5,K5,M5") = "Spare"
    End If
 
Upvote 0
If the intent is just to shorten the code while filling any blanks in the non-contiguous range with the string "Spare", maybe:
VBA Code:
Sub Test()
Dim Rng As Range, c as Range
Set Rng = Range("C5,E5,G5,I5,K5,M5")
If Application.CountA(Rng) < Rng.Count Then
    For Each c In Rng
        If c.Value = "" Then c.Value = "Spare"
    Next c
End If
End Sub
 
Upvote 0
Thank you all!
If I put in 3000 cell addresses, will it slow my sheet down?
 
Upvote 0
You can't do it that way, but there are other ways to do what you want.

VBA Code:
    Dim R As Range, S As String

    For Each R In Range("C5,E5,G5,I5,K5,M5")
        S = S & Trim(R.Text)
    Next R
    If S = "" Then
        Range("C5,E5,G5,I5,K5,M5") = "Spare"
    End If
How do you merge code?
 
Upvote 0
Thank you all!
If I put in 3000 cell addresses, will it slow my sheet down?
Of course. But the question you should be asking is will it slow down enough to matter? The way to answer that question is to conduct a test.
 
Upvote 0
If you put in 3000 addresses, it won't work, since the Range property only accepts a 255 character address string. You'll have to use Union. Although it looks like you are simply doing every other column, so you could use a simple loop with step 2 over the whole range.
 
Upvote 0
If you put in 3000 addresses, it won't work, since the Range property only accepts a 255 character address string. You'll have to use Union. Although it looks like you are simply doing every other column, so you could use a simple loop with step 2 over the whole range.
Got it! Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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