Excel VBA Loop and Replace if Text

AdamJ82

New Member
Joined
Feb 6, 2018
Messages
3
Hello all. This is probably a simple one that I am just having problems figuring out because I am still learning VBA. But I am trying to loop through a column (Q) and replace any values that are Text with the current time and leave any numeric cells alone from rows 2 to 3000. I've tried every way I can think of from complex functions to the simplest I can and it either stops once the first value is replaced or just plain does not work at all. The latest thing (and simplest way I could think of) I've tried stops once the first value is replaced (Below). Any help is greatly appreciated.

Code:
Sub GP_Click()
Dim i As Integer


ThisWorkbook.Sheets("Performance Log").Select


i = 2


Do While i < 3000
    If Application.WorksheetFunction.IsText(ActiveCell.Value) Then
    Cells(i, 17).Value = Now
    End If
    i = i + 1
Loop






End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can also use the IsNumeric function iirc.

Btw you can change the type of loop to this...
Code:
For i = 2 To 2999
    ...
Next i

that would save you from incrementing 'i' yourself

to see why it stops just step through the code, set a breakpoint and step through each line with F8
 
Last edited:
Upvote 0
You are only ever testing the condition of the ActiveCell (you're not actually looping through cells). For example

Code:
Sub GP_Click()
Dim i As Integer

dim myWS as worksheet
set myWS=ThisWorkbook.Sheets("Performance Log")
i = 2

Do While i < 3000
    If Application.WorksheetFunction.IsText(myWS.cells(i,17).Value) Then[INDENT]myWS.Cells(i, 17).Value = Now[/INDENT]
    End If
    i = i + 1
Loop

End Sub
 
Last edited:
Upvote 0
Hi & welcome to the board.

How about
Code:
Sub GP_Click()
   Dim Cl As Range
   
   With ThisWorkbook.Sheets("Performance Log")
      For Each Cl In .Range("Q2:Q3000")
         If Application.WorksheetFunction.IsText(Cl.Value) Then
            Cl.Value = Now
         End If
      Next Cl
   End With

End Sub
 
Upvote 0
Try:

Code:
Range("Q2:Q3000").SpecialCells(xlCellTypeConstants, xlTextValues) = Time
 
Upvote 0
If there are no formulas in Column Q, then you can do what you want without using a loop...
Code:
[table="width: 500"]
[tr]
	[td]Sub GP_Click()
  Range("Q2", Cells(Rows.Count, "Q").End(xlUp)).SpecialCells(xlConstants, xlTextValues) = Now
End Sub[/td]
[/tr]
[/table]


Edit Note
------------------
This macro is similar to what Eric posted (I guess I should have refreshed the webpage before posting) except that I let the code determine the end of the data rather than hard-coding a guess at a cell that is at or after the last data cell. I also used a shorter, alternate defined constant name for the first argument in the SpecialCells function call.
 
Last edited:
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
For what it's worth, I used Time instead of Now. The difference being that Now includes date and time, while Time only includes the time. Choose the one that you want.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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