xlTypeBlanks/xlTypeVisible selecting wrong range

ClaireS

Board Regular
Joined
Jul 29, 2013
Messages
155
Office Version
  1. 365
Platform
  1. Windows
I have some simple code that copies values from one workbook into another. When this is done, I want to add a timestamp (in a variable called dtStaticDate) to the copied cells in both the source and destination files. I need to select the blank cells in a table called "StaffWork" in the column headed "Logged". I have been trying this code:
Code:
Range("StaffWork[Logged]").SpecialCells(xlCellTypeBlanks).Select
Selection.FormulaR1C1 = dtStaticDate
This sometimes works fine, but sometimes (and I don't know what makes it happen) it selects either the entire sheet or all the cells in and above the table. What am I doing wrong? There are always one or more new cells to log, as I don't do this if no records have been copied. I have tried filtering the table and using xlTypeVisible instead, but the problem persists.
 
In that case you could modify your code to:
Code:
With Range("StaffWork[Logged]")
  If .Count > 1 Then .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = dtStaticDate
End With

That would actually not capture the case that the named range consists of 1 empty cell, if a timestamp is needed in that case.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Thanks all very much. I have gone with a slightly modified version of JackDanIce's code, as it is more elegant, bumping out of the IF on the first option once the table has got going! This is what I'm using now:
Code:
 With Range("StaffWork[Logged]")
            If .Cells.Count > 1 Then
                .SpecialCells(xlCellTypeBlanks).Value = dtStaticDate
            ElseIf .Value = "" Then
                    .Value = dtStaticDate
            End If
        End With
The suggested versions seemed a bit short on "End if", "End With", so it took me a while to compile, but it's all good now. ;)
 
Upvote 0
BTW I'm not checking whether the size of the range = 1 cell on the ElseIf, as it's a table in Excel 2010 - so if it's not greater than 1 it must BE 1.
 
Upvote 0

Forum statistics

Threads
1,216,227
Messages
6,129,609
Members
449,520
Latest member
TBFrieds

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