EXCEL VBA Macro: Can this be simplified using Range?

Sharif Mohamed
Sub NewPortName ()
      If ThisWorkbook.Sheets("PAR Form").Cells(2, 7).Value = "RJ45" Then
      ThisWorkbook.Sheets("PAR_import").Cells(16, 3).Value = "PCI-" +      ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(2, 13), 7)

      ElseIf ThisWorkbook.Sheets("PAR Form").Cells(2, 7).Value = "LC-LC" Then
      ThisWorkbook.Sheets("PAR_import").Cells(16, 3).Value = "PFI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(2, 13), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(2, 36) + " to " + Left(ThisWorkbook.Sheets("PAR Form").Cells(2, 14), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(2, 38)
      End If
      If ThisWorkbook.Sheets("PAR Form").Cells(3, 7).Value = "RJ45" Then
      ThisWorkbook.Sheets("PAR_import").Cells(17, 3).Value = "PCI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(3, 13), 7)

      ElseIf ThisWorkbook.Sheets("PAR Form").Cells(3, 7).Value = "LC-LC" Then
      ThisWorkbook.Sheets("PAR_import").Cells(17, 3).Value = "PFI-" + ThisWorkbook.Sheets("Equipment details").Cells(4, 4).Value + "-" + Left(ThisWorkbook.Sheets("PAR Form").Cells(3, 13), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(3, 36) + " to " + Left(ThisWorkbook.Sheets("PAR Form").Cells(3, 14), 10) + ":" + ThisWorkbook.Sheets("PAR Form").Cells(3, 38)
      End If

End sub

I am currently modifying this line by line for individual cells due to the nature of the output over several ranges.

I am wondering if this can be simplified using Range, Two of the ranges in question out of 7 are (M2:M100) and (N2:N100)

I will need to repeat this code and change the cells individually over 700 times to reflect 700 individual cells if I can't make this abstract

Davesexcel

Check it out,

Sub Button1_Click()
    Dim sh As Worksheet, ws As Worksheet, Esh As Worksheet
    Dim Rws As Long, Rng As Range, c As Range, cr
    Dim s1 As String, s2 As String, s3 As String

    Set sh = Sheets("PAR Form")
    Set ws = Sheets("PAR_import")
    Set Esh = Sheets("Equipment details")

    s1 = "RJ45"
    s2 = "LC-LC"
    s3 = Esh.Cells(4, 4).Value

    With sh
        Rws = .Cells(.Rows.Count, "G").End(xlUp).Row
        Set Rng = .Range(.Cells(2, "G"), .Cells(Rws, "G"))
    End With

    For Each c In Rng.Cells
        cr = c.Row
        If c = s1 Then
            ws.Cells(cr + 14, 3).Value = "PCI-" + s3 + "-" + Left(sh.Cells(cr, 13), 7)
        ElseIf c = s2 Then
            ws.Cells(cr + 14, 3).Value = "PFI-" + s3 + "-" + Left(sh.Cells(cr, 13), 10) + ":" + sh.Cells(cr, 36) + " to " + Left(sh.Cells(cr, 14), 10) + ":" + sh.Cells(cr, 38)
        End If
    Next c

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel VBA using Range in Macro

From Dev

Can't specify the exact range in Excel vba macro

From Dev

Sort range of cells in single column using excel vba macro

From Dev

Using Range in VBA and Excel

From Dev

Can using Option Explicit speed up a large macro in Excel VBA?

From Dev

Copy a variable range in excel with VBA macro

From Dev

VBA excel, run macro in cell range that = to "yes"

From Dev

VBA Excel: Macro-created graph with dynamic range

From Dev

VBA excel macro for a button that selects named reference from a named range

From Dev

Excel VBA: Can Shell run a macro in a workbook in another Excel instance

From Dev

How to find a row in an Excel sheet using Excel Macro VBA?

From Dev

Excel VBA select entire sheet using Range

From Dev

Excel VBA - Using lookup formula for a dynamic range

From Dev

Excel VBA - Custom Filter using Named Range

From Dev

Excel VBA select range using cells and xlDown

From Dev

Error when using a String as Range (VBA Excel)

From Dev

Excel VBA select entire sheet using Range

From Dev

Excel VBA - Using lookup formula for a dynamic range

From Dev

excel VBA Range for columns using count

From Dev

Excel VBA using Range returned from Function

From Dev

Using AutoFill in Excel VBA for a range of cells

From Dev

Dynamic Naming of Range of Excel using vba

From Dev

Set range using a variable for the row - Excel VBA

From Dev

Paste an Excel range in browser using Selenium + VBA

From Dev

excel macro using variable to define range of a integer failure

From Dev

Excel offsetting macro increase performance by using Variant instead of Range

From Dev

excel macro using variable to define range of a integer failure

From Dev

Excel 2010 Macro "Command could not be completed by using the range specified"

From Dev

Excel vba sorting macro

Related Related

  1. 1

    Excel VBA using Range in Macro

  2. 2

    Can't specify the exact range in Excel vba macro

  3. 3

    Sort range of cells in single column using excel vba macro

  4. 4

    Using Range in VBA and Excel

  5. 5

    Can using Option Explicit speed up a large macro in Excel VBA?

  6. 6

    Copy a variable range in excel with VBA macro

  7. 7

    VBA excel, run macro in cell range that = to "yes"

  8. 8

    VBA Excel: Macro-created graph with dynamic range

  9. 9

    VBA excel macro for a button that selects named reference from a named range

  10. 10

    Excel VBA: Can Shell run a macro in a workbook in another Excel instance

  11. 11

    How to find a row in an Excel sheet using Excel Macro VBA?

  12. 12

    Excel VBA select entire sheet using Range

  13. 13

    Excel VBA - Using lookup formula for a dynamic range

  14. 14

    Excel VBA - Custom Filter using Named Range

  15. 15

    Excel VBA select range using cells and xlDown

  16. 16

    Error when using a String as Range (VBA Excel)

  17. 17

    Excel VBA select entire sheet using Range

  18. 18

    Excel VBA - Using lookup formula for a dynamic range

  19. 19

    excel VBA Range for columns using count

  20. 20

    Excel VBA using Range returned from Function

  21. 21

    Using AutoFill in Excel VBA for a range of cells

  22. 22

    Dynamic Naming of Range of Excel using vba

  23. 23

    Set range using a variable for the row - Excel VBA

  24. 24

    Paste an Excel range in browser using Selenium + VBA

  25. 25

    excel macro using variable to define range of a integer failure

  26. 26

    Excel offsetting macro increase performance by using Variant instead of Range

  27. 27

    excel macro using variable to define range of a integer failure

  28. 28

    Excel 2010 Macro "Command could not be completed by using the range specified"

  29. 29

    Excel vba sorting macro

HotTag

Archive