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.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Welcome to the board. What does this do:
Code:
Range("StaffWork[Logged]").SpecialCells(xlCellTypeBlanks).Value = dtStaticDate
 
Upvote 0
Theoretically, it selects just the blank cells in the table column called "Logged", and sets the value of these cells to the timestamp held in dtStaticDate. But sometimes, it selects the entire worksheet and splatters the timestamp over the whole thing - not a very satisfactory result!
 
Upvote 0
Do you use an 'On Error Resume Next' statement in your code?

If so, if no blank cells are found, the procedure continues with some previous selection.
 
Upvote 0
If the Logged range only had one cell, then Specialcells would apply to the entire sheet and not just that range - could that be your issue?
 
Upvote 0
JLeno - No, I haven't tried that yet, though it doesn't seem to throw an error... RoryA - This could explain it, as currently I'm working with just a few records of test data. Do you mean it happens when the entire field range is just one cell, or does it happen if there's just one blank cell to select?
 
Upvote 0
If the entire field range is one cell.
 
Upvote 0
JLeno - No, I haven't tried that yet, though it doesn't seem to throw an error... RoryA - This could explain it, as currently I'm working with just a few records of test data. Do you mean it happens when the entire field range is just one cell, or does it happen if there's just one blank cell to select?

I wasn't asking so you can try it, I would advise not to.

If the named range is just 1 cell, Excel will expand the selection to the entire (used portion of the) sheet. You could change the code to

Code:
With Range("StaffWork[Logged]")
If .Cells.Count = 1 Then
If .Value="" Then .Value = dtStaticDate
Else
.SpecialCells(xlCellTypeBlanks).Value = dtStaticDate
End If
 
Upvote 0
That looks like a great solution, JLeno, and RoryA, thanks for the explanation - it explains why I was getting the problem when I cleared down the data and tried a test with just a record or two in each staff worksheet. Back to the code window!
 
Upvote 0
In that case you could modify your code to:
Code:
With Range("StaffWork[Logged]")
  If .Count > 1 Then .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = dtStaticDate
End With
 
Upvote 0

Forum statistics

Threads
1,215,389
Messages
6,124,662
Members
449,178
Latest member
Emilou

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