Hidden rows not hidden after copy paste in excel - vba

I am copying contents of an excel workbook to another workbook.My sheet has few hidden rows which has to be kept hidden in the target workbook as well.
I am using the below lines of code to copy the data :
'Set rngCopyRange = wbSource.ActiveSheet.Cells
rngCopyRange.Copy
This works fine in Win 10 64 bit,Office 2013 64bit or Office 2016 32 bit combination
But in windows 10 64b , Office 2010 32b or 2013 32b combination,the hidden rows are not hidden.
Is this an issue with excel version?Can someboby help me to sort this issue?

How odd. I'm using 2013(64bit) and if I use your code I DO NOT copy across the hidden property of each row to the target.
I think the issue you're getting though is that you're copying across a range that doesn't extend across all columns, so setting the row height (i.e. for all columns) would be counter-intuitive.
If you add .EntireRow to your copy range, the row height will come across:
Set rngCopyRange = wbSource.ActiveSheet.UsedRange.EntireRow

In 99%* of the cases where people hide rows and then copy and paste
them to another location they don't actually need the hidden rows to
exist in the destination
... so I'm taking a punt and offering this as a solution:
rngCopyRange.SpecialCells(xlCellTypeVisible).Copy
*Like all good statistics, I made this one up

This is just a speculation, but if you are using wbSource.ActiveSheet.Cells, aren't you copying all the cells in that worksheet, including the hidden ones ? Maybe the logic is a bit different for the Office versions, though I still new to VBA.

Related

Excel 2016 VBA - Slow performance

Our company recently upgraded to Office 2016 and a lot of people in the office are experiencing very slow calculation speeds (like 1%/s if their lucky).
I have tried restarting, repairing Office, removing the add-ins, etc. I narrowed it down to a file having VBA in it. For example, I created two files: both have two columns of 10k observations of hard-coded numbers and one calculated column. The only difference is that file #1 uses a predefined formula (below) while file #2 uses a UDF (below) and is saved as .xlsm instead of .xlsx.
File #1 (.xlsx)
C1 = 100 * A1 + B1
File #2 (.xlsm)
C1 = UDF(A1, B1)
Where UDF is defined as:
Function UDF(x, y)
UDF = 100 * x + y
End Function
Are there any steps I can try to fix this issue? Not everyone is experiencing it either. I've sent the VBA file around and for those that it calculates fast (i.e. instantaneously) I had them screenshot their VBA references but they are the same as what I have.
If starting Excel in Safe Mode cures the problem, it has to be down to an add-in or startup file - or some combination of them. Disable them all and see if that fixes it, then you can re-enable one by one to try and narrow down the culprit.

VBA Import/Copy Specifc Worksheet witout Opening Workbook

thanks in advance.
I've spent nearly two days routing around but can't quite find/workout what I need.
I need to get an entire specific worksheet from a closed workbook without opening (even in the background).
Further info:
1) My source file has several sheets that I don't want/need. I only need one and it seems sloppy to copy all and trim the data down (plus it means moving large amounts of data unnecessarily).
2) The end goal of this is to save the sheet as a third, seperate, workbook. So if you know a way that helps this that doubles your awesomeness.
3)Targeting individual cells is not a viable solution, there's too much and the code would be huge/akward/cumbersome.
4) I've found a way to import all sheets into my operating/active workbook but cant figure out how to make it target a specific sheet (I'm missing osmehting simple I'm sure).
Test code snippet:
Dim Importsheet As Worksheet
Sheets.Add Type:= _
"C:\Users\haa\Documents\Personal\My Hours E2V-mk3.xlsm\"
Seems simple enough right?
5) Finally, I've seen some potential solution routes that uses "executeexcel4macro" (something I've never even seen before) but so far it only seems to target cell ranges not sheets.
That's all the key info. I really have dug around (and would have liked to crack this myself). Reminder that I'm looking for a way to do this without copying individual cells or opening the source sheet.
Thanks again campers!
4) I've found a way to import all sheets into my operating/active workbook but cant figure out how to make it target a specific sheet (I'm missing osmehting simple I'm sure). Test code snippet
Import all the sheets, then delete all but the one you want to keep.

Finding a VBAS defnied Named Range definition

