+ Reply to Thread
Results 1 to 12 of 12

Coding for offset cell references with multiple row headings with merged cells

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Coding for offset cell references with multiple row headings with merged cells

    Hi

    I understand how to code to specify a cell in which to enter data into a spreadhseet, when the heading contains only one row.

    In the attached example the headings contain multiple rows with merged cells, is there any way of overcoming this so that the entries are placed in the correct cells?

    Many thanks.

    Andy
    Attached Files Attached Files
    Last edited by AndyE; 01-04-2010 at 02:32 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Coding for offset cell references with multiple row headings with merged cells

    It'sbest to avoid Merged Cells completely. Your code would be easier if you didn't have empty columns as well

    Change your Submit code to this
    
    Private Sub cmbSubmit_Click()
        Dim NextRw As Long
        'Write data to worksheet
        If Me.cboOvertimeType.Value = "" Then
            MsgBox "Please select the type of Overtime worked.", vbExclamation, "Overtime Form"
            Me.cboOvertimeType.SetFocus
        End If
    
        With Worksheets("Overtime")
         NextRw = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
            .Cells(NextRw, 1).Value = Me.RosStartDatePicker.Value
            .Cells(NextRw, 2).Value = TimeValue(Me.RosStartTimePicker.Value)
            .Cells(NextRw, 3).Value = Me.RosEndDatePicker.Value
            .Cells(NextRw, 4).Value = TimeValue(Me.RosEndTimePicker.Value)
            .Cells(NextRw, 6).Value = Me.ActStartDatePicker.Value
            .Cells(NextRw, 7).Value = TimeValue(Me.ActStartTimePicker.Value)
            .Cells(NextRw, 8).Value = Me.ActEndDatePicker.Value
            .Cells(NextRw, 9).Value = TimeValue(Me.ActEndTimePicker.Value)
            .Cells(NextRw, 12).Value = Me.cboOvertimeType.Value
        End With
    
        Unload Me
    
    End Sub
    Last edited by royUK; 01-04-2010 at 01:02 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Coding for offset cell references with multiple row headings with merged cells

    Hello Andy,

    I have your workbook open. Can you give me an example of what the problem is?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Coding for offset cell references with multiple row headings with merged cells

    Leith

    If you open the form and click submit you will find that the values entered will be placed in various columns across Row 4, rather than in the order A4, B4, C4 etc.

    Thanks,

    Andy

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Coding for offset cell references with multiple row headings with merged cells

    Have you checked the code that I posted?

  6. #6
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Coding for offset cell references with multiple row headings with merged cells

    Yes I have Roy, sorry I replied for the benefit of Leigh & hadn't read the code at that time.

    I've tried it and it works a peach except that it begins entering the data on Row 18 for some reason.....any ideas?

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Coding for offset cell references with multiple row headings with merged cells

    Hello AndyE,

    I revised your macro after I figured out what you wanted to do. This macro has been added to the user form in the attached workbook.
    Private Sub cmbSubmit_Click()
      
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Overtime")
    
       'First entry starts at "A4"
        Set Rng = Wks.Range("A4")
        
       'Find the last entry in column "A"
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        
       'If last entry is below "A4" then use row 4. Otherwise, use the last entry row + 1
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, RngEnd.Offset(1, 0))
        
       'Make the range a single row of 12 columns
        Set Rng = Rng.Resize(ColumnSize:=12)
        
       'Write data to worksheet
        If Me.cboOvertimeType.Value = "" Then
           MsgBox "Please select the type of Overtime worked.", vbExclamation, "Overtime Form"
           Me.cboOvertimeType.SetFocus
        End If
    
          With Rng
            .Item(1).Value = RosStartDatePicker.Value
            .Item(2).Value = VBA.Format(RosStartTimePicker.Value, "hh:mm:ss")
            .Item(3).Value = RosEndDatePicker.Value
            .Item(4).Value = VBA.Format(RosEndTimePicker.Value, "hh:mm:ss")
            .Item(6).Value = ActStartDatePicker.Value
            .Item(7).Value = VBA.Format(ActStartTimePicker.Value, "hh:mm:ss")
            .Item(8).Value = ActEndDatePicker.Value
            .Item(9).Value = VBA.Format(ActEndTimePicker.Value, "hh:mm:ss")
            .Item(12).Value = cboOvertimeType.Value
          End With
    
        Unload Me
    
    End Sub
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Coding for offset cell references with multiple row headings with merged cells

    I figuered out the starting point in Row 18...just me being addtitionally thick....

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Coding for offset cell references with multiple row headings with merged cells

    Quote Originally Posted by AndyE View Post
    I figuered out the starting point in Row 18...just me being addtitionally thick....
    What was it?

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Coding for offset cell references with multiple row headings with merged cells

    Quote Originally Posted by AndyE View Post
    Yes I have Roy, sorry I replied for the benefit of Leigh & hadn't read the code at that time.

    I've tried it and it works a peach except that it begins entering the data on Row 18 for some reason.....any ideas?
    It shouldn't,it posted to Row 4 in your example

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Coding for offset cell references with multiple row headings with merged cells

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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