Combining rows 3 through 7 into row 2, one row takes a painfully long time with this code. Why?

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
As stated in title, this code block
1) finds any value using FIND,
2) copies it to row 2 of Sheet2, and the next 6 rows below it to a blank sheet and
3) combines all the rows below row 2 into row 2.
4) and will display the results of the combined row in Textbox1 of Userform1.
It was a challenge, that I managed to make work, but is painfully slow in calculating.

Code:
From a button:
Private Sub cmdFIND_Click()
Sheets("BIBLESTUDY").Range("A2:E20").ClearContents
Dim LastRow As Integer, x As String, c As Range, rw As Long
          x = ComboBox1.Value
           LastRow = Sheets("BIBLESTUDY").Range("A" & rows.count).End(xlUp).Row
                 With Worksheets("Sheet2").Range("B1:B31103")
                          Set c = .FIND(x, LookIn:=xlValues, Lookat:=xlPart, MatchCase:=False, SearchFormat:=False)
                            If Not c Is Nothing Then
                             Worksheets("Sheet2").Select
                                c.Select
                                         If LastRow = 1 Then
                                             Range(Cells(c.Row + 5, 2), Cells(c.Row, 3)).Copy Destination:=Sheets("BIBLESTUDY").Range("A" & LastRow + 1)
                                        Else
                                              Worksheets("Sheet2").Select
                                               c.Select
                                             Range(Cells(c.Row + 5, 2), Cells(c.Row, 3)).Copy Destination:=Sheets("BIBLESTUDY").Range("A" & LastRow + 1)
                                        End If
                             Else
                                   MsgBox "value not found"
                            End If
                     End With
                     TextBox1 = Sheets("BIBLESTUDY").Range("B2") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("B3") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("B4") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("B5") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("B6") _
'
                     TextBox4 = Sheets("BIBLESTUDY").Range("C2") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("C3") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("C4") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("C5") _
                      & vbCrLf & Sheets("BIBLESTUDY").Range("C6") _
                    
                     'This code block below seems to be the slow wheel on the gear turn:  (AKA this is what I think is making it take a ong time)                 
                     Dim ws As Worksheet, LastRow2, i As Long
                    Set ws = ThisWorkbook.Sheets("BIBLESTUDY")
                    LastRow2 = ws.Cells(ws.rows.count, 1).End(xlUp).Row + 1
                    For i = 2 To LastRow2
                    ws.Cells(2, i).Value = ws.Cells(2, i).Value _
                    & vbCrLf & ws.Cells(3, i).Value _
                    & vbCrLf & ws.Cells(4, i).Value _
                    & vbCrLf & ws.Cells(5, i).Value _
                    & vbCrLf & ws.Cells(6, i).Value _
                    & vbCrLf & ws.Cells(7, i).Value _

                    Next i
                    Worksheets("BIBLESTUDY").rows(3 & ":" & Worksheets("BIBLESTUDY").rows.count).Delete
    Dim LastEmpty, LastRow3 As Long
    LastEmpty = Sheets("BIBLESTUDY").Cells(rows.count, "A").End(xlUp).Row
    LastRow3 = Worksheets("BIBLESTUDYALL").Range("A" & rows.count).End(xlUp).Row + 1
     MsgBox "LastEmpty row is" & "  " & LastEmpty & "   " & "LastRow3 row is" & "  " & LastRow3
     Sheets("BIBLESTUDY").Range("A2:F2" & LastEmpty).Copy Destination:=Sheets("BIBLESTUDYALL").Range("A" & LastRow3)
   
The concanetnaton or combining process ends here.  it does give the correct of putting all rows below row 2 into row 2  but takes forever.
     End Sub
Yes - this is a lot of code to look at and I don't like posting long code blocks - unless it reveals a better, faster, more efficient way.

The simple bottom line is, is there any faster more efficient way to combine 6 or 7 rows below row 2 into row 2 and copy that to another sheet?


Images show the result. Last image is the code that does the combining - and slows the process down.

A lot of stluff. Any help would be greatly appreciated.
cr
 

Attachments

  • CODE COPIES 6 ROWS EACH ON A SEPARATE ROW.png
    CODE COPIES 6 ROWS EACH ON A SEPARATE ROW.png
    73.8 KB · Views: 9
  • RESULT WITH CODE BLOCK BETWEEN BOLDED LINES ADDED TO THE CODE.png
    RESULT WITH CODE BLOCK BETWEEN BOLDED LINES ADDED TO THE CODE.png
    64.1 KB · Views: 9
  • THIS IS THE CODE BLOCK THAT SLOWS IT DOWN.  WITHOUT THI BLOCK, THE RESULT IS LIGHTNING FAST - ...png
    THIS IS THE CODE BLOCK THAT SLOWS IT DOWN. WITHOUT THI BLOCK, THE RESULT IS LIGHTNING FAST - ...png
    70.3 KB · Views: 11

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
In the code below, why are using a row counter (i / LastRow2) in the column position ?

VBA Code:
                    Set ws = ThisWorkbook.Sheets("BIBLESTUDY")
                    LastRow2 = ws.Cells(ws.rows.count, 1).End(xlUp).Row + 1
                    For i = 2 To LastRow2
                       ws.Cells(2, i).Value = ws.Cells(2, i).Value _
                       & vbCrLf & ws.Cells(3, i).Value _
                       & vbCrLf & ws.Cells(4, i).Value _
                       & vbCrLf & ws.Cells(5, i).Value _
                       & vbCrLf & ws.Cells(6, i).Value _
                       & vbCrLf & ws.Cells(7, i).Value _
                    Next i

I would have expected something more like this:
VBA Code:
    Dim sConcat As String
    sConcat = ws.Cells(2, 2).Value
    For i = 3 To LastRow2
        sConcat = sConcat & vbCrLf & ws.Cells(i, 2).Value
    Next i
    ws.Cells(2, 2).Value = sConcat
 
Upvote 0
Maybe
Code:
Dim lr As Long
Dim joined As String
lr = Cells(Rows.Count, 2).End(xlUp).Row
joined = Join(Application.Transpose(Range("B2:B" & lr)), vbLf)
Cells(2, 2) = joined
 
Upvote 0

Forum statistics

Threads
1,223,874
Messages
6,175,107
Members
452,613
Latest member
amorehouse

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