a valueI've inherited a large VBA project and whilst I have lots of dev expereince I have a small amount of VBA. The code reads data off a sheet in the form:
Intersect(Range("colName"), .Rows(intCurrentRow)).Value
Where colName is a named range, or so I thought. I have searched all of the project code and the excel sheets and cannot find where colName is defined ?
So far I have searched the code, looked in Name Manager on the sheet and have googled furiously but hit a total blank. As I now need to read in another value from the Sheet I would really prefer to use the code that is currently used with another value instead of colName to reference my new data field.
Is there anything obvious I'm missing ?
Edits:
activesheet.range("colName").address gives a value of "$L:$l"
Its probably a hidden name.As Doug Glancy said, you can unhide it using VBA
Activeworkbook.Names("colName").Visible=True
If you are working with defined names you may find it useful to get My & Jan Karel Pieterse's Name Manager addin which (amongst many other things) handles hidden names. download from
http://www.decisionmodels.com/downloads.htm
It could be a hidden Name. Try:
ActiveWorkbook.Names("colName").Visible=True

Link from cell in other sheet shows 0 but other sheet shows data cant get past this

Spent hours searching for any mention of this but no luck. Maybe someone here knows the answer. I developed a large application in excel. sheet1 receives data from a Com server that just places it there. This has been working for a long time with no problems.
I have sheet2 two with links from sheet 1 and formulas. Mostly I do all the processing in sheet1 change event because I got circular refernces when putting functions in a cell.
From yesterday, I have a problem that the cell in sheet 2 shows 0. When I go to the linked cell it has data. Sometimes numbers and sometimes text depending, but sheet 2 shows 0.
As well as the linked cells, I also screen scrape some data with a seperate process and place it sheet 3 to link across to other cells, so sheet2 will also contain links form this data. I spent today changing this so it places the data in directly in sheet1 instead of creating links. This is tedious and will cause other problems.
I checked options -> formulas. Right now it is set at max iterations 100; max change 100. I had reduced max iterations to stop some functions firing themselves recursively through the sheet change event. Now I raised it back to 100.
I deleted all the data saved the sheet started again and the same thing happened again.
e.g. sheet1 cell "A1" has a value of 10
sheet2 cell a1 has a formula of ='sheet1'!A1
sheet 2 cell A1 is showing 0
when I look in the formula bar I see the formula
This data was damaged down to row 42 the last row that had held data. below that the conditional formatting was the correct color even though the formulas looked identical and i could see nothing wrong to explain it, I dragged the formula from 44 upwards again and everything worked, but bizarrely, when I copied from sheet1 cells 1 to 44 and pasted a link back in, I still got 0.
When I run the system and the COM erver delivers a load and the change event fires etc, I end up back with the same problem.
Now when I add a new sheet and paste my links from sheet1 into this, it works as it should.
Right now after my attempts to fix it. All sheet1 and sheet2 columns are formatted general though previousy I had one column text and some others number. I can't control the format of sheet1 because excel just adapts to the data it receives regardless what I do.
I also keep checkng for application.enableevents and this is not the problem nor is worksheet calculate and in the options I have automatic claculation set
The only other routine running under sheet change in sheet 2 is a tidyup that clears out old data after the COM server has delivered a smaller number of rows.
It copies default data from unused cells in the sheet. I placed the right formatting and default such as blank or 0 depending on what is required. This has not in the past caused any issues and I cant see what it might do, though previously when I just tried puttng "" in the cells to be cleared that did cause nasty excel erros. but not now.
Does anyone have any ideas?
I don't know if this is still relevant, but I had the same issue and came here searching for an answer.
In my case, I realized the cell I was trying to copy was a merged cell (i.e. it provided a range) the formula was [='Sheet1'!B10:N10] and the result was 0 and not the text I wanted. so I changed the formula to ='USA-CA'!B10 and it worked.
hope this is helpful to someone else.
This little routine fixes it.
Sub fixformulas()
'fix formulas broken by excel for some reason
Cells.Replace What:="=", Replacement:="=", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
All I can work out is that when excel copies the formulas and puts them back in place it corrupts the equals sign. Everything else is fine but as oon as I replace this the lights go on.
I'd rather know how to prevent it, but I hope nobody else suffers like I have.

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal