How do I prevent an excel table's formatting from overwriting a range's existing formatting in VBA?

Jared Rondeau

I'm working with a spreadsheet that requires the use of a table. I resize it to only include the headers and the first line of data I'm working with, but when I then refit it to cover all of my existing data, it overwrites the formatting and resets the color back to default white. I need these colors as they are referenced later in the code. Is there a way to prevent the table from doing this?

Dim FLF As Worksheet
Set FLF = Workbook("My Workbook").Sheets("FLF")

Dim x As Long
Dim lng As Long

With FLF
FLF.Activate

.ListObjects("Table1").Resize Range("$A$6:$K$7")

lng = .Cells(.Rows.count, "D").End(xlUp).Row

.Range("E7:G" & lng).NumberFormat = "0.00%"

.ListObjects("Table1").Resize Range("$A$6:$K$" & lng)

For x = 7 To lng
    If .Range("A" & x).Interior.ColorIndex = 46 Then
        TopPercent = .Range("K" & x).Value
        Do
            x = x + 1
            .Range("K" & x) = TopPercent * .Range("F" & x).Value
            .Range("K" & x).Font.FontStyle = "Italic"
        Loop While .Range("A" & x + 1).Interior.ColorIndex = 36
    End If
Next x
Czeskleba

I tried to recreate the thing you are describing. I think I'm missunderstanding whats happening.

I made a table, put some data below the table, colored the cells yellow and then dragged that little blue thing in the bottom right down over my yellow data to include it in the table. For me it keeps the formatting.

The only thing I can think of, is that you manually applied some form of filling to the table? You could change the table style and formatting. If you want to keep your table, the only thing I can think of is to save the colorindex of the important range before expanding the table and then reformatting that range after you have expanded it. This could be done in several ways but since we are already in VBA, how about this?

'I don't know how to Dim this in one line, sorry
Dim ColorIndexArray()
ReDim ColorIndexArray(ThisWorkbook.Sheets("FLF").Cells(ThisWorkbook.Sheets("FLF").Rows.Count, "A").End(xlUp).Row)

For i = 1 To ThisWorkbook.Sheets("FLF").Cells(ThisWorkbook.Sheets("FLF").Rows.Count, "A").End(xlUp).Row
    ColorIndexArray(i) = ThisWorkbook.Sheets("FLF").Range("A" & i).Interior.ColorIndex
Next

'Do your stuff

For i = 1 To ThisWorkbook.Sheets("FLF").Cells(ThisWorkbook.Sheets("FLF").Rows.Count, "A").End(xlUp).Row
    ThisWorkbook.Sheets("FLF").Range("A" & i).Interior.ColorIndex = ColorIndexArray(i)
Next

Edit: I tried loading all indices into the array at once and failed, hence the loop. Also, if you have tens or hundreds of thousands of rows, and you already have an array now, you could do your calculations on it instead to speed things up? But if its only a couple lines it shouldnt matter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I prevent tar from overwriting an existing archive?

Table Formatting in Excel 2007: How do I remove it?

how do I get a cell's location as a range in excel vba?

How to do conditional formatting of a label in Excel VBA

How do I use custom formatting to change number from ########### to #####.##.##.## in Excel?

How do i apply custom date formatting from excel in java?

How do I apply conditional formatting to a table from a graph?

How to just add conditional formatting to a cell without overwriting the cell's direct formatting

Create email from Excel-VBA to include Range with Conditional Formatting

How do I convert the Excel formula =STDEV.S(IF(FREQUENCY(range,range),range)) into VBA code?

How do you prevent pandas to_csv from formatting numbers as numeric in order to preserve leading 0s?

How do I write to an existing excel file using xlwt and keep the formatting?

How do I copy and paste formatting of excel tab to other tabs except a selected sheet using excel VBA

Replace table text in PowerPoint from Excel VBA, preserve PowerPoint formatting

Excel VBA copy cell formatting to range

Excel VBA - apply conditional formatting formula to range

How do I prevent Visual Studio Code from breaking long lines when formatting code?

How do I automate upper case formatting for select columns in excel with VBA?

Prevent date formatting in VBA?

How do I apply formatting to the parts of word.range?

How do I stop Debian from overwriting /etc/resolv.conf and overwriting my VPN's nameservers?

How to get the background color from a Conditional Formatting in Excel using VBA

Preserve formatting of Word table in Excel VBA

prevent excel formatting as number

How to use VBA for conditional formatting on the same range

How can I modify the "Applies To" property of a Cell's Conditional Formatting rule using VBA?

How do I change the formatting of numbers in an array in VBA

How do I avoid data formatting when replacing text VBA?

How to add Conditional Formatting in Excel for a Range of Values

TOP Ranking

HotTag

Archive