+ Reply to Thread
Results 1 to 18 of 18

VBA_Help

Hybrid View

  1. #1
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    VBA_Help

    HI Excel Expert,

    I am seeking your help

    #1 I have a master_report file, where I keep on appending the selected rows from time to time, not montly but whenever there is a requirement. Now I am facing some issue. I will need excel experts help to help me out with this issues.

    I am attaching the master_report file there in Module 1, I have codes to append either the full data by overwriting or to select only few rows and append after the last row. Now until n unless I put the code in sheet 1 it does not work, so how should I modify the codes so that the codes in Module 1 work for sheet 1, also the code in Module 1 works when I type the Month name in ##w2 cell and it find the file in the same folder, here after appeding the file the Month name doesn't disapper, what should I do to make it disapper once the data has been copied or appened.

    #2 In module 2 in the same master_report file, I have codes to do saveas the file , but those are not working properly. Basically I want to do a saveas by changing the file extension to csv and saving it in destination folder and save only from Column A to Column U till the last rows(Not the Information Box which I have put in column X) and link the SaveAS codes to ActiveX control(Aslo this Active X should not be save into csv after doing SaveAs).

    Data in the tables is only for testing purpose doesnot resemble anything/anyone.
    Attached Files Attached Files
    Last edited by Nyay; 06-16-2024 at 12:13 PM.

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,410

    Re: VBA_Help

    Take the worksheet _change event out of the module and paste it into the worksheet module - this will then work after putting in an entry into 'W2'
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

  3. #3
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    I tried doing that it is still not working. Attaching the file again to with the changes I made as suggested by you.

    ## Code in Sheet 1
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.CountLarge > 1 Then Exit Sub
        If Intersect(Target, Range("W2")) Is Nothing Then Exit Sub
    End Sub
    ## Code in Module 1
    
    Sub LinkedToSheet1()
        Dim srcWB As Workbook, desWB As Workbook, desWS As Worksheet, lRow As Long, sPath As String, copyRng As Range
        sPath = ThisWorkbook.Path & "\This_" & "Report_" & Target.Value & ".xlsx"
        If Len(Dir(sPath)) = 0 Then
            MsgBox ("Source file " & sPath & " not found.")
            Exit Sub
        End If
        Set desWB = ThisWorkbook
        Set desWS = desWB.Sheets(1)
        If MsgBox("Do you want to copy all the data from the source workbook or just a selected range?  Click 'Yes' for all data or 'No' to select a range.", vbYesNo) = vbYes Then
            Application.ScreenUpdating = False
            desWS.UsedRange.Offset(1).ClearContents
            Set srcWB = Workbooks.Open(sPath)
            With Sheets(1)
                lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
                If lRow > 1 Then
                    .UsedRange.Offset(1).Copy desWS.Range("A2")
                    MsgBox ("Data appended from " & sPath & " to " & desWB.Name)
                Else
                    MsgBox ("No data found in " & sPath)
                End If
            End With
            srcWB.Close False
            Application.ScreenUpdating = True
        Else
            Application.ScreenUpdating = False
            Set srcWB = Workbooks.Open(sPath)
            Application.ScreenUpdating = True
            Set copyRng = Application.InputBox(Prompt:="Select a range to copy.", Title:="Range Selection", Type:=8)
            Application.ScreenUpdating = False
            copyRng.Copy
            desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial
            srcWB.Close False
            Application.ScreenUpdating = True
        End If
    End Sub
    Attached Files Attached Files
    Last edited by Nyay; 06-16-2024 at 12:33 PM.

  4. #4
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Can someone please help me on this,will be really grateful to you. I just need code correction rest everything is working
    Last edited by Nyay; 06-17-2024 at 03:14 AM.

  5. #5
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Can someone please help me with this.

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VBA_Help

    For #1.
    I have done changes in codes.
    For #2
    Code is available in module 2. I have put button in "New" sheet. Make suitable changes regarding path and file name. Assign macro " cmdSave" to the button.
    The creates a new blank file and copied data from current sheet and paste it new file, saves as .csv file.
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  7. #7
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    WhatsApp Image 2024-06-17 at 19.22.54_a8cb6de1.jpg

    Hi I have enabled macros in the above file but still getting the issue. Please help as I face this issue quite a few time

    Sub cmdSave()
        Dim sFileName As String
        Dim WB As Workbook
        Dim LastRow As Long
        
        Application.DisplayAlerts = False
    
        sFileName = "MyFileName.csv"
        LastRow = ws.UsedRange.Rows.Count
        'Copy the contents of required sheet ready to paste into the new CSV
        Sheets(1).Range("A1:U" & LastRow).CurrentRegion.Copy 'Define your own range if required
    
        'Open a new XLS workbook, save it as the file name
        Set WB = Workbooks.Add
        With WB
            .Title = "MyTitle"
            .Subject = "MySubject"
            .Sheets(1).Select
            ActiveSheet.Paste
            .Saveas "D:\Downloads\Excel" & sFileName, xlCSV
            .Close
        End With
    
        Application.DisplayAlerts = True
    End Sub
    For SaveAs query which is attached in the Module 2, I tried changing the range but it is showing me debug. Please help
    Last edited by Nyay; 06-17-2024 at 10:05 AM.

  8. #8
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VBA_Help

    Do not change the line I have given. It selects all data from column A to U. Last row not required.
    Sheets(1).Range("A1").CurrentRegion.Copy 'Define your own range if required

  9. #9
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Sub cmdSave()
        Dim sFileName As String
        Dim WB As Workbook
        Dim LastRow As Long
        
        Application.DisplayAlerts = False
    
        sFileName = "Testing.csv"
        'Copy the contents of required sheet ready to paste into the new CSV
        Sheets(1).Range("A1").CurrentRegion.Copy 'Define your own range if required
    
        'Open a new XLS workbook, save it as the file name
        Set WB = Workbooks.Add
        With WB
            .Title = "MyTitle"
            .Subject = "MySubject"
            .Sheets(1).Select
            ActiveSheet.Paste
            .Saveas "D:\Downloads\Excel" & sFileName, xlCSV
            .Close
        End With
    
        Application.DisplayAlerts = True
    End Sub
    I have changed the location and the filename still it is not saving the file

  10. #10
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VBA_Help

    I am not having problem.
    Change this line
    .Saveas "D:\Downloads\Excel" & sFileName, xlCSV
    as
    .Saveas "D:\Downloads\Excel\" & sFileName, xlCSV

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,729

    Re: VBA_Help

    Quote Originally Posted by Nyay View Post
    Can someone please help me on this,will be really grateful to you. I just need code correction rest everything is working
    Quote Originally Posted by Nyay View Post
    Can someone please help me with this.
    Administrative Note:

    Welcome to the forum.

    Unfortunately your post does not comply with Guideline #7 of our Forum RULES:

    Do not bump a thread until 24 hours after it is posted.

    It may take time for someone who can help to look at your thread.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  12. #12
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Thanks it is sorted. Thanks for your help. Just one more help

    sometime I don't get the option for adding the module/code in the button what to do in that case.

    WhatsApp Image 2024-06-17 at 21.25.10_7731b59c.jpg
    Last edited by Nyay; 06-17-2024 at 11:55 AM.

  13. #13
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: VBA_Help

    Do not use active X control buttons for assigning macro. Use only form control buttons.

  14. #14
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Thanks, It work smoothly now.

    One more help I am attaching a file, the file has 4 sheet and 5th Sheet #SaveAs I have placed button to click SaveAs.
    I want to saveAs my current workbook, SaveAs in CSV by attaching the SaveAs Macro in the Button which is placed in 5th Sheet. I want only the 4 sheet and only
    data which is present, and not the information tag which I have placed to Add Month Name. Can you please help me with the code, First 4 sheet only columns till last row
    and not the information tag to do SaveAs in CSV format.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Can someone please look into Post #14 and help me with the solution.
    I have SaveAs Button in 5th Worksheet and want to saveAs only 4 worksheet till where the data is populated. Please help

  16. #16
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Can someone please help me with the vba codes for query in Post #14

  17. #17
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,729

    Re: VBA_Help

    Quote Originally Posted by kvsrinivasamurthy View Post
    Do not use active X control buttons for assigning macro. Use only form control buttons.
    I have been using ActiveX buttons for about 20 years without issues. Why are you giving this rather absolute advice?

  18. #18
    Registered User
    Join Date
    06-16-2024
    Location
    Dubai, UAE
    MS-Off Ver
    2019
    Posts
    31

    Re: VBA_Help

    Hey I tried Active X button, it was not function and tried form control and it worked. Can you please help me with my query in comment #14

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1