Excel displays incorrect cell data, disappears - excel

Data in my spreadsheet seems to be stored correctly but is displaying wrong.
Each row is displayed in the subsequent row. Row 1 data is not displayed. Row 2 data is displayed in row 1. Row 3 data is displayed in row 2, and so on. If a cell in Row 2 is double-clicked, the correct cell contents are displayed in the formula bar, while the cell contents being displayed disappear.
Also, all text appears to be vertically aligned along the top inside the cells, even though this option isn't chosen. (text appears shifted to the top of each cell)
Sorry if this seems confusing, I appreciate the help. Below is a picture of what all the text looks like, and an example of the cell/formula bar displaying different contents

The modification of the font size can indeed cause such an issue, although normally font size modification also causes row heigth update.
I would advise you to do following modifications to your macro:
Currently:
Sheet2.Rows("2:" & Rows.Count).ClearContents
Sheet2.Rows("2:" & Rows.Count).ClearFormats
Sheet2.Cells.Font.Name = "Calibri"
Sheet2.Cells.Font.Size = 11
Change it into:
Sheet2.Rows("2:" & Rows.Count).ClearContents
Sheet2.Rows("2:" & Rows.Count).ClearFormats
As you see, just drop the formatting specification.

Related

VBA Shapes(Application.Caller).TopLeftCell.Row Returning Incorrect Row

TIA for the help.
I am having a problem with the row for shapes returning an incorrect value. Here is my setup:
Each of the buttons on the right is named after the corresponding column header, the value in column A of the same row, and a number combining the row and column number (ex. Edit_555_10 would be the name of the first shape in cell G3). This ensures there are no duplicate names for when Application.Caller is used in the code.
All of the "Import Data" buttons run the same macro to import data. The Batch IDs have a corresponding sheet which the data is pasted to by using this code:
variable = Shapes(Application.Caller).TopLeftCell.Row
Worksheets(Range("A" & variable).Value).Range("A1").PasteSpecial xlPasteAll
However, during runtime, any and all "Import Data" buttons will return 3 when assigning a value to the variable. I have tested it with 3 different "Import Data" buttons in I3, I4, and I5 at the same time, each returning 3. During runtime I have moved the buttons to all areas of the sheet, and they all return row 3 every time. I have even moved all shapes out of row 3 and 4 so it wouldn't be possible to return 3, and it still returns 3.
All the "Edit" and "Create Tabs" buttons behave normally and have no problems. But for some reason, even without duplicate names, and setting the "Import Data" buttons down as far as row 8, they always return row 3 during runtime, which is obviously causing data to be put in the incorrect place.
Any ideas?
You have not posted the full code for the miscreant Shapes, but if you substitute your code for the Import Data Shapes with:
Dim s As Shape, r As Long, sname As String
sname = Application.Caller
Set s = ActiveSheet.Shapes(sname)
r = s.TopLeftCell.Row
MsgBox r
do you get a different value for each Shape ??
So I actually figured it out. But I am still confused as to why it is happening.
When pressing the "Edit" button, an option is to delete the entry. This grabs the row from Shapes(Application.Caller).TopLeftCell.Row and sets it to a variable. It then deletes the entry with Range("A" & variable & ":I" & variable).Delete xlShiftUp When doing this, the "Edit" and "Create Tabs" buttons are deleted no problem. However, the "Import Data" buttons are just scrunched up to 0 height and end up between I2 and I3. So every time I had deleted the entries in the past, it just kept adding to the stack of invisible buttons; meaning when I was running a test with the same names, there were 10 or so duplicate buttons for each unique name, and the row of one of the invisible buttons was returned.
My workaround for this is to cycle through the shapes, compare each Shapes(name).TopLeftCell.Row with Shapes(Application.Caller).TopLeftCell.Row and delete the shapes in the same row.
So while the problem is fixed, I am not sure why the "Edit" and "Create Tabs" buttons were deleted correctly each time while the "Import Data" buttons remained and were scrunched to be invisible. If anyone has thoughts I would be interested.

dynamically set the print area by visible content not formula

I need to dynamically set the print area of a sheet (Sheet1!). Sheet1 is a summery sheet listing the content of multiple other sheets through an array formula, with content in col A:C. the number of rows are always different, so in order to have the print area height dynamically set to be according to the visible data, i entered a new name in Name Manager with this formula =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$C:$C),3) named DynPrint. and the actual Print Area for Sheet1 I referred to =DynPrint. this should work, but the problem is that excel sees the array formula as content and therefor I'm getting 2000 rows printed out unless i'm manually resetting the print area before each print.
Thank You

Copy Block of Data with Blanks to Other Location

