Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Issue to copy details

Hi,
It is not copying full details of sheet through code below. Why? 
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = F2                Debug.Print F2                Application.DisplayAlerts = False                Windows(Curr_Bk).Activate                Worksheets(F2).Range("A2:D100").Copy                                wbk.Activate                Worksheets(F2).Range("A2").PasteSpecial xlPasteValues                wbk.Close SaveChanges:=True

Open in new window

When you run the file below, you can choose relevant Text file as the input. It will prompt for file when saving Excel file.
https://1drv.ms/u/s!Ai8CrEskdewXu0c7aU5u35GZ2aA_?e=FOsiGq
Avatar of HainKurt
HainKurt
Flag of Canada image

can you update the code, it does not show properly
all on one line

+ zip attached have pw, which is not provided...
Avatar of Peter Chan

ASKER

Good day Hainkurt,
Code below is not doing copy properly
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Str0
               
                Application.DisplayAlerts = False
                Windows(Curr_Bk).Activate
                Worksheets(Str0).Range("A1:D100").Copy
               
                'Windows(W0).Activate
                wbk.Activate
                Worksheets(Str0).Range("A1").PasteSpecial xlPasteValues
                'Workbooks(W0).Close SaveChanges:=True
                wbk.Close SaveChanges:=True

Open in new window



Avatar of Norie
Norie

What isn't being copied?
As it stand, your code is only copying values, in other words, formula results are kept, everything else is not (formatting, font, color ect ….).

Depend on what you want, you'll need to use another argument than xlPasteValue with the PasteSpecial function.

Side note:
Application.DisplayAlerts = False

Open in new window

Before using performances ehencing instructions, ensure that your code behave as expected.
Premature optimisation is the root of all evils.
Thanks to all.
You can run Excel file below
https://1drv.ms/x/s!Ai8CrEskdewXu0qyuwIP4nOs_Rnr?e=JgEhqr 

by choosing relevant .txt file below,
https://1drv.ms/t/s!Ai8CrEskdewXu0sHFJDze7zj5GZ2?e=ZdjzzI 

It is now having issue like
User generated imagedue to this line below.

    Set wsForValidation = Worksheets(Par_Sht)

Open in new window

Previously there was no same issue. How to avoid it?


Peter

How do we select the text file?
Worksheets(Par_Sht) 

Par_Sht contains a string and there is no such sheet at that time...
simple as that... not sure where do you get/set that variables...
Hi all,
Norie,
You can choose attached text file below, when saving Excel file.

3.txt

Hainkurt,
Par_sht is one Sheet name ending with "_C".

All,
For the code below, that is doing copy into another Excel file, it seems the followingA part does remove one sheet from "original" Excel file (wbk is actually new Excel file). How to correct it?
                wbk.Activate
               
                Sh_Exist = False
                Dim j As Integer
                For j = 1 To Worksheets.Count
                     If Worksheets(j).Name = F2 Then
                         Sh_Exist = True
                         Exit For
                     End If
                Next
               
                If Sh_Exist Then
                    Application.DisplayAlerts = False
                    Sheets(F2).Delete
                    Application.DisplayAlerts = True
                End If



Open in new window

Jeeeez, a worksheet completly build by code….
It will be better to have an hidden "master" worksheet that you will just duplicate.

Next, your functions are too long, and break the SRP (Single Responsibility Principle).
Write small functions that do one thing (and do it well).

When dealing with the file system, use the FSO object (it is in the Microsoft Scripting Runtime Library), it is more intuitive and safe.

Finally, what are you trying to achieve ?
With some luck, a Power BI solution will be more reliable, less head-breaking and easyer to maintain.
Hi,
I checked that the part below is running for Child file but issue is that data is copied well to Child file. Any idea?


                Sh_Exist = False
                Dim j As Integer
                For j = 1 To wbk.Worksheets.Count
                     If wbk.Worksheets(j).Name = F2 Then
                         Sh_Exist = True
                         Exit For
                     End If
                Next
                ...

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial