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:
Button 2 Code is:
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