Better way to write this

Nlhicks

Active Member
Joined
Jan 8, 2021
Messages
264
Office Version
  1. 365
Platform
  1. Windows
Is there a better way to write this and have it still do what it is doing?


VBA Code:
Sub TryUpdate()

Dim RngRange01 As Range

    rngRang01 = Range("A" & Rows.Count).End(xlUp).Row
    Range("A2:P" & rngRang01).SpecialCells(xlCellTypeVisible).Select

 
    If Worksheets("Line Update").Range("B11") <> Worksheets("Line Update").Range("E11") Then
    Worksheets("Sheet2").Range("B2:B" & rngRang01).Value = Worksheets("Line Update").Range("E11").Value
   
    End If
   
    If Worksheets("Line Update").Range("B12") <> Worksheets("Line Update").Range("E12") Then
    Worksheets("Sheet2").Range("C2:C" & rngRang01).Value = Worksheets("Line Update").Range("E12").Value
 
    End If

    If Worksheets("Line Update").Range("B13") <> Worksheets("Line Update").Range("E13") Then
    Worksheets("Sheet2").Range("D2:D" & rngRang01).Value = Worksheets("Line Update").Range("E13").Value
 
    End If

    If Worksheets("Line Update").Range("B14") <> Worksheets("Line Update").Range("E14") Then
    Worksheets("Sheet2").Range("E2:E" & rngRang01).Value = Worksheets("Line Update").Range("E14").Value
 
    End If
   
     If Worksheets("Line Update").Range("B15") <> Worksheets("Line Update").Range("E15") Then
     Worksheets("Sheet2").Range("F2:F" & rngRang01).Value = Worksheets("Line Update").Range("E15").Value
 
    End If
     If Worksheets("Line Update").Range("B16") <> Worksheets("Line Update").Range("E16") Then
        Worksheets("Sheet2").Range("G2:G" & rngRang01).Value = Worksheets("Line Update").Range("E16").Value
 
    End If
   
    If Worksheets("Line Update").Range("B17") <> Worksheets("Line Update").Range("E17") Then
    Worksheets("Sheet2").Range("H2:H" & rngRang01).Value = Worksheets("Line Update").Range("E17").Value
 
    End If

    If Worksheets("Line Update").Range("B18") <> Worksheets("Line Update").Range("E18") Then
    Worksheets("Sheet2").Range("I2:I" & rngRang01).Value = Worksheets("Line Update").Range("E18").Value
 
    End If


End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
How about
VBA Code:
Sub TryUpdate()
   Dim RngRange01 As Long, i As Long
   Dim Ws As Worksheet

   Set Ws = Sheets("Sheet2")
    Range("A2:P" & rngRang01).SpecialCells(xlCellTypeVisible).Select

   With Worksheets("Line Update")
      rngRang01 = .Range("A" & Rows.Count).End(xlUp).Row
      For i = 11 To 18
         If .Range("B" & i) <> .Range("E" & i) Then
            Ws.Cells(2, i - 9).Resize(rngRang01 - 1).Value = .Range("E" & i).Value
         End If
      Next i
   End With
End Sub
 
Upvote 0
Solution
I got the error below and it pointed to
1666793709032.png




1666793631480.png
 
Upvote 0
As I see no point in having that line, just delete it.
 
Upvote 0
Whoo Hooo it worked, I had to modify a couple of minor things but it works:)🥳👯‍♂️👯‍♀️
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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