+ Reply to Thread
Results 1 to 5 of 5

VBA Formatting Added T Incorrect Column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Formatting Added T Incorrect Column

    Hi, I wonder whether someone could help me please.

    I've put together the following code which applies formatting to a given sheet.

    Sub UniqueRecordsABFormat()
        
        Dim cell As Range
        Dim i As Long
        Dim LastRow As Long
        Dim LR As Long
        Dim MyNames As Variant
        
        Application.ScreenUpdating = False
        
        Sheets("Unique Records AB").Select
        Const StartRow As Long = 8
    
        Set Data = Range("E8:P" & Range("B" & Rows.Count).End(xlUp).Row)
    
        With Range("B2")
            .Value = "Current Month"
        End With
    
        With Range("B3")
            .Value = Format(Date, "1 mmm yy")
            .NumberFormat = "mmm yy"
            .HorizontalAlignment = xlCenter
            .Interior.ColorIndex = 37
            With .Font
                .Name = "Lucida Sans"
                .Bold = True
                .Size = 10
            End With
        End With
    
        With Range("B2, B5")
            .HorizontalAlignment = xlCenter
            .Interior.ColorIndex = 11
            With .Font
                .Name = "Lucida Sans"
                .Bold = True
                .Size = 11
                .ColorIndex = 2
            End With
        End With
    
        With Range("B7:R7")
            .HorizontalAlignment = xlCenter
            .Interior.ColorIndex = 37
            With .Font
                .Name = "Lucida Sans"
                .Bold = True
                .Size = 10
            End With
        End With
    
        For Each cl In Data
            If cl.Value = 0 Then
                cl.Value = ""
            End If
        Next cl
    
        LastRow = Cells(Rows.Count, "B").End(xlUp).Row
        If LastRow >= StartRow Then
            With Range("B8:R" & LastRow)
                With .Font
                    .Name = "Lucida Sans"
                    .Size = 10
                End With
            End With
    
            With Range("E8:P" & LastRow)
                .HorizontalAlignment = xlCenter
                .NumberFormat = "#,##0.00"
                .Value = Application.Round(.Value, 2)
            End With
            With Range("B8:R" & LastRow)
                .Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 2), order2:=xlAscending
            End With
        End If
    
        For Each cell In Range("R8", Range("B" & Rows.Count - 1).End(xlUp)).Cells
            If cell.Value = "" Then
                cell.Value = "Fixed Resource"
            End If
        Next cell
    
        For Each cell In Range("E8:P8", Range("B" & Rows.Count - 1).End(xlUp)).Cells
            If cell.Value = 0 Then
                cell.Value = ""
            End If
        Next cell
        
        Columns("B:R").AutoFit
        End Sub
    The code works, except for this section:

    For Each cell In Range("R8", Range("B" & Rows.Count - 1).End(xlUp)).Cells
            If cell.Value = "" Then
                cell.Value = "Fixed Resource"
            End If
        Next cell
    What I'm trying to do is add the word "Fixed Resource" if any of the cells in column R, are emtpy.

    This works, but the problem is that it also applies the same value to the cells in column Q and I'm not sure why.

    I just wondered whether someone could possibly look at this please and let me know where I'm going wrong?

    Many thanks and kind regards

    Chris

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA Formatting Added T Incorrect Column

    probably a typo
    For Each cell In Range("R8", Range("R" & Rows.Count - 1).End(xlUp)).Cells

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Formatting Added T Incorrect Column

    Hi @nilem, thank you for replying to my post and for the solution.

    I have to admit that it wasn't a typo but more stupidity on my part.

    I had assumed that the second cell reference was used to determine which rows to check, i.e if column B was blank don't check column R.

    A very useful learning experience!

    All the best and kind regards

    Chris

  4. #4
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: VBA Formatting Added T Incorrect Column

    Hi Chris,

    Change line
    For Each cell In Range("R8", Range("B" & Rows.Count - 1).End(xlUp)).Cells
    to
    For Each cell In Range("R8", Range("R" & Rows.Count - 1).End(xlUp)).Cells.
    Hope this will fix the issue or else attach a sample file displaying required output.

    Regards,
    Last edited by BS Singh; 05-18-2014 at 09:39 AM.

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Formatting Added T Incorrect Column

    Hi @BS Singh thank you also for taking the time to reply to my post and for the solution. It is greatly appreciated.

    All the best and kind regards

    Chris

+ 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. Incorrect Formula - conditional formatting
    By mahowarth in forum Excel General
    Replies: 4
    Last Post: 09-10-2013, 08:27 PM
  2. [SOLVED] Formatting the Master data with incorrect macro
    By Deepa12 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-11-2012, 11:26 PM
  3. Incorrect default locale when formatting cells
    By Tjbaer in forum Excel General
    Replies: 0
    Last Post: 07-29-2009, 06:30 PM
  4. Replies: 3
    Last Post: 07-22-2005, 08:05 PM
  5. Incorrect Date formatting
    By dionne_w in forum Excel General
    Replies: 2
    Last Post: 02-22-2005, 06:06 PM

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