Targeting a specific Column with VBA.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I'm sure I have hidden values within a cell. I have this code I used elsewhere in my project, but if I use this one too early as it gives me another problem down the line. How Can I just target Column C and only C? Do I just simply replace "A1" with "C1" and "X" with "C"?


VBA Code:
Sub remove_hidden_Values()
With ActiveSheet
For Each Cell In Range("A1", Range("X" & Rows.Count).End(xlUp))
        Cell.Value = Trim(Replace(Cell.Value, Chr(160), Chr(32)))
    Next
    End With
End Sub
 

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
Thank you, I was trying to add just one other Column I. I tried using the code below, but obviously I'm wrong. I'll continue to search.
Sub remove_hidden_Values_ColumnnsA_I()
With ActiveSheet
For Each Cell In Range("C1","I1" Range("C"),("I") & Rows.Count).End(xlUp))
Cell.Value = Trim(Replace(Cell.Value, Chr(160), Chr(32)))
Next
End With
End Sub
 
Upvote 0
For Each Cell In Range("C1","I1" Range("C"),("I") & Rows.Count).End(xlUp))
You cannot do that for a non-contiguous range (two columns that aren't touching).

You would either need to do two separate loops, or use "UNION" to create a non-contiguous range, something like this:
VBA Code:
    Set rng = Union(Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row), Range("I1:I" & Cells(Rows.Count, "I").End(xlUp).Row))
    For Each cell In rng
        ...
 
Upvote 0
Solution
You cannot do that for a non-contiguous range (two columns that aren't touching).

You would either need to do two separate loops, or use "UNION" to create a non-contiguous range, something like this:
VBA Code:
    Set rng = Union(Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row), Range("I1:I" & Cells(Rows.Count, "I").End(xlUp).Row))
    For Each cell In rng
        ...
Thank you for the advice. I decided to have separate codes since there was enough activity between certain function.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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