Solved

Simple Excel VBA

Posted on 2014-04-02
17
264 Views
Last Modified: 2014-04-02
Hi Experts,

I have one task which I want to be automated by VBA. will you pls help me out on this?

I have three WB Data - Process - Result.

in WB Process I have button Calles "Activate" so by clicking that button. Code do below mention Steps.
1.Match 1st sheet name from data WB to Result WB.
2.If Match not found then copy range A1:F till end from WB Data & past to A1 in WB Process.
3.Pop box ask for values in Cell L3 & M3 & N3. which user manually fill in popup box.
4.End

Note:- only Process WB is open rest two are closed. path for there two WB are "C:\Users\Administrator\Desktop\Data.xlsx" & "C:\Users\Administrator\Desktop\Result.xlsx".

Thank You Awaiting your response.
0
Comment
Question by:itjockey
  • 10
  • 7
17 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39974176
Try this:
Sub Demo()

    Dim wbCurrent As Workbook, wbData As Workbook, wbResult As Workbook
    
    Set wbCurrent = ActiveWorkbook
    Set wbData = Workbooks.Open("C:\Users\MacroShadow\Desktop\Data.xlsx")
    Set wbResult = Workbooks.Open("C:\Users\MacroShadow\Desktop\Result.xlsx")

    If wbData.Sheets(1).Name <> wbResult.Sheets(1).Name Then
    
        wbData.Activate
        wbData.Sheets(1).Range("A1:F" & Range("F1048576").End(xlUp).Row).Copy
        wbResult.Activate
        Range("A1").PasteSpecial

        wbResult.Sheets(1).Range("L3") = InputBox("Please enter value for L3:", "Data input")
        wbResult.Sheets(1).Range("M3") = InputBox("Please enter value for M3:", "Data input")
        wbResult.Sheets(1).Range("N3") = InputBox("Please enter value for N3:", "Data input")

    End If
    
    wbData.Close False
    wbCurrent.Close False

End Sub

Open in new window

0
 
LVL 8

Author Comment

by:itjockey
ID: 39974188
I guess
Sub Demo2()

    Dim wbCurrent As Workbook, wbData As Workbook, wbResult As Workbook
    
    Set wbCurrent = ActiveWorkbook
    Set wbData = Workbooks.Open("H:\4.Trading Master\Thunderbolt\Simple Excel Formula\Data.xlsx")
    Set wbResult = Workbooks.Open("H:\4.Trading Master\Thunderbolt\Simple Excel Formula\Result.xlsx")

    If wbData.Sheets(1).Name <> wbResult.Sheets(1).Name Then
    
        wbData.Activate
        wbData.Sheets(1).Range("A1:F" & Range("F1048576").End(xlUp).Row).Copy
        wbCurrent.Activate
        Range("A1").PasteSpecial

        wbCurrent.Sheets(1).Range("L3") = InputBox("Please enter value for L3:", "Data input")
        wbCurrent.Sheets(1).Range("M3") = InputBox("Please enter value for M3:", "Data input")
        wbCuurent.Sheets(1).Range("N3") = InputBox("Please enter value for N3:", "Data input")

    End If
    
    wbData.Close False
    wbCurrent.Close False

End Sub

Open in new window


But I got Error in
wbCuurent.Sheets(1).Range("N3") = InputBox("Please enter value for N3:", "Data input")

Open in new window


Thanks
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39974194
Sorry, change:
        wbCurrent.Sheets(1).Range("L3") = InputBox("Please enter value for L3:", "Data input")
        wbCurrent.Sheets(1).Range("M3") = InputBox("Please enter value for M3:", "Data input")
        wbCuurent.Sheets(1).Range("N3") = InputBox("Please enter value for N3:", "Data input")

Open in new window

to:
        wbCurrent.Sheets(1).Range("L3").Value = InputBox("Please enter value for L3:", "Data input")
        wbCurrent.Sheets(1).Range("M3").Value = InputBox("Please enter value for M3:", "Data input")
        wbCuurent.Sheets(1).Range("N3").Value = InputBox("Please enter value for N3:", "Data input")

Open in new window

0
 
LVL 8

Author Comment

by:itjockey
ID: 39974195
cant we have only one input box for all three values & values separated by space?

Thanks
0
 
LVL 8

Author Comment

by:itjockey
ID: 39974198
did it but Error
Thanks
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39974207
Sub Demo()

    Dim wbCurrent As Workbook, wbData As Workbook, wbResult As Workbook
    Dim varData() As Variant
    
    Set wbCurrent = ActiveWorkbook
    Set wbData = Workbooks.Open("H:\4.Trading Master\Thunderbolt\Simple Excel Formula\Data.xlsx")
    Set wbResult = Workbooks.Open("H:\4.Trading Master\Thunderbolt\Simple Excel Formula\Result.xlsx")

    If wbData.Sheets(1).Name <> wbResult.Sheets(1).Name Then
    
        wbData.Activate
        wbData.Sheets(1).Range("A1:F" & Range("F1048576").End(xlUp).Row).Copy
        wbCurrent.Activate
        Range("A1").PasteSpecial

        varData = Split(InputBox("Please enter value for L3:N3, seperated by a space.", "Data input"), " ")
        
        wbCurrent.Sheets(1).Range("L3").Value = varData(0)
        wbCurrent.Sheets(1).Range("M3").Value = varData(1)
        wbCuurent.Sheets(1).Range("N3").Value = varData(2)

    End If
    
    wbData.Close False
    wbCurrent.Close False

End Sub

Open in new window

0
 
LVL 8

Author Comment

by:itjockey
ID: 39974217
Error
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39974223
Change line 4 to
Dim varData() As String

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Author Comment

by:itjockey
ID: 39974227
Error
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39974235
there is a typo, change;
wbCuurent.Sheets(1).Range("N3").Value = varData(2)

Open in new window

to:
wbCurrent.Sheets(1).Range("N3").Value = varData(2)

Open in new window

0
 
LVL 8

Author Comment

by:itjockey
ID: 39974241
Sorry that is my mistake. How dumb I am . but there is still errorErrorThanks
0
 
LVL 8

Author Comment

by:itjockey
ID: 39974243
and I guess this is Error
wbResult.Close False

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39974245
That line should not produce an error. What error do you get (in the future please include the error so i don't have to guess what the problem is!)?
0
 
LVL 8

Author Comment

by:itjockey
ID: 39974252
ok done may I ask new question on this context?
0
 
LVL 8

Author Closing Comment

by:itjockey
ID: 39974253
Thank you
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39974257
Sure.
0
 
LVL 8

Author Comment

by:itjockey
ID: 39974268
This is the link Merge this Two VBA Code in To One

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now