+ Reply to Thread
Results 1 to 3 of 3

How to simplify the VBA code from function to send rows into send range

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    How to simplify the VBA code from function to send rows into send range

    Hi,
    I have a code where the function is to send a certain amount of data from a single rows value from one sheet to another sheet by name in a specific cell.
    my question is whether function can be simplified if we want to send 2 or more rows because this is the appropriate code below just to send the value from the 2 rows on the 8 column value
    And will become a long code if we want to send 50 rows

    Dim nextRow As Range
    Dim lngCounter As Long
    
    If Range("B3").Value = "" Then Exit Sub 'variable-1
    On Error Resume Next
    With Sheets(Range("B3").Value) 'variable-2
        Set nextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
    End With
    If Not nextRow Is Nothing Then
      For lngCounter = 1 To 8
        Select Case lngCounter
          Case 1
            nextRow = "'" & Cells(3, 2 + lngCounter).Value 'variabel-3
          Case Else
            nextRow.Offset(0, lngCounter - 1) = Cells(3, 2 + lngCounter).Value 'variabel-4
        End Select
      Next lngCounter
    End If
    
    If Range("B4").Value = "" Then Exit Sub
    On Error Resume Next
    With Sheets(Range("B4").Value)
        Set nextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
    End With
    If Not nextRow Is Nothing Then
      For lngCounter = 1 To 8
        Select Case lngCounter
          Case 1
            nextRow = "'" & Cells(4, 2 + lngCounter).Value
          Case Else
            nextRow.Offset(0, lngCounter - 1) = Cells(4, 2 + lngCounter).Value
        End Select
      Next lngCounter
    End If
    If the code "send ROWS value from one sheet to other in the next blank row" how if simplified to " send RANGE value from one sheet to other in the next blank row"
    Thanks for the help
    Last edited by herukuncahyono; 07-13-2013 at 03:11 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: How to simplify the VBA code from function to send rows into send range

    Hi, herukuncahyono,

    maybe try it like this (untested):
    Dim nextRow As Range
    Dim lngCounter As Long
    Dim lngRow As Long
    
    lngRow = 3
    Do While Range("B" & lngRow).Value <> ""
      On Error Resume Next
      With Sheets(Range("B" & lngRow).Value) 'variable-2
        Set nextRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
      End With
      If Not nextRow Is Nothing Then
        For lngCounter = 1 To 8
          Select Case lngCounter
            Case 1
              nextRow = "'" & Cells(lngRow, 2 + lngCounter).Value 'variabel-3
            Case Else
              nextRow.Offset(0, lngCounter - 1) = Cells(lngRow, 2 + lngCounter).Value 'variabel-4
          End Select
        Next lngCounter
      End If
      lngRow = lngRow + 1
    Loop
    I doubt itīs a good idea to have On Error Resume Next in the code.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    Re: How to simplify the VBA code from function to send rows into send range

    Hi Holgar,
    After I try it on the sheet ,works as I wanted ... thank you

    regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1