0

I have two bits of codes that work seperatley, but I can't figure out how to combine them. I want to go through a column of numbers and test if they are >= 100 and then change the result if true.

I don't know how many rows this column will be so I have used the following line in another part of this Macro.

Range("D1:D" & Final & "").

And I have successfully tested a single cell by directly specifiying it like so:

Sub CellTest()
Dim cell As Integer
cell = Range("D7").Value
If cell >= 100 Then
Range("D7").Value = "NoSplit"
End If
End Sub

Can anybody help me combine these so it iterates through every row and tests the value?

Update:

This spreadsheet is downloaded via a script on a webpage and has a different name each time. There are a few things I will know ahead of time such as...

  • There is only 1 sheet
  • The column in question is column D
  • column D will only have numbers with no decimal points
  • I will not know how many rows in this column ahead of time
2
  • 1
    I see that there's an answer for you listed, but will bring up some basics for looping. A loop will iterate through an array of values; that's it. What you do with that can be simple or complex. Let's use "i" as a variable to account for iterating... your data can be a range of cells in column A (1) from rows 1 to 10 and you want to go 1 by 1, e.g., 1 to 2, 2 to 3, 3 to 4, etc. You can generate a for loop to account for this, such that For i = 1 to 10 Step 1, then you can utilize the iterated value (rows) using cells(i,1).value = "" inside of your loop. Commented Jan 10, 2019 at 17:07
  • @Cyril I saw the demo of that method, but was unsure how to utilize it since I don't know how many rows. Commented Jan 10, 2019 at 21:03

1 Answer 1

2
Public Sub testforhundred()
    Dim rcell As Range, rng As Range
    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("D1:D" & ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count)
    For Each rcell In rng.Cells
        If rcell.Value >= 100 Then rcell.Value = "NoSplit"
    Next rcell
End Sub

this combines them easily. However if the row count is huge itll be time consuming due to interacting with the sheet object

Sign up to request clarification or add additional context in comments.

9 Comments

Hmm the sheet name will change per use, so having it hard coded is a no go :(
Will it be a specified sheet or the ActiveSheet?
@FreeSoftwareServers, your question doesn't mention anything about dynamic sheets. If this is a criteria, it needs to be listed in your question. Even with your comment, it's not clear what sheet. Do you want to loop this code through all sheets? Some sheet? Will it just run on the selected sheet? Please edit your question and specify any criteria
@FreeSoftwareServers For all extensive purposes, an answer can explain how to do something (which doesn't have to be a copy-pasteable response), where it is an understood assumption that persons asking questions have some understanding of the subject matter when conversing with experts. Essentially, the poster of a question must be able to recognize the point made by the expert answering said question.
With this, you should be able to apply Doug's answer to your code. (Don't forget to mark it as correct if it solves your issue).
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.