-1

I am running a code that pulls data from PI using conditional formula. When the values are pulled, it assigns the formula to entire column but only few of them have actual values:

enter image description here

The column has about 300,000 rows but I want to run the loop only when I have actual values (4 times instead of 300,000 in this case).

Are there any suggestions to do it avoiding the loop 300,000 times?

I have tried using the replace function by replacing all the spaces with blank and then count the number of non-blank cells using COUNTA :

'Replacing all spaces in a column by blanks
Workseets("Sheet6").Range("D:D") = Replace(Worksheets("Sheet6").Range("D:D")," ","")
'counting non-blank cells
n = WorksheetFunction.CountA(Worksheets("Sheet6").Range("D:D"))
'running code only 4 times
for I = 1 to n.....

But I am getting type mismatch error for the replace function. I have not written anything inside for FOR Loop yet. Currently just trying to use the REPLACE function correctly

3
  • Dim n As Long and Dim I as Long, 300,000 well exceeds the limit of integer data type. Also, please consider showing what you're doing inside the loop. Sep 7, 2016 at 18:50
  • If a loop is necessary (and it may not be) you could use Do While loop instead of For loop, with criteria to exit when the cell value is empty. Sep 7, 2016 at 18:52
  • Ideally, instead of using hard-coded Workseets("Sheet6").Range("D:D") you'll define a proper Range variable that represents only the rows/data you want to process. Sep 7, 2016 at 18:57

2 Answers 2

2

Your attempt uses the String.Replace function and causes a mismatch error, because you're passing a variant/range to a function which expects a string.

'Replacing all spaces in a column by blanks
Workseets("Sheet6").Range("D:D") = Replace(Worksheets("Sheet6").Range("D:D")," ","")

A solution is to use the Range.Replace method, instead. No assignment is necessary, the method operates on the range object which calls it. This copies the values directly and replaces the empty string

'Replacing all spaces in a column by blanks
 With Worksheets("Sheet6").Range("D:D")
    .Value = .Value2
    .Replace " ", "", xlWhole
 End With
1
  • Range.Replace only returns a Boolean function. It is giving TRUE for every cell in the column
    – Ahsan Khan
    Sep 7, 2016 at 20:39
2

You should use the Intersect method to trim the range down to the actual used cells.

With Worksheets("Sheet6")

    Intersect(.Range("D:D"), .UsedRange).Replace " ", ""

End With
3
  • If the range has spaces or other non-printing characters, or contains formula which output same, the UsedRange will not be reliable. (There are other reasons why UsedRange is not always reliable, too!) Sep 7, 2016 at 20:18
  • From the OP there: it assigns the formula to entire column but only few of them have actual values. Sep 7, 2016 at 20:24
  • Rather than get in a pissing match about this, it would've been much more appropriate for you to simply edit my answer for what is an obvious oversight. Sep 7, 2016 at 21:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.