+ Reply to Thread
Results 1 to 9 of 9

Add Columns to Code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    1,009

    Add Columns to Code

    Hello,

    I would like to add columns 8 and 9 to the below code so that column 7 is not included. The line is ' For ApptCol = 4 To 6'

    Can this be done?

    The reason I want to do this is so the duration on the Schedule sheet is calculated from the start time to end time.

    Sub Appt_Load()
    With Schedule
        If .Range("B9").Value = Empty Then
            MsgBox "Please select a correct appointment"
            Exit Sub
        End If
        ApptRow = .Range("B9").Value
        .Range("E3").Value = Appts.Range("C" & ApptRow).Value 'Add in Contact Name
        For ApptCol = 4 To 6
            .Range("E" & ApptCol + 1).Value = Appts.Cells(ApptRow, ApptCol).Value 'Add in Appt Details
        Next ApptCol
    End With
    Schedule.Range("E3").Select
    End Sub
    Attached Files Attached Files
    Last edited by billy60; 03-22-2023 at 12:32 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,880

    Re: Add Columns to Code

    Try:

    Sub Appt_Load()
    With Schedule
        If .Range("B9").Value = Empty Then
            MsgBox "Please select a correct appointment"
            Exit Sub
        End If
        ApptRow = .Range("B9").Value
        .Range("E3").Value = Appts.Range("C" & ApptRow).Value 'Add in Contact Name
        For ApptCol = 4 To 9
            If ApptCol <> 7 Then
                .Range("E" & ApptCol + 1).Value = Appts.Cells(ApptRow, ApptCol).Value 'Add in Appt Details
            End If
        Next ApptCol
    End With
    Schedule.Range("E3").Select
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,843

    Re: Add Columns to Code

    I don't love the low elegance of this solution but it will work.
    Sub Appt_Load()
    With Schedule
        If .Range("B9").Value = Empty Then
            MsgBox "Please select a correct appointment"
            Exit Sub
        End If
        ApptRow = .Range("B9").Value
        .Range("E3").Value = Appts.Range("C" & ApptRow).Value 'Add in Contact Name
        For ApptCol = 4 To 9
            If ApptCol <> 7 Then
               .Range("E" & ApptCol + 1).Value = Appts.Cells(ApptRow, ApptCol).Value 'Add in Appt Details
            End If
        Next ApptCol
    End With
    Schedule.Range("E3").Select
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,880

    Re: Add Columns to Code

    @6SJ: hahhh, you copy my code, colour it in, and then call it inelegant!

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,843

    Re: Add Columns to Code

    Quote Originally Posted by TMS View Post
    @6SJ: hahhh, you copy my code, colour it in, and then call it inelegant!
    Let's just say great minds think alike! I was just slow on the draw.

    I just don't like having a loop with exceptions. Some languages would allow this
    Formula: copy to clipboard
    For ApptCol = 4, 5, 6, 8, 9

    but not VBA.

    I guess you could do this. But I can't think of a good way to express "4-9 except 7" without writing the numbers individually.
    Sub Appt_Load()
    
    Dim ApptCol As Variant
    
    With Schedule
        If .Range("B9").Value = Empty Then
            MsgBox "Please select a correct appointment"
            Exit Sub
        End If
        ApptRow = .Range("B9").Value
        .Range("E3").Value = Appts.Range("C" & ApptRow).Value 'Add in Contact Name
        For Each ApptCol In Array(4, 5, 6, 8, 9)
               .Range("E" & ApptCol + 1).Value = Appts.Cells(ApptRow, ApptCol).Value 'Add in Appt Details
        Next ApptCol
    End With
    Schedule.Range("E3").Select
    End Sub

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,880

    Re: Add Columns to Code

    You could have two loops: 4 to 6 and 8 to 9. Not elegant at all. But it would work.

  7. #7
    Forum Contributor
    Join Date
    02-26-2023
    Location
    Lousianna
    MS-Off Ver
    Microsoft 365
    Posts
    1,009

    Re: Add Columns to Code

    Thank you very much for your solutions @TMS and @6StringJazzer. They both work great!

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,880

    Re: Add Columns to Code

    You're welcome. Thanks for the rep.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,880

    Re: Add Columns to Code

    I quite like the array approach and, at first glance, it looks scalable. Until the OP say, "I’ve got 100 columns and I need to ignore 7 of them … 7, 23, 31, 40, 52, 67 and 82" or whatever .

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Need fix for VBA Code to Custom Sort multiple columns that includes DATE columns
    By Kirk3737 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2020, 02:35 PM
  2. VBA Code to copy values from two columns of a WS to two columns of another WS
    By Webz2k in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2019, 03:24 AM
  3. Need macro code to rearrange columns and to delete unwanted columns
    By Giri.hb in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 08-16-2019, 03:00 AM
  4. Replies: 3
    Last Post: 06-29-2016, 01:38 PM
  5. VBA code to hide filtered columns (simple table 5 rows / 6 columns wide)
    By e2010 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-18-2014, 02:41 PM
  6. [SOLVED] Use VBA code to insert 30 blank columns every 30 columns
    By tsamuels in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-02-2014, 11:02 PM
  7. Code selects entire rows upto one range and same code selects only columns for other.
    By sriharigk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2012, 05:19 AM

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