AutoFit not adjusting row height in VBA code in Excel 2007 - excel-2007

I know many discussions have addressed this but I have not found a solution.
I regularly produce worksheets with about 100 records of text and date fields, each of which must be transposed into a 2-column table and printed to a PDF file.
To do this I have been using VBA code which works through the worksheet rows to sequentially: copy/transpose into two columns in a separate worksheet (Template) and then use rng.ExportAsFixedFormat Type:=xlTypePDF to create the PDF file.
It has all been working fine for several years, until someone recently noticed that sometimes the largest field does not show all the text from the Excel cell. It is invariably cut off after about 1000 - 1100 characters.
Many discussions mention that there is a 1024 limit on cell displays, but I thought this only applied to Excel 2003 and before - 2007 should be fine shouldn't it? In any case, I have found it is always possible to manually adjust the Excel field to reveal all of the text (both in the original worksheet and the temporary 2-column Excel table), sometimes totalling more than 2000 characters. Of course, I don't want to manually adjust and print to PDF 100 times on a regular basis. So I used AutoFit: Sheets("Template").Rows("1:18").Selection.EntireRow.AutoFit
Unfortunately this does not seem to duplicate the manual cell expanding that we have tried successfully. None of the cells is merged. All are wrapped and General formatted. I have tried cleaning text entries via Notepad before entry and inserting blank rows with Alt-F (as suggested elsewhere).
If AutoFit will not work, I am thinking maybe I could include some code to set a customised row height for each table by getting the total word count (is there a function?) and setting row height to about 0.8 * number of words - based on initial calculations.
Any ideas please?

I've used a method similar to what you suggested your last paragraph but with a character count instead of a word count. The VBA function is:
len(range("A1").value)
I did it since I had merged cells and they wouldn't autofit.
You'll have to calibrate for your column width, font and font size but from what I've learned there's no exact method. If you set your characters per line factor too high you might cut off a line, too low and you might get a blank line.

Related

Looking for an excel formula to autofill a column

I'm using a spreadsheet to assign box positions on shelves. The boxes are currently ranked by weight so that I can distribute that weight evenly across all shelves. As I'm assigning box positions, I would like excel to give me a running count for how many boxes have been assigned to each RACK and SHELF in the 'SHELF SPACE' column. A sample of the data is included below
What I'd like the SHELF SPACE column to do
I would like excel to find the previous SHELF SPACE for a given RACK and SHELF and add it to the BOX HEIGHT. The sum would then be the new SHELF SPACE value.
Entering by hand has taken a long time, so I've tried looking for formulas to help, but haven't had any luck piecing things together.
I've tried a lot of different formulas but the following has gotten me the closest.
what I've tried
I concatenated the 'RACK' and 'SHELF' columns, then I tried the following formula (which is in the image above):
INDEX(B2:B12,AGGREGATE(15,6,ROW(E2:E12)-1/(E2:E12="A4"),2))
I feel like this is getting me closer, but it can't give me a running count and only tracks box heights that match "A4" and the aggregate formula will only return a value if I input the K value.
UPDATE: 1/6/2023
I made the following formula, which gets me even closer, but this still only makes a running count for cells containing "A4". I would like it to make a running count for cells that also contain "B4" through "X4"
=IF(U2<>"A4","",SUMIF($U$2:U2,"A4",$P$2:P2))
UPDATE 1/6/2023
I made a nested if formula that seems to be doing what I want, but it is too long, so I've broken it in to 4 parts (1 string for each shelf). Here it is (from another spread sheet that I've been testing on). If it could be simplified, that would be great. I've also read that long nested IF statements can start producing errors.
IF(R2="A4",SUMIF($R$2:R2,"A4",$O$2:O2),IF(R2="B4",SUMIF($R$2:R2,"B4",$O$2:O2),IF(R2="C4",SUMIF($R$2:R2,"C4",$O$2:O2),IF(R2="D4",SUMIF($R$2:R2,"D4",$O$2:O2),IF(R2="E4",SUMIF($R$2:R2,"E4",$O$2:O2),IF(R2="F4",SUMIF($R$2:R2,"F4",$O$2:O2),IF(R2="G4",SUMIF($R$2:R2,"G4",$O$2:O2),IF(R2="H4",SUMIF($R$2:R2,"H4",$O$2:O2),IF(R2="I4",SUMIF($R$2:R2,"I4",$O$2:O2),IF(R2="J4",SUMIF($R$2:R2,"J4",$O$2:O2),IF(R2="K4",SUMIF($R$2:R2,"K4",$O$2:O2),IF(R2="L4",SUMIF($R$2:R2,"L4",$O$2:O2),IF(R2="M4",SUMIF($R$2:R2,"M4",$O$2:O2),IF(R2="N4",SUMIF($R$2:R2,"N4",$O$2:O2),IF(R2="O4",SUMIF($R$2:R2,"O4",$O$2:O2),IF(R2="P4",SUMIF($R$2:R2,"P4",$O$2:O2),IF(R2="Q4",SUMIF($R$2:R2,"Q4",$O$2:O2),IF(R2="R4",SUMIF($R$2:R2,"R4",$O$2:O2),IF(R2="S4",SUMIF($R$2:R2,"S4",$O$2:O2),IF(R2="T4",SUMIF($R$2:R2,"T4",$O$2:O2),IF(R2="U4",SUMIF($R$2:R2,"U4",$O$2:O2),IF(R2="V4",SUMIF($R$2:R2,"V4",$O$2:O2),IF(R2="W4",SUMIF($R$2:R2,"W4",$O$2:O2),IF(R2="X4",SUMIF($R$2:R2,"X4",$O$2:O2),"")))))))))))))))))))))))))))

Workaround for either my approach or the 256 character limit in Excel VBA

My problem is as follows, I have a workbook with (to begin with) 2 worksheets, the first (called WIP) acts as a form of data entry, each new occurrence of the BoM requires an insertion of 4 columns which happens to the left of the existing columns. At the same time a new worksheet is created based on a copy of the existing second worksheet (called FitOut) which pulls various bits of a data from the first worksheet based mainly upon the version of the BoM selected and the supplier referenced.
Of course adding new columns to the WIP sheet causes the functions, arrays and formulas in the sheets to automatically update, I had used a quick workaround by using some code to hold and then paste the new occurrences data into the worksheet which is created at the start of the macro, however the formulas have become slightly complex (due to the need to look for the previous 4 occurrences and return values based on specific cell locations) that the 256 character limit has been completely shot ( I think I'm over 800 on some bits).
I've very limited as to the layout of the WIP sheet, and the sheet needs to be fairly idiot proof (hence macros, buttons etc) but it needs to run well...
ANy and all suggestions/help would be much appreciated.
I have put an example of the formula I am trying to use, if it can be condensed further pleas let me know:
=IFERROR(IF($C$1='WIP'!$U$1,(INDEX('WIP'!$A$1:$X$2500,SMALL(IF('WIP'!$U$1:$U$2500=$A$1,ROW('WIP'!$U$1:$U$2500)),ROW(6:6)),COLUMN('WIP'!$C:$C))),(IF($C$1='WIP'!$X$1,(INDEX('WIP'!$A$1:$AA$2500,SMALL(IF('WIP'!$X$1:$X$2500=$A$1,ROW('WIP'!$X$1:$X$2500)),ROW(6:6)),COLUMN('WIP'!$C:$C))),(IF($C$1='WIP'!$AA$1,(INDEX('WIP'!$A$1:$AD$2500,SMALL(IF('WIP'!$AA$1:$AA$2500=$A$1,ROW('WIP'!$AA$1:$AA$2500)),ROW(6:6)),COLUMN('WIP'!$C:$C))),(INDEX('WIP'!$A$1:$AG$2500,SMALL(IF('WIP'!$AD$1:$AD$2500=$A$1,ROW('WIP'!$AD$1:$AD$2500)),ROW(7:7)),COLUMN('WIP'!$C:$C)))))))),"")

Highlight duplicates not next to each other using conditional formatting (Larger Dataset)

We have a list of product numbers in Excel in a certain order. For reasons I won't get into, we need to highlight when there are duplicates that aren't next to each other.Currently, I'm using this formula in a conditional format to do the job.
=AND(COUNTIF($A$2:$A$82,$A2)>1,$A1<>$A2,$A2<>$A3)
This mostly works well except in cases where there are pairs of duplicates like in the example below, we would want FO-1694 to be highlighted, because they aren't all next to each other. But we would not want NIS0257 to be highlighted because they are.
Currently
Ideally, this is what we want to see...
Expected
I originally asked this same question January 30th (link) and I received what seemed to be a working solution with the conditional formatting formula
=NOT(AND(A2=OFFSET(A2,COUNTIF($A2:$A$82,A2)-1,0),A2=OFFSET(A2,-(COUNTIF($A$2:$A2,A2)-1),0)))
While this original answer appears to work for a small group of data (example), it does not function as needed on much larger datasets like the ones I'll be dealing with (images below are of the issues with the larger dataset). I've adjusted the formula as necessary to accommodate the larger range, so the conditional formatting formula you see in the larger dataset pictures & file is
=NOT(AND(A2=OFFSET(A2,COUNTIF($A2:$A$5422,A2)-1,0),A2=OFFSET(A2,-(COUNTIF($A$2:$A2,A2)-1),0)))
Cells with the arrow pointing at them should be highlighted
For the last picture, even though it's the end of the range the cells should still be highlighted since they're blank and I need all blank cells in the range to be highlighted too.
The full file with the larger dataset can be downloaded from my dropbox account using this link.
I should have thought of those problems:
=NOT(AND(IFERROR(COUNTIF(OFFSET(A1,0,0,-COUNTIF($A$1:$A1,A2)),A2),0)=IFERROR(COUNTIF($A$1:$A1,A2),0),IFERROR(COUNTIF(OFFSET(A3,0,0,COUNTIF($A3:$A$5422,A2)),A2),0)=IFERROR(COUNTIF($A3:$A$5422,A2),0),A2<>""))
Should do it.
I'm not sure if this would work for you
=IF(COUNTIF($A$1:$A$8,A2)>2,1,IF(AND(COUNTIF($A$1:$A$8,A2)=2,A1<>A2,A3<>A2),1,0))
I've tested it on a test bit of data, and seems to work. You need to check for the 1st row, as you cant check the previous cell, so the A1<>A2 part..
1 being true condition on my test so would need tweaking for conditional formatting.
Enjoy.

Excel formulas have stopped working

I have excel sheet which has basic + Array excel formulas, earlier it used to work properly but for the last 1 week it is not give me desired result.
I have tried the following things but nothing seems to work.
Change the formula setting from manual to automatic.
Change the format of the cell from text to General.
No space before the equal sign.
Note: I am using 2010 excel and rest in my office uses 2013 and we shared everything on common folder. Is this can be one of the reason ?
Regarding the ANZ formula, the rows have to match. Someone inserted 12 rows into part of Trades worksheet and this expanded the range to U1:U65548 and Z1:Z65548. The AW column remains at AW1:AW65536. While the ranges can be offset from each other, they have to be the same size.
This shows up on your formula for the FINEX as well.

Unable to move / delete rows in shared workbook - Not enough resources

this one's a bit of a painful one so thank you for your help and patience with me.
We have an Excel spreadsheet that we use as a master file for our website products. As such there are quite a few sheets and quite a few products on each running along side some macros to provide some extra functionality (turning entered data into HTML for product page, etc).
My issue is that one of our most used spreadsheets has become a trouble in that it has some phantom formatting all the way down to the millionth-and-something row and all the way across, causing the last cell to be the very last cell possible.
The issue that has finally popped up as a result is that we can no longer move rows in, out or around the sheet (a required functionality) as it results in an 'out of resources error'.
I've tried:
Highlight all rows below used range to right-click> delete - Results in runtime error (from macro)
Highlighting large chunks of rows and using Clear All - Resulted in the 38MB file bloating to 380MB
Deleting a chunk of rows at a time - Maxed out at 1,000 before it caused Excel to crash
Moving to new spreadsheet - Broke all our macros (which I did not write and am not proficient enough to fix on a new sheet)
Disabling macros and trying the above options, only marginally more efficient but still out of resources
I'm at my wits end on this one and, while we can continue with most day-to-day functions, we will soon be completely unable to use this particular sheet as we need it at all.
I'm wondering if there might be a way to run a VBA script to remove these rows, potentially one by one? I've tried running a short script that went something like rows[960,1000000].Delete (forgive my terrible VBA markup), but this also resulted in not enough resources errors.
I'm wondering if there's anything like:
row = 960;
while(row<=1048576){row.Delete};
Continuing, the runtime error debug points me to the below if statement within the macro:
If Target.Count > 1 Then Exit Sub
Where Target is the variable passed to the sub.
Which strikes me as very odd because my (limited) understanding of VBA and IF's in general simply recognizes that 'if my selection is larger than 1 (row?), do not run this code..
Thanks again in advance.
Use this method only if you don't have any links into or out of the sheet that will get broken. Also might have Sql connections that might get broken. Might need to disable macros. There are many possible problems with this approach. Use at your own risk.
Note the exact "Name" and "(Name)" of the sheet; Look in the VBA code window at the properties for the sheet. "Name" is the name displayed on the worksheet tab. "(Name)" is the code name visible only in the properties window.
Make a list of range names on the sheet.
Copy the data to a new sheet.
Copy any macros to the new sheet.
Delete the old sheet.
Rename the "Name" and "(Name)" of the new sheet the same as the old one.
Recreate range names.
A better method if you don't have too many formats:
Disable macros and set calculation to manual. This avoids recalculating while doing your delete operation.
Select entire sheet and clear formats.
Delete all rows below your data.
Redo your formatting. Select entire column (not just used area) to apply format if applicable.
It is important to remove formatting on the entire sheet from A1 to the end. Otherwise you'll get the bloat you mentioned. Just that step may solve your problem. If not then proceed with removing all the rows below the data. This should not cause file size bloat.