Problem setting Range.Value with a Filter

ou81aswell

Board Regular
Joined
Oct 6, 2009
Messages
50
I'm having a problem setting a Range.Value on a worksheet that has a Filter.

Here's an example worksheet (Sorry about the wonky spacing. I tried my best!)

<font size="2">Worksheet: Sheet1 UsedRange: $A$1:$B$7 Range: $A$1:$B$7</font><table border="1" cellspacing="2" cellpadding="3">
<tr>
<td width="40px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2"> </font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">A</font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">B</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA1_705"></a><font size="2">TYPE</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB1_705"></a><font size="2">VALUE</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA2_705"></a><font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB2_705"></a><font size="2">a</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA3_705"></a><font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB3_705"></a><font size="2">b</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">4</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA4_705"></a><font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB4_705"></a><font size="2">c</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">5</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA5_705"></a><font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB5_705"></a><font size="2">d</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">6</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA6_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB6_705"></a><font size="2">e</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">7</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA7_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB7_705"></a><font size="2">f</font>
</td>
</tr>
</table>
Here is the worksheet filtered by TYPE = 3

<table border="1" cellspacing="2" cellpadding="3">
<tr>
<td width="40px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2"> </font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">A</font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">B</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA1_705"></a><font size="2">TYPE</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB1_705"></a><font size="2">VALUE</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">6</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA6_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB6_705"></a><font size="2">e</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">7</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA7_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB7_705"></a><font size="2">f</font>
</td>
</tr>
</table><p />

If I run this macro with the filter applied I get unexpected results.

This code simply forces the text in the cells in column B to upper case.

Code:
Sub Test()

Dim oRange As Excel.Range
Set oRange = Application.Range("B2:B7")

Dim Cells
Cells = oRange.Value

Dim nRows
nRows = oRange.Rows.Count

Dim r
For r = 1 To nRows
    Cells(r, 1) = UCase(Cells(r, 1))
Next r

oRange.Value = Cells

End Sub

I end up with the following sheet (auto filter is still on). I would have expected to see E and F instead of A and B:

<table border="1" cellspacing="2" cellpadding="3">
<tr>
<td width="40px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2"> </font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">A</font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">B</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA1_705"></a><font size="2">TYPE</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB1_705"></a><font size="2">VALUE</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">6</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA6_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB6_705"></a><font size="2">A</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">7</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA7_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB7_705"></a><font size="2">B</font>
</td>
</tr>
</table><p />

Here is the resulting sheet with autofilter off:

<table border="1" cellspacing="2" cellpadding="3">
<tr>
<td width="40px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2"> </font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">A</font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">B</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA1_705"></a><font size="2">TYPE</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB1_705"></a><font size="2">VALUE</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA2_705"></a><font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB2_705"></a><font size="2">a</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA3_705"></a><font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB3_705"></a><font size="2">b</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">4</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA4_705"></a><font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB4_705"></a><font size="2">c</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">5</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA5_705"></a><font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB5_705"></a><font size="2">d</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">6</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA6_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB6_705"></a><font size="2">A</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">7</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA7_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB7_705"></a><font size="2">B</font>
</td>
</tr>
</table><p />

Could anyone tell me how to get this to work (I would like all of the underlying cells to be upper cased) with the filter so I end up with the following:

<table border="1" cellspacing="2" cellpadding="3">
<tr>
<td width="40px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2"> </font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">A</font>
</td>
<td width="100px" align="center" valign="top" style="background-color:#eeeeee;">
<font size="2">B</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA1_705"></a><font size="2">TYPE</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB1_705"></a><font size="2">VALUE</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA2_705"></a><font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB2_705"></a><font size="2">A</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA3_705"></a><font size="2">1</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB3_705"></a><font size="2">B</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">4</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA4_705"></a><font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB4_705"></a><font size="2">C</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">5</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA5_705"></a><font size="2">2</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB5_705"></a><font size="2">D</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">6</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA6_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB6_705"></a><font size="2">E</font>
</td>
</tr>
<tr>
<td width="40px" style="background-color:#eeeeee;">
<font size="2">7</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cA7_705"></a><font size="2">3</font>
</td>
<td width="100px" style="background-color:#ffffff;">
<a name="cB7_705"></a><font size="2">F</font>
</td>
</tr>
</table><p />



Thanks.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I'm not quite sure what you are after. Maybe

Code:
Sub test()
Dim LR As Long, c As Range
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
For Each c In Range("B2:B" & LR).SpecialCells(xlCellTypeVisible)
    c.Value = UCase(c.Value)
Next c
End Sub
 
Upvote 0
Thanks. I was hoping to make the existing code work (I have a bunch of different routines to update) without making too many changes.

I was just puzzled by the fact that doing a Cells = Range.Value returns the same cell values independent of having a filter applied to the worksheet but when you do a Range.Value = Cells, it behaves differently if there is a filter enabled.
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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