I'm reusing this string from another thread to copy a whole column WITH the blanks (needed for alignment of other information) to a new location. BUT I see that its copy action will stop at the first blank AND infact it does. What I need it to do is copy the blanks and everything as a block then put it under the Range as below. I considered filling all the blanks first but that just sends the fill value all the way to infinity. There will be more blanks than data.
Range(Range("P2"), Range("P2").End(xlDown)).Copy '!!!Stops at frist blank!!!
For idx = 1 To 1
Columns("P:P").Cut
Cells(Range("D2").End(xlDown).Row + 1, "D").Select
ActiveSheet.Paste
Next
I'm not seeing why it can't do that. I don't need it to be THIS code if there is some other solution. The task is that I'm changing the layout from one associated information fills a complete row to a "stacked" layout, where associated data (with some blanks) repeats down the column. So the segments are to be stacked. Cut copy pate with the whole columns has been mostly working.
It could copy a range based on the non empty value of another cell prior to moving it. BUT it needs to land on the first empty cell at the bottom of the new range. I'll repeat this for several columns but can do it separately.
I could bypass the first issue if I with some code that would look at the cells in columns BI through BO and fill them with a value ("0" or "-") IF the value in BH is NOT blank.
Your code and question is a little bit confusing but I think this is what you're looking for. this should copy all data from column P including blanks.
Range(Range("P2"), Range("P" & ActiveSheet.Cells(ActiveSheet.Rows.Count, "P").End(xlUp).Row)).Copy

Excel Bar/Column graph showing blank cells

