Vba excel: invisibility if data in cells - excel

I am dumping data from one sheet to another using vba code .All cells are getting correctly populated except 6 cells in the beginning.Actually the data us populated in these cells but its not visible until I double click on the cells.What is going wrong.Can someone help?

If you are blaming the wrong format you have two options:
a) clear the format of the destination cells, e.g. Worksheets("DestinationSheet").Range("A1:A6").ClearFormats and then copy the data without formatting
b) copy the format from some cell whose format is correct, e.g. Worksheets("DestinationSheet").Range("B1").Copy Destination:=Worksheets("Sheet1").Range("A1:A6") and then copy the data without formatting (this means you copy good format but wrong data first, but then in the next action you are going to overwrite the wrong data with good data)
Note 1: to copy the data without formatting it is better to use direct assignment Worksheets("DestinationSheet").Range("A1:A61").Value = Worksheets("SourceSheet").Range("A1:A6").Value than Copy function
Note 2: In my view it is always better to avoid using Copy & Paste in two commands because you are messing with the system clipboard, which it dangerous at least. Just imagine the user wants to use the clipboard in another program at the same time you are running the macro in Excel... To my best knowledge, the Copy function above (without being followed by Paste) does not use the system clipboard (please someone do correct me if I am wrong)

Related

SSIS: can I write formulas to a destination spreadsheet?

I am using SSIS to write data from a SQL source to an Excel destination. Plugging in hardcoded values is fine. However, I've been requested to put in SUM() formulas in the last column. They want the actual formula, not the summed value, which I could do on the SSIS side.
I wrote a formula that worked and added it as an additional column in my data transform:
"=SUM(OFFSET($A$1,ROW()-1,3,1,9))"
HOWEVER, when I open my target spreadsheet, this formula is just plugged in as text. It will not evaluate unless I expressly edit the cell (just click in and out). F9, Shift-F9, etc do not work.
I'm pretty sure there's no way to overcome this, but I thought I'd check. I am open to other solutions. I cannot pre-fill the formulas, since the amount of data I'm adding is variable.
I should also mention that I do not want to use any solution that requires manipulating Excel via the Interop library. The entire point of this exercise is to stop using Excel on the server machine, so I can only install the relevant driver, not Excel itself.

Copy cells from excel sheet and paste it to a DB query Sikuli

I am new to Sikuli. I need to copy data from excel sheets and paste them to a DB query using sikuli script. And how can I iterate among the excel cells to copy and paste the data repeatedly.
These data needs to copied and pasted one after the other.
It might be easier to copy all of the cells at once, then paste them one by one.
once Sikuli has opened Excel, you could do something like:
type(Key.HOME, KeyModifier.CTRL) #takes you to cell A1
type("a", KeyModifier.CTRL) #select all
type("c", KeyModifier.CTRL) #copy to clipboard
fromExcel = Env.GetClipboard().strip() #get clipboard contents into Sikuli, without leading or trailing white space
cells = fromExcel.split("/n") #split each cell into list on newline
#go to the destination app, maybe using App.open("nameOfYourApp") if it's not open yet, or App.focus("nameOfYourApp") if it is already open
for cell in cells: #use python to iterate through your list
#navigate to the line or cell where you want to paste
paste(cell)
Would something like that be of help?
Rather than providing a specific approach let's understand the options you have.
Simulate user keyboard actions (like it is described here by #autoKarma).
Excel sheet having a very specific structure allows you to detect some key points like first column and first row and then calculate other cells locations based on them.
You can try and use one of the Python Excel API libraries to access the excel sheet directly via API. If you only need to read the document and to to amend it, I believe this will be fairly easy to do.
Note: In all cases you will obviously have to think of how do you bring yourself to the point where you have an open Excel sheet on your screen and how to dispose of it when done.

Auto populate column by reference?

I am working on the following sheet, called Raw_Data:
In a new sheet, I want to copy the registration_date column by reference. This means that if I change the registration_date on the Raw_Data, the changed value should be reflected in the new sheet.
To implement this, I have entered the following =Raw_Data!C2. So far it works fine as you can see below:
But the problem is that when I double click on the little green square here, it doesn't automatically populate the entire column.
I don't want to manually drag-and-drop because there are several thousand rows. Does anyone know how I could automatically populate the column by reference?
While there are some automation things you could do, I think a lot of that would be overkill for what could be just an input issue with how you are choosing to copy in Excel.
If you find that you need to copy a large block of data, rather than dragging the corner of your cell like that, try one of these alternate methods:
While selected on the cell, press CNTRL + C. Then in the 'Name Box' (where it shows the address of the cell you are on), type in the cell where you want to go (A17000); then press SHIFT + ENTER. This will jump you to that cell, and will highlight all cells inbetween where you were and where you are going. Then press CNTRL + V.
Another method of moving around a large data block in Excel is to hold CNTRL and press an arrow key. This will move you as far down the data block as possible. Note that this will not work on a blank sheet, as there is no data and therefore Excel doesn't know when to stop.
Again - some automation would be possible here, but moving around an Excel worksheet is something you will be doing too frequently in too many different ways to want to automate what might be 5 keystrokes once a week.
In excel the "double click to fill" feature is a heuristic based feature that fills the cells that appear to be relevant with the selected formula (+ reference corrections).
In your case there is no hint the heuristic can use to tell what to fill so nothing is being done.
Regarding dragging "several thousand rows", that's not a real problem. If you only do it once, there no reason to even trying anything "smart" or complicated.
You can also copy the source cell, select all the cells you want to fill and then paste. You can select cells in any way you like, not just dragging.
You could instead of all the dragging/copying/filling approach simply copy directly from the source, and paste by reference.
As a last resort, you can always go for a VBA solution to do that for you.

Issue with Copying Excel Cells

I am not sure if there is a solution to this issue. In Excel when you copy a cell the border starts moving/flashing. Only when that border is moving you are allowed to paste its contents into another cell. Once the border is no longer moving, you are also unable to paste its contents. The other option is to double click on the cell or go into the formula bar to manually copy the data, which then you can paste as much as you want, at least until something else is copied.
Here is my problem. I have script within the “Private Sub Worksheet_SelectionChange(ByVal Target As Range)” section of my worksheet. So every time another cell is selected this script is run. The script works perfectly and I do not need this changed. The spreadsheet first needs to be unlocked before the script can run, so I had to put this at the top of the script “ActiveSheet.Unprotect” and this at the bottom “ActiveSheet.Protect”. When I select a different cell and the code hits either the unprotect or the protect commands, then the copied cell border is no longer flashing, so I can’t paste.
Is there a script of some sort that I can use so it retains the copied data? The script should only run when a cell is copied. I also don’t want to use the SendKeys function because that typically causes more issues then what it solves. I have always been wondering where Excel stores its data for copied cells, since usually copied data is stored in the windows clipboard, or some other dumping ground since Windows 7 & 8 no longer use the clipboard for copping data. So I don’t understand why Excel doesn’t use the same method as everything else when a cell is copied.
This isn’t too much of an issue, more of an annoyance than anything. If someone should have a solution to this please let me know, but I don’t think there is one.

Pasting the same text copied from different sources behaves differently in Excel

Now this is a weird one
We have a project where we are reading some data from an Excel spreadsheet. Obviously this data has to be in a certain format. Some of the fields consists of numbers, but should be treated as text.
To stop Excel from being "smart" and change the cell types, I have set the format in the respective cells to 'text'.
Now here is the problem: some of the numbers we're pasting have spacing between the digits. When we remove the white spaces, Excel change the cell format to 'standard' and turn the text into the 2.42805E+11 format.
BUT: this only happens when the text is copied from some sources. If a paste a number copied from a textbox, everything turn out fine when we edit the spaces. If we copy the exact same number from a web page, Excel change cell format.
I thought copy-paste would be copy-paste, but obviously some formating or something gets along on the ride.
Does anyone know what causes this, or know have to get Excel to stop being "smart" with the formating?
EDIT: I found a somewhat peculiar solution to this. I recorded a macro that uses the 'Paste Special' function with text as parameter, and overrided ctrl-v with it (in that particular spreadsheet). Works like a charm! Feels a bit "hacky", though. Can anyone think of a scenario where this will backfire?
Try using the Edit Paste Special command, it will give you some controls to choose what to do with the data.
For a taste of the complexity of what is really going on underneath, look in MSDN about Clipboard Formats. In short, it isn't all Excel's fault...
A common user trick copying data out of excel is to paste it into Notepad and cut it back to the clipboard, which flattens all the formatting down to plain text. It won't help you for pasting data into Excel, however.
Copy-paste in windows retains formatting. One way to get rid of the formatting is to paste the text into e.g. notepad first, then select and copy it again. This loses any copied formatting.

Resources