Combining TWO buttons operations into ONE

Zahid0111

New Member
Joined
Mar 8, 2020
Messages
23
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have two buttons in my workbook, they perform different operation by importing data from same workbook but different sheets. I want to combine this into one button, please help me with this.
Button 1 Code is:
VBA Code:
Private Sub CommandButton1_Click()
    'Sheets("RawDataChamla").Visible = True
    Dim fn As String
    Const wsName As String = "Grade Wise"
    fn = Application.GetOpenFilename("ExcelFiles,*.xls?")
    If fn = "False" Then Exit Sub
    If Not IsSheetExistsIn(wsName, fn) Then
        MsgBox Chr(34) & wsName & Chr(34) & " not found in " & fn: Exit Sub
    End If
    Dim cn As Object, rs As Object
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    With cn
        .Provider = "Microsoft.Ace.OLEDB.12.0"
        .Properties("Extended Properties") = "Excel 12.0;HDR=No;"
        .Open fn
    End With
    rs.Open "Select * From [" & wsName & "$a2:e50000] Where F1 Is Not Null;", cn
    Sheets("RawDataChamla").Range("a" & Rows.Count).End(xlUp)(2).CopyFromRecordset rs
    'Sheets("RawDataChamla").Select
    Set cn = Nothing: Set rs = Nothing
    Chamla
End Sub

Button 2 Code is:
VBA Code:
Private Sub CommandButton2_Click()
' Copy data from farmer history sheet to main sheet
Dim srcWB As Workbook
    Dim rCl As Range
    Dim rC2 As Range
    Dim rC3 As Range
    Dim rC4 As Range
    Dim rC5 As Range
    Dim rC6 As Range
    Dim rC7 As Range
    Dim rC8 As Range
    Dim FileToOpen As Variant

    Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename(Title:="Browse for file Farmer History Gradewise(Chamla)", FileFilter:="Excel Files (*.xls*),*xls*")
    If FileToOpen <> False Then
        Set srcWB = Application.Workbooks.Open(FileToOpen)
        With srcWB.Sheets("Farmer History")
            Set rCl = .Range("A1").CurrentRegion.Rows(1).Find(What:="  Crop Area", LookIn:=xlValues)
            If Not rCl Is Nothing Then .Range(rCl, rCl.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("F13")
            
            Set rC2 = .Range("A1").CurrentRegion.Rows(1).Find(What:="  Target Qty", LookIn:=xlValues)
            If Not rC2 Is Nothing Then .Range(rC2, rC2.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("R13")
            
            Set rC3 = .Range("A1").CurrentRegion.Rows(1).Find(What:="Cumulative Sold", LookIn:=xlValues)
            If Not rC3 Is Nothing Then .Range(rC3, rC3.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("S13")
            
            Set rC4 = .Range("A1").CurrentRegion.Rows(1).Find(What:="Village Code", LookIn:=xlValues)
            If Not rC4 Is Nothing Then .Range(rC4, rC4.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("E13")
            
            Set rC5 = .Range("A1").CurrentRegion.Rows(1).Find(What:="Father Name", LookIn:=xlValues)
            If Not rC5 Is Nothing Then .Range(rC5, rC5.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("D13")
            
            Set rC6 = .Range("A1").CurrentRegion.Rows(1).Find(What:="Farmer Name", LookIn:=xlValues)
            If Not rC6 Is Nothing Then .Range(rC6, rC6.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("C13")
            
            Set rC7 = .Range("A1").CurrentRegion.Rows(1).Find(What:="Farmer No.", LookIn:=xlValues)
            If Not rC7 Is Nothing Then .Range(rC7, rC7.End(xlDown)).Offset(1).copy ThisWorkbook.Sheets("Chamla").Range("B13")
            
        End With
        
        srcWB.Close False

    End If
    Application.ScreenUpdating = True
    
LineAll4

End Sub
 
Hi ...again when i paste the above code, some errors pops up..please check..thank you :)
error.PNG

error2.PNG
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Did you delete the buttons?

Also, where is the code at?

You may need to rename your procedures to something not formatted like an event (which doesn't hurt anyway in terms of making the code cleaner).
 
Upvote 0
yes i have made some changes..now its working..thank you dear for the help and time...
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top