Starting with screenshot:
http://i.imgur.com/Isj9MER.png
(I'm a new user, can't post images)
Working for a call center. We have a program that tracks our time spent in various phone states (so when we're on calls, out to lunch, etc) that can export data for a given team and date range as a CSV.
I'm working on automating this report. The way it works is that the team lead will pull the CSV, copy-paste into another tab, and then I've got a bunch of array formula If functions and Indirect references to pull all the data as shown. The data analysis and everything is working great.
My problem is the graph. Right now, I've got column B with an If function that either outputs the agent's email (which is how the system tracks it) or "" if all emails have been used. The rest of the columns have If(B2="","", [relevant formula]). That way, we can have all the team leads with various (and fluctuating) team sizes use the same report with a simple copy-paste.
My problem is the stupid bar chart. It pulls data from rows 2-32 (A2:A32). Our current largest team is 28, and I left room for new hires showing up soon. My problem happens when I use data from one of our smaller teams. As you can see, even though the blank rows are filled with "" in every cell, it's still displaying those rows. In the chart. This means that with the smallest team (shown), the chart is half wasted whitespace.
Is there a way to make the column chart only show rows that have actual data in them?
One thing I tried was putting an Indirect reference for Series Values. So I had a cell (AA1) with {=MAX(IF(B2:B31="","",ROW(B2:B31)))}. That outputs the row number of the last non-blank row. Then for the Series Values I put =Indirect("Report!A2:A"&AA1), but Excel gave me an error saying the function was not valid. I guess you can only have an actual range (and not a formula) in the data input for a chart.
Excel 2016, by the way.
I come up with three possible solutions for this problem.
Convert your data table to a pivot table and use a pivot chart (currently only available in Windows version, sorry if you are using a Mac).
Use a bit of VBA to hide the empty rows.
Use a bit of VBA to modify the data displayed on the chart.
Sample Data Setup
Although you provided a screen shot of your data, it is not simple to convert that into a test case to demonstrate the three solutions. Therefore, I threw together this very simple set up.
Column A contains a list of possible "users".
Cell D1 is a user entry to change the contents of column B and C.
Column B contains the actual "users". It is calculated with =IF(A2<=$D$1,A2,"")
Column C contains the data that goes with the "users". It is calculated with =IF(B2<>"",10,"")
A chart is added to the sheet.
Below is a screenshot of the sample setup, where all potential users are included. (Note: It is Sheet1)
Below is a screenshot of the sample setup, where only potential users A through E are included.
The white space in the second image is the problem we are trying to address.
SOLUTION 1: Make a pivot table of the data
Select all of the pertinent data, in this case B1:C12.
Select Insert -> Pivot Table
In the Create Pivot Table dialog, make sure "New Worksheet" is selected, and click OK.
On the Pivot Table, place "User" field in Rows, and "Total" field in Values. Select the Value Field Settings... for the "Total" field and make sure it uses Sum.
In the Pivot Table, select the Row Labels drop down, Label Filters, Greater Than... . Type "" into the dialog and select OK.
From Pivot Table Tools -> Analyze, select PivotChart. Choose Bar Chart from the dialog.
Below is a screen shot of the Pivot Table.
On the tab with the data, change the last potential user from E to G. On the Pivot Table, refresh the data.
Below is a screen shot of the refreshed pivot table.
SOLUTION 2: Use VBA to hide empty rows
The below code is attached to a button made visible on the worksheet. (n.b. There are other ways to activate the code such as change events, but this will depend on a number of factors outside the scope of this answer).
Sub HideRows()
Dim AllCatRange As Range
Set AllCatRange = Worksheets("Sheet1").Range("B2:B12")
Dim iLoop As Long
For iLoop = 1 To AllCatRange.Rows.Count
If AllCatRange.Cells(iLoop, 1) = "" Then
AllCatRange.Cells(iLoop, 1).EntireRow.Hidden = True
Else
AllCatRange.Cells(iLoop, 1).EntireRow.Hidden = False
End If
Next iLoop
Set AllCatRange = Nothing
End Sub
Below is a screen shot of the data tab with the button added.
After clicking the button, it now looks like this ...
This code will expand rows when they contain data, and collapse rows when they do not.
A potential problem with this approach is that expanding/collapsing rows will change the size of the chart, if the chart lays over those rows.
SOLUTION 3: Use VBA to modify the chart
The below code is attached to a button, and used to modify the chart after the source data is changed.
Sub ModifyChart()
Dim AllCatRange As Range
Set AllCatRange = Worksheets("Sheet1").Range("B2:B12")
Dim lastRow As Long
lastRow = 1
Dim iLoop As Long
For iLoop = 1 To 11
If AllCatRange.Cells(iLoop, 1) <> "" Then
lastRow = lastRow + 1
End If
Next iLoop
Dim PlotCatRange As Range
Set PlotCatRange = Worksheets("Sheet1").Range("B2:B" & lastRow)
Dim PlotSerRange As Range
Set PlotSerRange = Worksheets("Sheet1").Range("C2:C" & lastRow)
Worksheets("Sheet1").ChartObjects(1).Chart.FullSeriesCollection(1).XValues = "=Sheet1!" & PlotCatRange.Address
Worksheets("Sheet1").ChartObjects(1).Chart.FullSeriesCollection(1).Values = "=Sheet1!" & PlotSerRange.Address
Set AllCatRange = Nothing
Set PlotCatRange = Nothing
Set PlotSerRange = Nothing
End Sub
Below is a screenshot of the data tab with the new button in place.
Below is a screenshot of the tab after clicking the button.
Wrap up
My personal preference is to use VBA to modify the chart after the data is modified, as this reflects what you would do manually.
I found a solution! Thanks largely to this page. Here's what I did:
I created made a named range using this formula: =OFFSET(Report!$B$1,1,0,COUNTIF(Report!$B$2:$B$30,"<>-"),1)
For that to work, I had to change all the empty cells to output "-" when empty in stead of "". I couldn't get COUNTIF to accept "<>""" or "<>" or any other weird tricks I tried. Using "-" was easier.
That makes a dynamic named range that changes size as I put data into the sheet. I named a similar range for everything I'm trying to chart (Approved Status, Call Ready, Not Ready). The chart was able to accept those names and now it's dynamically sized. If I only have three agents on the sheet, it shows three huge bars. With twenty, it shows twenty bars (exactly what I was looking for).
One other tip: I changed my first row to output "" when empty, so that COUNTIF(Report!$B$2:$B$30,"<>-") always returns at least 1 (otherwise you get annoying errors because you have a named range referencing a 0-length array).

VBA Excel: change Page Break according to number of rows and last row position

I made a workbook consist of few sheets. By using VBA I read varying number of rows on sheet1 and copy them to last sheet that act like some kind of custom report. In most cases this last sheet is long just one page during printing, but sometime VBA routine copy rows with plenty data, arranged into two or more lines of text, so last rows of such reports (where shall be a sign, date, place etc.) appears unexpected on Page 2! And if I made Page break on the way that only Page 1 is printable then my report lost these details; in second case, if I set Page break to 2 pages (and on the page are just few rows of data), then process of printing will produce printing of 2 pages: one filled with data and second, empty!
Is there some way to detect where is situated last row with data, and then from VBA change printing area according to result of detection, to print only Page 1 or Page 1 and Page 2 together!? I tried it by using row number, but some rows have twice height than others, so I gave up!
Thank you.
In meantime, I thought I found simple and working solution (Excel 2010):
LastRow3 = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox "Last row filled with data is " & LastRow3"
'detect how many pages is in this sheet
ActiveWorkbook.Sheets(1).Range("A1").Select
tnp= (ActiveSheet.HPageBreaks.Count + 1) * (ActiveSheet.VPageBreaks.Count + 1)
pagnr = "Total number of pages is:" & tnp
MsgBox pagnr
After that, based on tnp variable value is probably easy to set Page break with:
ActiveSheet.PageSetup.PrintArea = "$A$1:$E$80" ' takes in account Page1 and Page2
or
ActiveSheet.PageSetup.PrintArea = "$A$1:$E$50" 'only one page is for printing
Anybody has better solution!? Thank you.

Resources