0

The code I have, works, but it is slow and I want to avoid using select.

I have tried something in the line of the following:

Sub PopulateBlastEvents()

    Dim wsfr As Worksheet
    Dim wsl As Worksheet
    Dim BlNumber As String
    Dim BSStep As Long

    Dim SI As String
    Dim Srng As Range
    Dim Nrng As Range

    Dim Arng As Range

    Dim NotF As String

    Dim Found As Range

        Application.ScreenUpdating = False

        NotF = "NO INFO"
        BSStep = 1

            Set Rrng = Sheets("Blast List").Range("A2:A45")

            Set Srng = Sheets("Blast List").Range("E1:R1")

            For Each cell In Rrng

                If cell <> "" Then

                    For Each cell2 In Srng

                        If cell2 <> "" Then

                            On Error Resume Next

                            SI = cell.Value

                            BlNumber = CStr("Blasted " & BSStep)

                            Set wsfr = Sheets(CStr(BlNumber))
                            Set wsl = Sheets("Blast List")

                                With wsfr.Range("A:A")

                                    Set Found = Cells.Find(What:=SI, LookIn:=xlFormulas, _
                                    LookAt:=xlPart, SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

                                    If Found Is Nothing Then

                                    With wsl.Range("A:A")

                                        Set Found1 = Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
                                        LookAt:=xlPart, SearchOrder:=xlByRows, _
                                        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1)

                                        Found1.Value = NotF

                                    End With

                                    Else

                                    With wsl.Range("A:A")

                                        Set Found1 = Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
                                        LookAt:=xlPart, SearchOrder:=xlByRows, _
                                        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1)

                                        Found1.Value = Found.Value

                                    End With

                                    End If

                                End With

                        End If

                    Next cell2

                    BSStep = BSStep + 1

                End If

            Next cell

        Set Arng = ThisWorkbook.Worksheets("Blast List").Range("E3:X3").End(xlDown).Select

    Application.ScreenUpdating = True

        Columns("A:S").EntireColumn.AutoFit

End Sub


The code does run, but returns no value as the range value "rng" remains at NOTHING even though it is in the sheet where it is looking for the value.

Below is the current code I am using that needs to change:

Sub PopulateBlastEvents()

    Dim wsfr As Worksheet
    Dim wsl As Worksheet
    Dim BlNumber As String
    Dim BSStep As Long

    Dim SI As String
    Dim Srng As Range
    Dim Nrng As Range

    Dim Rrng As Range
    Dim Brng As Range

    Dim Arng As Range

    Dim NotF As String

    Application.ScreenUpdating = False

    NotF = "NO INFO"

    BSStep = 1

    Set Rrng = Sheets("Blast List").Range("A2:A45")

    Set Srng = ThisWorkbook.Worksheets("Blast List").Range("E1:R1")

    For Each Brng In Rrng.Cells

        If Brng <> "" Then

        For Each Nrng In Srng.Cells

        If Nrng <> "" Then

        On Error Resume Next

        SI = Nrng.Value

        BlNumber = CStr("Blasted " & BSStep)

        Set wsfr = ThisWorkbook.Worksheets(CStr(BlNumber))
        Set wsl = ThisWorkbook.Worksheets("Blast List")

        wsfr.Select
            Range("A1").Select
                Cells.Find(What:=SI, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
                Selection.Copy

        If Err.Description <> "" Then

        Sheets("Blast List").Select
            Range("A1").Select
                Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1).Select

                Selection.Value = NotF

        Else

        Sheets("Blast List").Select
            Range("A1").Select
                Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1).Select

                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False

        End If

        End If

        Next Nrng

        BSStep = BSStep + 1

    End If

    Next Brng

    Set Arng = ThisWorkbook.Worksheets("Blast List").Range("E3:X3").End(xlDown).Select

    Application.ScreenUpdating = True

    Columns("A:X").EntireColumn.AutoFit

End Sub

I really want to speed up the code and all previous questions I have posted, I was informed not to or avoid using Select.

Please could someone help.

  • 1
    `Set rng = Range("A1").Find(What:=SI` etc. This only searches cell "A1". You should expand this range to the full range where your value is and it will run correctly. Use a [lastrow statement](https://stackoverflow.com/questions/38882321/better-way-to-find-last-used-row/38882823#38882823) if your data expands dynamically. – Plutian Oct 29 '19 at 11:13
  • Did you read the article about avoiding Select? I'm sure you've been pointed to it. – SJR Oct 29 '19 at 11:32
  • @SJR, have gone through it numerous times and also other articles I found via Google. This is the reason for me wanting to change this code. – Hendrik Sidaway Oct 29 '19 at 11:41
  • So what exactly is the question? As you probably know, we like specifics here. – SJR Oct 29 '19 at 11:42
  • @Plutian I have placed the code in a loop so it can scroll through the code for every line that is not blank in the sheet. Still returns NOTHING – Hendrik Sidaway Oct 29 '19 at 11:42
  • @SJR in my attempt to remove SELECT, I used the code I specified initially, I cannot get the code to work without SELECT and need help where someone can show me part of or full code to use that is faster than what I am currently using. – Hendrik Sidaway Oct 29 '19 at 11:43
  • 1st: Can you show your complete (updated) code, instead of just what is inside the loop? 2nd, even if it is inside a loop, `Range("A1")` is a static reference, it will only refer to cell "A1" no matter what loop it is in. – Plutian Oct 29 '19 at 11:45
  • Comment out the on error lines and step through your code to see what's going on. You can use `wsfr.Cells.Find...` but check first you have found something before doing anything else. – SJR Oct 29 '19 at 11:45
  • Thanks I will try and get back to both of you on what I find – Hendrik Sidaway Oct 29 '19 at 11:49
  • @All I have changed and update the code I am trying to use in my original question. I cannot for the life of me comprehend why it wont work. Please help – Hendrik Sidaway Oct 29 '19 at 14:12

0 Answers0